The Bucharest University of Economic Studies Data Structures ADTs-Abstract Data Types Stacks and Queues
Agenda Definition Graphical representation Internal interpretation Characteristics Operations Implementations
Stack It is a linear structure in which the two basic operations, insertion and deletion are made on a LIFO based rule. These operations are made in a single part of the structure called the head of the stack, or the top. push STACK pop
Stack A logical structure implemented with different type of data structures: linked lists or arrays; A homogenous structure with elements of the same type; Basic operations: Adding an element, called the push operation Removing an element, called the pop operation Looking at the top element, called the peek operation struct Stack{ int Stack * }; info; next;
Stack as an ADT The implementation rule is LIFO Last In First Out The pop operation from a NULL stack is considered an error The push operation into a stack is considered a limitation The element from the top of a stack is the only accessible element top 2 4 1 3 6
Stack graphical representation push pop K C A T S Stack mechanism
Stacks with Linked Lists top of the stack 2 1005 1002 1003 5 1328 new node 2 NULL 3241 3242 8 2500 1328 1329 1005 1006 34 NULL 2500 2501
Stacks with Linked Lists 2 1005 1002 1003 5 1328 top of the stack 2 1002 3241 3242 8 2500 1328 1329 1005 1006 34 NULL 2500 2501
Stacks with Linked Lists top of the stack 2 1005 1002 1003 5 1328 temporary node 2 1002 3241 3242 8 2500 1328 1329 1005 1006 34 NULL 2500 2501
Function call simulation 0x00f11464h int suma(int x, int y) { int z = 0, r = 2; z = x + y; return z; } void main(){ int a = suma(3,8); }
Function call simulation initialization. SP:0x0018FA18h BP:0x0018FA1Ch
Function call simulation push b b: 8. SP:0x0018FA14h SP:0x0018FA18h BP:0x0018FA1Ch
Function call simulation push a a: 3 b: 8. SP:0x0018FA10h SP:0x0018FA14h SP:0x0018FA18h BP:0x0018FA1Ch
Function call simulation push IP IP: 0x00f11464h a: 3 b: 8. SP:0x0018FA0Ch SP:0x0018FA10h SP:0x0018FA14h SP:0x0018FA18h BP:0x0018FA1Ch
Function call simulation push BP BP: 0x0018FA1Ch IP: 0x00f11464h a: 3 b: 8. SP:0x0018FA08h SP:0x0018FA0Ch SP:0x0018FA10h SP:0x0018FA14h SP:0x0018FA18h BP:0x0018FA1Ch
Function call simulation mov BP, SP BP:0x0018FA08h BP: 0x0018FA1Ch IP: 0x00f11464h a: 3 b: 8. SP:0x0018FA08h SP:0x0018FA0Ch SP:0x0018FA10h SP:0x0018FA14h SP:0x0018FA18h BP:0x0018FA1Ch
Function call simulation sub r: 2 esp,8 SP:0x0018FA00h BP:0x0018FA08h z: 0 BP: 0x0018FA1Ch IP: 0x00f11464h a: 3 b: 8. SP:0x0018FA08h SP:0x0018FA0Ch SP:0x0018FA10h SP:0x0018FA14h SP:0x0018FA18h BP:0x0018FA1Ch
Function call simulation mov r: 2 SP, BP SP:0x0018FA00h BP:0x0018FA08h z: 0 BP: 0x0018FA1Ch IP: 0x00f11464h a: 3 b: 8. SP:0x0018FA08h SP:0x0018FA0Ch SP:0x0018FA10h SP:0x0018FA14h SP:0x0018FA18h BP:0x0018FA1Ch
Function call simulation pop r: 2 BP SP:0x0018FA00h BP:0x0018FA08h pop BP z: 0 BP: 0x0018FA1Ch IP: 0x00f11464h a: 3 b: 8. SP:0x0018FA08h SP:0x0018FA0Ch SP:0x0018FA10h SP:0x0018FA14h SP:0x0018FA18h BP:0x0018FA1Ch
Function call simulation return BP:0x0018FA08h pop BP return r: 2 z: 0 BP: 0x0018FA1Ch IP: 0x00f11464h a: 3 b: 8. SP:0x0018FA00h SP:0x0018FA08h SP:0x0018FA0Ch SP:0x0018FA10h SP:0x0018FA14h SP:0x0018FA18h BP:0x0018FA1Ch
Function call simulation BP:0x0018FA08h pop BP return r: 2 z: 0 BP: 0x0018FA1Ch IP: 0x00f11464h a: 3 b: 8. SP:0x0018FA00h SP:0x0018FA08h SP:0x0018FA0Ch SP:0x0018FA10h SP:0x0018FA14h SP:0x0018FA18h BP:0x0018FA1Ch
PUSH Pseudocode 1. memory allocation (newnode); 2. newnode initialization; 3. if (stack_full) stack overflow; else newnode >next = empty(head)? NULL: head; head = newnode;
POP Pseudocode 1. declaring a temporary node (tmpnode); 2. if (stack_empty) stack underflow; else value = head->info; tmpnode = head; head = head >next; free(tmpnode); return value;
QUEUES It is a linear structure in which the two basic operations, insertion and deletion are made on a FIFO based model. The insertion is made on the tail section and the deletion is made on the front section of the queue. dequeue get Queue put enqueue
Queue A logical structure implemented with different type of data structures: linked lists or arrays; A homogenous structure with elements of the same type; Basic operations: Adding an element, called the enqueuer or put operation Removing an element, called the dequeuer or get operation struct Queue{ Node* head; Node* tail; }; struct Node{ int Node * }; info; next;
Queue Implementation model FIFO Fifo In First Out 5 2 7 1 front back
Queue Implementation put E U E U Q get
PUT Pseudocode 1. memory allocation for a new node (newnode); 2. newnode initialization; 3. if (full queue) queue overflow; else if (empty queue) head = tail = newnode ; else tail >next = newnode;
GET Pseudocode 1. declaring a temporary node (tmpnode); 2. if (empty queue) queue underflow; else value = head >info; tmpnode = head; head = head >next; free(tmpnode); return value;
Queues with Linked Lists head of the queue 2 1005 1002 1003 5 1328 1005 1006 2 NULL 3241 3242 8 2500 1328 1329 tail of the queue 34 NULL 2500 2501
Queues with Linked Lists head of the queue 2 1005 1002 1003 5 1328 1005 1006 8 2500 1328 1329 tail of the queue 2 NULL 3241 3242 34 3241 2500 2501
Queues with Linked Lists temporary node 2 1005 1002 1003 head of the queue 5 1328 1005 1006 8 2500 1328 1329 tail of the queue 2 NULL 3241 3242 34 3241 2500 2501
Queue Circular array Initial state (back < front && empty == true): back front
Queue Circular array Enqueue (2): 2 front back
Queue Circular array Enqueue (4): 2 4 front back
Queue Circular array Enqueue (1): 1 2 4 back front
Queue Circular array Enqueue (3): 1 3 2 4 back front
Queue Circular array Dequeue => return 2: 1 3 2 4 back front
Queue Circular array Dequeue => return 4: 1 3 2 4 front back
Queue Circular array Dequeue => return 1: 1 3 2 4 back front
Queue Circular array Dequeue => return 3: 1 3 2 4 back front
Palindrome Able was I ere I saw Elba
Ring buffer
Infix Postfix Prefix notations Evaluating mathematical expressions by using the stack data structure: reordering the elements so that the expression can be evaluated easily by one single crossing over the entire expression; The Polish notation invented by Jan Lukasiewicz also called the prefix expression; The Reverse Polish notation is called postfix notation.
Infix Postfix Prefix notations Evaluating mathematical expressions: Prefix or Polish notation: the operators are written before the operands Postfix or revers Polish notation: the operators are placed after the operands
Infix Postfix Prefix notations The postfix or the reverse Polish notation is much easily interpreted than the infix, normal mathematical expressions: the order of operations is straight forward; the parentheses are not necessary; the evaluation are automatically made by the use of a stack data structure;
Infix Postfix Prefix notations The algorithm for casting an infix expression to a postfix notation is called Dijkstra Shunting-Yard algorithm: using a stack for storing the operators in order to be transferred into the postfix notation; each operator has an adequate hierarchy level as depicted in the next table;
Infix Postfix Prefix notations Operators hierarchy level 1 2 3 4 ( [ { ) ] } + - * /
Infix Postfix Prefix notations Mathematical expression (the infix notation) Polish notation (prefix expression) Reverse Polish notation (postfix expression) 4 + 5 + 4 5 4 5 + 4 + 5 * 5 + 4 * 5 5 4 5 5 * + 4 * 2 + 3 + * 4 2 3 4 2 * 3 + 4 + 2 + 3 + + 4 2 3 4 2 + 3 + 4 * (2 + 3) * 4 + 2 3 4 2 3 + *
Dijkstra Shunting-Yard algorithm Scan the entire infix expression from left to right for tokens (operators, operands and parentheses) For each token, test whether: If token is an operator: All operators from the top of the stack that are higher or equal precedence than the incoming one are popped out from the stack and appended to the postfix expression; after this operation the new token is pushed on the stack;
Dijkstra Shunting-Yard algorithm If token is an operand, append it to the postfix expression; If token is a left parenthesis, push it on the stack; If token is a right parenthesis: Pop all the operators from the stack and append them to the postfix expression, till the matching left parenthesis is found, which is popped out without being added to the result; When all tokens of the infix expression have been scanned, pop all the elements from the stack and append them to the postfix expression;
Infix to Postfix 2*(4+3)+9/3 Stack: Output:2
Infix to Postfix 2*(4+3)+9/3 Stack:* Output:2
Infix to Postfix 2*(4+3)+9/3 Stack:*( Output:2
Infix to Postfix 2*(4+3)+9/3 Stack:*( Output:24
Infix to Postfix 2*(4+3)+9/3 Stack:*(+ Output:24
Infix to Postfix 2*(4+3)+9/3 Stack:*(+ Output:243
Infix to Postfix 2*(4+3)+9/3 Stack:* Output:243+
Infix to Postfix 2*(4+3)+9/3 Stack:+ Output:243+*
Infix to Postfix 2*(4+3)+9/3 Stack:+ Output:243+*9
Infix to Postfix 2*(4+3)+9/3 Stack:+/ Output:243+*9
Infix to Postfix 2*(4+3)+9/3 Stack:+/ Output:243+*93
Infix to Postfix 2*(4+3)+9/3 Stack: Output:243+*93/+
Postfix evaluation algorithm Scan the entire postfix expression from left to right for tokens (operands and operators): For each token, test whether: If token is an operand is pushed on the stack; If token is an operator: The left element is popped from the stack into a x variable; The right element is popped from the stack into a y variable; The operation between x operator y is computed; The result is pushed on the stack; The last value standing on the stack is the result of the expression.
Infix->Postifix->Evaluation E = 2+5*3-(3*2+1)
Bibliografie Ion IVAN, Marius POPA, Paul POCATILU, coordonatori Structuri de date, Editura ASE, Bucuresti, 2008, ISBN 978-606-505-031-0 Ion SMEUREANU Programarea in limbajul C/C++, Editura CISON, 2001 Michael MAIN Data Structures & Other Objects Using Java, Editura Pearson, 2012, ISBN: 9780132911504 http://www.acs.ase.ro http://www.itcsolutions.eu