Lecture 3: Lists, Stacks, and Queues

ECE4050/CSC5050 Algorithms and Data Structures Lecture 3: Lists, Stacks, and Queues 1 Lists A list is a finite, ordered sequence of data items. ...
Author: Jeffry Hubbard
10 downloads 1 Views 1MB Size
ECE4050/CSC5050

Algorithms and Data Structures

Lecture 3: Lists, Stacks, and Queues

1

Lists A list is a finite, ordered sequence of data items.

Important concept: List elements have a position. Notation: What operations should we implement?

2

List Implementation Concepts Our list implementation will support the concept of a current position. Operations will act relative to the current position.

3

List ADT

4

List ADT (Cont’d)

5 5

List ADT Examples List: L.insert(99);

Result: Iterate through the whole list: for (L.moveToStart(); L.currPos()0) { if (l1_left > 0) data1 = L1.getValue(); if (l2_left > 0) data2 = L2.getValue(); if (l1_left == 0) { new_data = data2; L2.next(); l2_left--; } else if (l2_left == 0) { new_data = data1; L1.next(); l1_left--; } else if (data1 < data2) { new_data = data1; L1.next(); l1_left--; } else { new_data = data2; L2.next(); l2_left--; } L.append(new_data); } }

55

(2) int main() { AQueue Q; AStack S; char X; /* code to place data into queue Q */ while (Q.length() > 0) { X = Q.dequeue(); S.push(X); } while (S.length() > 0) { X = S.pop(); Q.enqueue(X); } }

56

(3) int main() { AList L; char char1, char2; int size; int i; /* code to place string into list L */ size = L.length(); i = 0; while (size - 1 - i > i) { L.moveToPos(i); char1 = L.getValue(); L.moveToPos(size - 1 - i); char2 = L.getValue(); if (char1 != char2) return 0; } return 1; } 57