Skip to content

Commit 86fc8de

Browse files
committed
new commit
1 parent 257c6cc commit 86fc8de

File tree

2 files changed

+242
-1
lines changed

2 files changed

+242
-1
lines changed

module-2-programming-project.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
edibleQueue.enqueue('Apples', true);
137137
edibleQueue.enqueue('Peas', false);
138138
edibleQueue.enqueue('Oranges', true);
139-
var foodItem = edibleQueue.dequeue();
139+
var foodItem = objedibleQueue.dequeue();
140140
//console.log(foodItem.toString());
141141
//console.log(foodItem);
142142
console.log(edibleQueue.items);

module-3-programming-assignment.html

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>CMPS 260: Module 3 Programming Assignment</title>
6+
<style>* { font-family: monospace; }</style>
7+
<script>
8+
9+
// NOTE: You must implement the data structures using the no prototype approach.
10+
// This is what the book uses, so you can copy it.
11+
// See also: https://it.pointpark.edu/tutorials/no-prototype-vs-prototype/
12+
13+
// NOTE: Please review the following links regularly:
14+
// https://it.pointpark.edu/tutorials/arrays-vs-objects/
15+
// https://it.pointpark.edu/tutorials/no-prototype-vs-prototype/
16+
// https://it.pointpark.edu/tutorials/implementation-vs-interface/
17+
18+
19+
20+
//--------------------------------//
21+
// The linked list data structure //
22+
//--------------------------------//
23+
console.log("The linked list data structure");
24+
25+
// 1. Suppose you have an array with 1,000 elements and a linked list with 1,000
26+
// elements. For both data structures, you want to retrieve the last entry.
27+
// Does one data structure complete that operation before the other (i.e.,
28+
// is it faster)? If yes, how much faster? If no, why does it take the same
29+
// amount of time?
30+
31+
//Answer: A linked list should be faster becasue it doesn't have to traverse
32+
//the entire array.
33+
34+
//------------------------//
35+
// Creating a linked list //
36+
//------------------------//
37+
console.log("Creating a linked list");
38+
39+
// We are going to implement the linked list data structure described in the
40+
// book. Instead of using 'let' we use 'var' everywhere. Note that this
41+
// implementation does not use the prototype (see homework). Here is a skeleton
42+
// that we will work on completing:
43+
44+
function LinkedList() {
45+
46+
// helper class
47+
var Node = function Node(element) {
48+
this.element = element;
49+
this.next = null;
50+
}
51+
52+
// store length and head
53+
var length = 0;
54+
var head = null;
55+
56+
this.append = function(element) {
57+
var node = new Node(element);
58+
var current;
59+
60+
if (head === null) { //first node in the List
61+
62+
head = node;
63+
64+
} else {
65+
66+
current = head;
67+
while(current.next) {
68+
current = current.next;
69+
}
70+
71+
current.next = node;
72+
}
73+
74+
length++;
75+
};
76+
77+
this.remove = function(element) {
78+
// remove a specific element from the list
79+
// ...
80+
};
81+
82+
this.removeAt = function(position) {
83+
84+
if (position > -1 && position < length) {
85+
var current = head;
86+
var previous;
87+
var index = 0;
88+
89+
if (position === 0) {
90+
91+
head = current.next;
92+
93+
} else {
94+
95+
while (index++ < position ) {
96+
97+
previous = current;
98+
current = current.next;
99+
100+
}
101+
102+
previous.next = current.next;
103+
104+
}
105+
106+
length--;
107+
return current.element;
108+
109+
} else {
110+
111+
return null;
112+
113+
}
114+
};
115+
116+
this.insert = function(position, element) {
117+
118+
if (position >= 0 && position <= length) {
119+
var node = new Node(element);
120+
current = head;
121+
var previous;
122+
index = 0;
123+
124+
if (position === 0) {
125+
node.next = current;
126+
head = node;
127+
128+
} else {
129+
130+
while (index++ < position) {
131+
previous = current;
132+
current = current.next;
133+
}
134+
node.next = current;
135+
previous.next = node;
136+
}
137+
138+
length++;
139+
return true;
140+
141+
} else {
142+
143+
return false;
144+
145+
}
146+
}
147+
148+
this.toString = function() {
149+
// overwrite the default toString method
150+
var current = head;
151+
var string = "List: ";
152+
while (current) {
153+
string += current.element + (current.next ? " -> " : "");
154+
current = current.next;
155+
}
156+
return string;
157+
};
158+
159+
this.indexOf = function(element) {
160+
161+
var current = head;
162+
var index = -1;
163+
164+
while (current) {
165+
if (element === current.element) {
166+
return index;
167+
}
168+
index++;
169+
current = current.next;
170+
}
171+
return -1;
172+
}; //end of indexOf()
173+
174+
this.isEmpty = function() {
175+
return length === 0;
176+
};
177+
178+
this.size = function() {
179+
return length;
180+
};
181+
182+
this.getHead = function() {
183+
return head;
184+
};
185+
186+
this.print = function() {
187+
// print to the console
188+
console.log(this.toString());
189+
};
190+
191+
192+
}; //end of function LinkedList
193+
194+
// create a linked list to operate on
195+
var list = new LinkedList();
196+
list.append(25);
197+
list.append(30);
198+
list.append(46);
199+
list.append("hello world");
200+
list.append(99);
201+
list.append("pino noir");
202+
list.indexOf(99);
203+
list.size();
204+
list.toString();
205+
list.insert("malbec");
206+
list.toString();
207+
list.isEmpty();
208+
list.
209+
// 1. Write pseudo code (meaning; use comments) to describe how you would
210+
// implement the append method. HINT: What two scenarios can you distinguish?
211+
212+
213+
/*
214+
pseudo code
215+
*/
216+
217+
// 2. Translate your pseudo code into actual code and test it.
218+
// HINT: Use list.print() to test your code.
219+
220+
// 3. Repeat (1) and (2) for insert.
221+
222+
// 4. Repeat (1) and (2) for removeAt.
223+
224+
// 5. Repeat (1) and (2) for remove.
225+
226+
// 6. Repeat (1) and (2) for indexOf.
227+
228+
// 7. Repeat (1) and (2) for isEmpty.
229+
230+
// 8. Repeat (1) and (2) for size.
231+
232+
// 9. Repeat (1) and (2) for getHead.
233+
234+
235+
236+
</script>
237+
</head>
238+
<body>
239+
See console!
240+
</body>
241+
</html>

0 commit comments

Comments
 (0)