Skip to content

Commit

Permalink
SLL SList (prompt)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinsosborne committed Sep 14, 2017
1 parent 0c3a1d8 commit e5b331e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
44 changes: 44 additions & 0 deletions Chapter 5 - Linked Lists/sList(prompt).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Create an SList with values entered. Use the prompt function to gather values one at a time from the user, putting each into a ListNode that you add to the end of the list. When the user hits [Cancel], return the list you have created.

// list node function
function listNode(val){
this.val = val;
this.next = null;
}

// SLL function
function sLinkedList(){
this.head = null;
this.display = function(){
var runner = this.head;
var string = "Node values in the list: ";
while(runner){
string += runner.val + ", ";
runner = runner.next;
}
console.log(string);
return this;
}
}

var list = new sLinkedList();
var node1 = new listNode(10);
list.head = node1;

var response = prompt("Enter value for list node: ");
while(response){
var newNode = new listNode(response);
if(!list.head){
list.head = newNode;
}
else{
var runner = list.head;
while(runner.next){
runner = runner.next;
}
runner.next = newNode;
}
response = prompt("Enter value for list node: ");
}

list.display();

0 comments on commit e5b331e

Please sign in to comment.