Download presentation
Presentation is loading. Please wait.
Published byKatherine Skinner Modified over 9 years ago
1
Lists, Stacks and Queues
2
2 struct Node{ double data; Node* next; }; class List { public: List(); // constructor List(const List& list); // copy constructor ~List(); // destructor List& operator=(const List& list); // assignment operator // plus other overloaded operators bool empty() const; // boolean function void add(double x); // add to the head void delete(); // delete the head and get the head element double head() const; // get the head element bool search(double x); // search for a given x void insert(double x); // insert x in a sorted list void delete(double x); // delete x in a sorted list void addEnd(double x); // add to the end void deleteEnd(); // delete the end and get the end element double end(); // get the element at the end void display() const; // output int length() const; // count the number of elements private: Node* head; }; More complete list ADT
3
3 class List { public: List(); // constructor List(const List& list); // copy constructor ~List(); // destructor bool empty() const; // boolean function double head() const; // get the head element void add(double x); // add to the head void delete(); // delete the head element void display() const; // output private: … }; list ADT (a general list) Or to define one function: Double delete(); Which deletes and returns the head element.
4
4 class List { public: List(); // constructor List(const List& list); // copy constructor ~List(); // destructor bool empty() const; // boolean function double head(); const; // get the first element void insert(double x); // insert x in a sorted list void delete(double x); // delete x in a sorted list void display() const; // output bool search(double x); // search for a given x private: … }; list ADT (a sorted list)
5
5 * Implementation n Static array n Dynamic array n Linked lists with and without ‘dummy head’ tricks * Efficiency n Insertion construction, composition n Deletion destruction, decomposition n Search application, usage Implementation and Efficiency
6
6 Advantages and disadvantages of different implementations of a list Static array Dynamic array Linked list Insertion Deletion Search
7
7 Stacks
8
8 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations of stacks using n array n linked list
9
9 Stack * A stack is a list in which insertion and deletion take place at the same end n This end is called top n The other end is called bottom * Stacks are known as LIFO (Last In, First Out) lists. n The last element inserted will be the first to be retrieved
10
10 Push and Pop * Primary operations: Push and Pop * Push n Add an element to the top of the stack * Pop n Remove the element at the top of the stack top empty stack A top push an element top push another A B top pop A
11
11 Implementation of Stacks * Any list implementation could be used to implement a stack n Arrays (static: the size of stack is given initially) n Linked lists (dynamic: never become full) * We will explore implementations based on arrays and linked list
12
12 class Stack { public: Stack(); // constructor Stack(const Stack& stack); // copy constructor ~Stack(); // destructor bool empty() const; double top() const; // keep the stack unchanged void push(const double x); double pop(); // change the stack … // optional void display() const; bool full() const; private: … }; Stack ADT update, ‘logical’ constructor/destructor, composition/decomposition ‘physical’ constructor/destructor Compare with List, see that it’s ‘operations’ that define the type! ‘stack’ is the same as our ‘unsorted’ list operating at the head inspection, access headElement addHead deleteHead
13
13 Using Stack int main() { Stack stack; stack.push(5.0); stack.push(6.5); stack.push(-3.0); stack.push(-8.0); stack.display(); cout << "Top: " << stack.top() << endl; stack.pop(); cout << "Top: " << stack.top() << endl; while (!stack.empty()) stack.pop(); stack.display(); return 0; } result
14
14 struct Node{ public: double data; Node* next; }; class Stack { public: Stack(); // constructor Stack(const Stack& stack); // copy constructor ~Stack(); // destructor bool empty() const; double top() const; // keep the stack unchanged void push(const double x); double pop(); // change the stack bool full(); // unnecessary for linked lists void display() const; private: Node* top; }; Stack using linked lists
15
15 void List::addHead(int newdata){ Nodeptr newPtr = new Node; newPtr->data = newdata; newPtr->next = head; head = newPtr; } void Stack::push(double x){ Node* newPtr = new Node; newPtr->data = x; newPtr->next = top; top = newPtr; } From ‘addHead’ to ‘push’ Push (addHead), Pop (deleteHead)
16
16 Stack using arrays class Stack { public: Stack(int size = 10); Stack(const Stack& stack); ~Stack() { delete[] values; } bool empty() { return (top == -1); } double top(); void push(const double x); double pop(); bool full() { return (top == size); } void display(); private: int size;// max stack size = size - 1 int top;// current top of stack double* values;// element array };
17
17 Attributes of Stack size : the max size of stack top : the index of the top element of stack values : point to an array which stores elements of stack Operations of Stack empty : return true if stack is empty, return false otherwise full : return true if stack is full, return false otherwise top : return the element at the top of stack push : add an element to the top of stack pop : delete the element at the top of stack display : print all the data in the stack
18
18 Allocate a stack array of size. By default, size = 10. Initially top is set to -1. It means the stack is empty. When the stack is full, top will have its maximum value, i.e. size – 1. Stack::Stack(int size /*= 10*/) { values=new double[size]; top=-1; maxTop=size - 1; } Although the constructor dynamically allocates the stack array, the stack is still static. The size is fixed after the initialization. Stack constructor
19
19 * void push(const double x); n Push an element onto the stack n Note top always represents the index of the top element. After pushing an element, increment top. void Stack::push(const double x) { if (full()) // if stack is full, print error cout << "Error: the stack is full." << endl; else values[++top] = x; }
20
20 * double pop() n Pop and return the element at the top of the stack n Don’t forgot to decrement top double Stack::pop() { if (empty()) { //if stack is empty, print error cout << "Error: the stack is empty." << endl; return -1; } else { return values[top--]; }
21
21 * double top() n Return the top element of the stack n Unlike p op, this function does not remove the top element double Stack::top() { if (empty()) { cout << "Error: the stack is empty." << endl; return -1; } else return values[top]; }
22
22 * void print() n Print all the elements void Stack::print() { cout "; for (int i = top; i >= 0; i--) cout << "\t|\t" << values[i] << "\t|" << endl; cout << "\t|---------------|" << endl; }
23
23 A better variant: 23 * A better approach is to let position 0 be the bottom of the stack n An integer to indicate the top of the stack (Stack.h) [7]? [6]? [5]77 [4]121 [3]64 [2]234 [1]51 [0]64 [7]? [6]95 [5]77 [4]121 [3]64 [2]234 [1]51 [0]29 [7]80 [6]95 [5]77 [4]121 [3]64 [2]234 [1]51 [0]29 [7]? [6]95 [5]77 [4]121 [3]64 [2]234 [1]51 [0]29 Push 95 Push 80 Pop top =
24
24 Stack Application: base conversion 24 226 213…0 26…1 23…0 21…1 0…1 Convert a base ten number into a base two number. Example: 26 11010
25
25 Stack stack(); while (D is not zero) { push the remainder of D D D/2 } while (not empty stack) pop
26
26 * Stack.h, Stack.cpp and BaseConversion.cpp * Can be written easily using recursion (do it yourself)
27
27 convert(D) if D is zero, stop else push(the remainder) convert(D/2) while (not empty stack) pop Recursive version:
28
28 Stack Application: Balancing Symbols * To check that every right brace, bracket, and parentheses must correspond to its left counterpart n e.g. [( )] is legal, but [( ] ) is illegal * How? n Need to memorize n Use a counter, several counters, each for a type of parenthesis …
29
29 Balancing Symbols using a stack * Algorithm (1) Make an empty stack. (2) Read characters until end of file i. If the character is an opening symbol, push it onto the stack ii. If it is a closing symbol, then if the stack is empty, report an error iii. Otherwise, pop the stack. If the symbol popped is not the corresponding opening symbol, then report an error (3) At end of file, if the stack is not empty, report an error (paren.cpp)
30
30 Stack Application: postfix, infix expressions and calculator * Evaluating Postfix expressions n a b c * + d e * f + g * + n Operands are in a stack * Converting infix to postfix n a+b*c+(d*e+f)*g a b c * + d e * f + g * + n Operators are in a stack * Calculator n Adding more operators …
31
31 Infix, prefix, and postfix 31 * Consider the arithmetic statement in the assignment statement: x = a * b + c * This is "infix" notation: the operators are between the operands * Compiler must generate machine instructions 1.LOADa 2.MULTb 3.ADDc 4.STOREx
32
32 Examples 32 InfixRPN (Postfix)Prefix A + BA B ++ A B A * B + CA B * C ++ * A B C A * (B + C)A B C + ** A + B C A - (B - (C - D))A B C D----A-B-C D A - B - C - DA B-C-D----A B C D Prefix : Operators come before the operands
33
33 RPN or Postfix Notation 33 * Reverse Polish Notation (RPN) = Postfix notation * Most compilers convert an expression in infix notation to postfix * The operators are written after the operands n So “a * b + c” becomes “a b * c +” n Easier for compiler to work on loading and operation of variables * Advantage: expressions can be written without parentheses
34
34 Evaluating RPN Expressions (Similar to Compiler Operations) Example: 2 3 4 + 5 6 - - * 2 3 4 + 5 6 - - * 2 7 5 6 - - * 2 7 -1 - * 2 8 * 16
35
35 while (not the end of the expression) do { get the current ‘token’ if it is operand, store it … a stack if it is operator, get back operands Evaluate store the result move to the next } Use a Stack to store ‘operands’ a stack! An expression is a list of ‘characters’ or more generally ‘tokens’ or ‘strings
36
36 while (not the end of the expression) { get the current ‘token’ if it is operand, push; else if it is operator, pop (one operand); pop (another operand); evaluate; push (the result); move to the next }
37
37 list expression stack empty while (notempty(list)) do { c = head(list); if (c == ‘operand’) push(c,stack) else if (c == ‘operator) operand2 = pop(stack); operand1 = pop(stack); res=evaluate(c,operand1,operand2); push(res,stack); list delete(list) } Further refine: - operators: +, -, *, / - operands: … - evaluate: …
38
38 List list(expression); Stack stack(); while (list.notempty) do { c = list.head(); if (c == ‘operand’) stack.push(c) else if (c == ‘operator) operand2 = stack.pop; operand1 = stack.pop; res=evaluate(c,operand1,operand2); stack.push; list.delete(); } Further refine: - operators: +, -, *, / - operands: … - evaluate: … More object-oriented:
39
39 24*95+- 4*95+- *95+- 5+- +- (end of strings) - 8 4 2 2 5 9 8 9 8 95+- 14 8 -6 Push 2 onto the stack Value of expression is on top of the stack Push 4 onto the stack Pop 4 and 2 from the stack, multiply, and push the result 8 back Push 9 onto the stack Push 5 onto the stack Pop 5 and 9 from the stack, add, and push the result 14 back Pop 14 and 8 from the stack, subtract, and push the result -6 back
40
40 RPN Conversion If not fully parenthesized, complete the parentheses 1. Given a fully parenthesized infix expression 2. Replace each right parenthesis by the corresponding operator 3. Erase all left parentheses A * (B + C) (A (B C + * A B C + * (A * (B + C) ) A * B + C ((A B * C + A B * C + ((A * B) + C)
41
41 while (not end of expression) c = the current ‘token’ if c is ‘operator’ store it somewhere … ( a stack!) else if c is ‘operand’, store it somewhere else, a list of output expression A stack for operators, not operands!
42
42 ((A * B) + C) *((*(( A B AB* ( +(+( AB*CAB*C+ stack list (result) list (input)
43
43 while (not end of expression) { c = the current ‘token’ If c is ‘(‘ … else if c is ‘)’ … else if c is ‘operator’ … push … else if c is ‘operand’, … pop … } while (not end of the stack) { pop } But operators have different priorities!
44
44 while (not end of expression) c = the current ‘token’ If c is ‘(’ push else if c is ‘)’ while (the top is not ‘(‘ ) pop and display else if c is ‘operator’, while (the top has higher priority than c, and is not ‘(‘ ) pop and display push else if c is ‘operand’ pop and display while (notempty stack) pop an display postfix.cpp Cannot push an operator on top of a higher or equal priority operator ‘display’ is actually a list: insert at the end
45
45 A+B*C+(D*E+F)*G + A B ABC*+DE*F+G*+ stack list (result) list (input)
46
46 Stack Application: function calls and recursion int fac(int n){ int product; if(n <= 1) product = 1; else product = n * fac(n-1); return product; } void main(){ int number; cout << "Enter a positive integer : " << endl;; cin >> number; cout << fac(number) << endl; }
47
47 Assume the number typed is 3. fac(3): has the final returned value 6 3<=1 ? No. product 3 = 3*fac(2) product 3 =3*2=6, return 6, fac(2): 2<=1 ? No. product 2 = 2*fac(1) product 2 =2*1=2, return 2, fac(1): 1<=1 ? Yes. return 1 Tracing the program …
48
48 fac(3)prod3=3*fac(2) prod2=2*fac(1)fac(2) fac(1)prod1=1 Call is to ‘push’ and return is to ‘pop’! top Actually for any function call, it is a stack!
49
49 Stack stack(); while (D is not zero) { push the remainder of D D D/2 } while (not empty stack) pop Let the system do the ‘stack’ for you! convert(D) if D is zero, stop else push(the remainder) convert(D/2) while (not empty stack) pop and display convert(D) if D is zero, stop else convert(D/2) display the remainder of D The same!
50
50 Static and dynamic objects * ‘static variables’ are from a Stack * ‘dynamic variables’ are from a heap (seen later …)
51
51 Array versus linked list implementations * push, pop, top are all constant-time operations in both array and linked list implementation n For array implementation, the operations are performed in very fast constant time * Often we use ‘array’ implementations for Stacks
52
52 Queues * A queue is a waiting line – seen in daily life n A line of people waiting for a bank teller n A line of cars at a toll both n "This is the captain, we're 5th in line for takeoff"
53
53 Queue * A queue is also a list. However, insertion is done at one end, while deletion is performed at the other end. * It is “First In, First Out (FIFO)” order. n Like customers standing in a check-out line in a store, the first customer in is the first customer served.
54
54 Queue Overview * Queue ADT * Basic operations of queue n Enqueuing, dequeuing etc. * Implementation of queue n Linked list n Array
55
55 Enqueue and Dequeue * Primary queue operations: Enqueue and Dequeue * Like check-out lines in a store, a queue has a front and a rear. * Enqueue – insert an element at the rear of the queue * Dequeue – remove an element from the front of the queue Insert (Enqueue) Remove (Dequeue) rearfront
56
56 Implementation of Queue * Just as stacks can be implemented as arrays or linked lists, so with queues. * Dynamic queues have the same advantages over static queues as dynamic stacks have over static stacks
57
57 class Queue { public: Queue(); Queue(const Queue& queue); ~Queue(); bool empty(); double front(); // optional: front inspection (no queue change) void enqueue(double x); double dequeue(); // optional void display(void); bool full(); private: … }; Queue ADT ‘physical’ constructor/destructor ‘logical’ constructor/destructor
58
58 Using Queue int main(void) { Queue queue; cout << "Enqueue 5 items." << endl; for (int x = 0; x < 5; x++) queue.enqueue(x); cout << "Now attempting to enqueue again..." << endl; queue.enqueue(5); queue.print(); double value; value=queue.dequeue(); cout << "Retrieved element = " << value << endl; queue.print(); queue.enqueue(7); queue.print(); return 0; }
59
59 Struct Node { double data; Node* next; } class Queue { public: Queue(); Queue(const Queue& queue); ~Queue(); bool empty(); void enqueue(double x); double dequeue(); // bool full(); // optional void print(void); private: Node* front;// pointer to front node Node* rear;// pointer to last node int counter;// number of elements }; Queue using linked lists
60
60 class Queue { public: Queue() {// constructor front = rear = NULL; counter= 0; } ~Queue() {// destructor double value; while (!empty()) dequeue(value); } bool empty() { if (counter) return false; else return true; } void enqueue(double x); double dequeue(); // bool full() {return false;}; void print(void); private: Node* front;// pointer to front node Node* rear;// pointer to last node int counter;// number of elements, not compulsary }; Implementation of some online member functions …
61
61 Enqueue (addEnd) void Queue::enqueue(double x) { Node* newNode = new Node; newNode->data = x; newNode->next = NULL; if (empty()) { front= newNode; } else { rear->next =newNode; } rear = newNode; counter++; } 8 rear newNode 5 58
62
62 Dequeue (deleteHead) double Queue::dequeue() { double x; if (empty()) { cout << "Error: the queue is empty." << endl; exit(1); // return false; } else { x = front->data; Node* nextNode = front->next; delete front; front= nextNode; counter--; } return x; } front 583 8 5
63
63 Printing all the elements void Queue::print() { cout "; Node* currNode=front; for (int i = 0; i < counter; i++) { if (i == 0) cout << "\t"; elsecout << "\t\t"; cout data; if (i != counter - 1) cout << endl; else cout << "\t<-- rear" << endl; currNode=currNode->next; }
64
64 Queue using Arrays * There are several different algorithms to implement Enqueue and Dequeue * Naïve way n When enqueuing, the front index is always fixed and the rear index moves forward in the array. front rear Enqueue(3) 3 front rear Enqueue(6) 3 6 front rear Enqueue(9) 3 6 9
65
65 * Naïve way (cont’d) n When dequeuing, the front index is fixed, and the element at the front the queue is removed. Move all the elements after it by one position. (Inefficient!!!) Dequeue() front rear 6 9 Dequeue() front rear 9 rear = -1 front
66
66 * A better way n When enqueued, the rear index moves forward. n When dequeued, the front index also moves forward by one element XXXXOOOOO (rear) OXXXXOOOO (after 1 dequeue, and 1 enqueue) OOXXXXXOO (after another dequeue, and 2 enqueues) OOOOXXXXX (after 2 more dequeues, and 2 enqueues) (front) The problem here is that the rear index cannot move beyond the last element in the array.
67
67 Using Circular Arrays * Using a circular array * When an element moves past the end of a circular array, it wraps around to the beginning, e.g. n OOOOO7963 4OOOO7963 (after Enqueue(4)) * How to detect an empty or full queue, using a circular array algorithm? n Use a counter of the number of elements in the queue.
68
68 class Queue { public: Queue(int size = 10);// constructor Queue(const Queue& queue); // not necessary! ~Queue() { delete[] values; } // destructor bool empty(void); void enqueue(double x); // or bool enqueue(); double dequeue(); bool full(); void print(void); private: int front;// front index int rear;// rear index int counter;// number of elements int maxSize;// size of array queue double* values;// element array }; full() is not essential, can be embedded
69
69 Attributes of Queue front/rear : front/rear index counter : number of elements in the queue maxSize : capacity of the queue values : point to an array which stores elements of the queue Operations of Queue empty : return true if queue is empty, return false otherwise full : return true if queue is full, return false otherwise enqueue : add an element to the rear of queue dequeue : delete the element at the front of queue print : print all the data
70
70 Queue constructor * Queue(int size = 10) n Allocate a queue array of size. By default, size = 10. n front is set to 0, pointing to the first element of the array n rear is set to -1. The queue is empty initially. Queue::Queue(int size /* = 10 */) { values=new double[size]; maxSize=size; front=0; rear=-1; counter=0; }
71
71 Empty & Full Since we keep track of the number of elements that are actually in the queue: counter, it is easy to check if the queue is empty or full. bool Queue::empty() { if (counter==0)return true; elsereturn false; } bool Queue::full() { if (counter < maxSize)return false; elsereturn true; }
72
72 Enqueue void Queue::enqueue(double x) { if (full()) { cout << "Error: the queue is full." << endl; exit(1); // return false; } else { // calculate the new rear position (circular) rear = (rear + 1) % maxSize; // insert new item values[rear]= x; // update counter counter++; // return true; } Or ‘bool’ if you want
73
73 Dequeue double Queue::dequeue() { double x; if (empty()) { cout << "Error: the queue is empty." << endl; exit(1); // return false; } else { // retrieve the front item x= values[front]; // move front front= (front + 1) % maxSize; // update counter counter--; // return true; } return x; }
74
74 Printing the elements void Queue::print() { cout "; for (int i = 0; i < counter; i++) { if (i == 0) cout << "\t"; elsecout << "\t\t"; cout << values[(front + i) % maxSize]; if (i != counter - 1) cout << endl; else cout << "\t<-- rear" << endl; }
75
75 Using Queue int main(void) { Queue queue; cout << "Enqueue 5 items." << endl; for (int x = 0; x < 5; x++) queue.enqueue(x); cout << "Now attempting to enqueue again..." << endl; queue.enqueue(5); queue.print(); double value; value=queue.dequeue(); cout << "Retrieved element = " << value << endl; queue.print(); queue.enqueue(7); queue.print(); return 0; }
76
76 Results Queue implemented using linked list will be never full! based on array based on linked list
77
77 Queue applications * When jobs are sent to a printer, in order of arrival, a queue. * Customers at ticket counters …
78
78 Counting Sort * Assume N integers are to be sorted, each is in the range 1 to M. * Define an array B[1..M], initialize all to 0 O(M) * Scan through the input list A[i], insert A[i] into B[A[i]] O(N) * Scan B once, read out the nonzero integers O(M) Total time: O(M + N) n if M is O(N), then total time is O(N) n Can be bad if range is very big, e.g. M=O(N 2 ) N=7, M = 9, Want to sort 8 1 9 5 2 6 3 12 589 Output: 1 2 3 5 6 8 9 36
79
79 * What if we have duplicates? * B is an array of pointers. * Each position in the array has 2 pointers: head and tail. Tail points to the end of a linked list, and head points to the beginning. * A[j] is inserted at the end of the list B[A[j]] * Again, Array B is sequentially traversed and each nonempty list is printed out. * Time: O(M + N)
80
80 M = 9, Wish to sort 8 5 1 5 9 5 6 2 7 1256789 Output: 1 2 5 5 5 6 7 8 9 5 5
81
81 Bin (or Bucket) Sort * The nodes are placed into bins, each bin contains nodes with the same score * Then we combine the bins to create a sorted chain * Note that it does not change the relative order of nodes that have the same score, the so-called stable sort. A datum has two ‘properties’, key and value For example, print out your midterm scores (over a 100 scale or letter grades (A->5, B->4, …) ) in decreasing and alphabetical order!
82
82 Generalization of counting sort * an array of ‘bins’, each ‘bin’ is processed on its own * a ‘bin’ is just generally a ‘list’ * an array is a ‘list’ (but be careful, we are ‘sorting’ arrays!) * it is a list of lists
83
83 Radix Sort * Extra information: every integer can be represented by at most k digits n d 1 d 2 …d k where d i are digits in base r n d 1 : most significant digit n d k : least significant digit
84
84 * Algorithm n sort by the least significant digit first (counting sort) => Numbers with the same digit go to same bin n reorder all the numbers: the numbers in bin 0 precede the numbers in bin 1, which precede the numbers in bin 2, and so on n sort by the next least significant digit n continue this process until the numbers have been sorted on all k digits
85
85 * Least-significant-digit-first Example: 275, 087, 426, 061, 509, 170, 677, 503 170 061 503 275 426 087 677 509 Respect the ordering: a queue
86
86 170 061 503 275 426 087 677 509 503 509 426 061 170 275 677 087 061 087 170 275 426 503 509 677
87
87 * Does it work? * Clearly, if the most significant digit of a and b are different and a < b, then finally a comes before b * If the most significant digit of a and b are the same, and the second most significant digit of b is less than that of a, then b comes before a.
88
88 Finding digit Di From Key * We want to find the i th digit in our key A A = D d D d-1.. D i …D 2 D 1 mod and div are integer operators Let D = 10 i Then Di = (A mod D) div (D/10) Get Digit Example. A=30487 we want D 3 i =3, D = 1000 A mod 1000 = 487(first) 487 div (D/10)(second) 487 div (100) = 4result!
89
89 // base 10 // d times of counting sort // re-order back to original array // scan A[i], put into correct slot // FIFO A=input array, n=|numbers to be sorted|, d=# of digits, k=the digit being sorted, j=array index Ten queues Repeat d times
90
90 Complexity analysis * Increasing the base r decreases the number of passes * Running time n k passes over the numbers (i.e. k counting sorts, with range being 0..r) n each pass takes 2N n total: O(2Nk)=O(Nk) n r and k are constants: O(N) * Note: n radix sort is not based on comparisons; the values are used as array indices n If all N input values are distinct, then k = (log N) (e.g., in binary digits, to represent 8 different numbers, we need at least 3 digits). Thus the running time of Radix Sort also become (N log N).
91
91 Example 2, sorting cards n 2 digits for each card: d 1 d 2 n d 1 = : base 4 n d 2 = A, 2, 3,...J, Q, K: base 13 A 2 3 ... J Q K n 2 2 5 K
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.