Upcoming SlideShare
×

# Data Structures: Stacks (Part 1)

400 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
400
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
14
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Data Structures: Stacks (Part 1)

1. 1.     Linear list in which insertions and deletions can take place only at the top Last-In-First-Out (LIFO) list Insertion (push) Deletion (pop) 1
2. 2. 6 6 6 6 6 6 5 5 5 5 5 5 4 4 4 4 4 4 3 3 3 3 3 3 2 2 2 2 2 1 1 top 1 0 0 top top 0 2 2 -23 29 1 29 2 0 2 top 1 29 0 top 2 1 top 0 2 -1 Empty stack Push 2 Push 29 Push -23 Pop Pop 2
3. 3.    Linear array or a linear linked list Representing a Stack using an Array Requirements  a variable, called top that holds the index of the top element  an array to hold the elements of the stack 3
4. 4. const int MAX = 10; class stack { int top, list [MAX]; public: stack() { top= –1;} void push (int); int pop(); int full(); int empty(); void display(); }; 4
5. 5.  Inserts an element X onto a stack S of size MAX. TOP is a pointer to the top element of the stack. 1. [Check for stack overflow] If TOP MAX then Write ('STACK OVERFLOW') Return 2. [Increment TOP] TOP TOP + 1 3. [Insert element] S [TOP] X 4. [Finish] 5
6. 6. int full() { if(top = = MAX–1) return 1; return 0; } void push (int num) { list[++top] = num; } 6
7. 7.  Removes and returns the top element from a stack S. TOP is a pointer to the top element of the stack. 1. [Check for stack underflow] If TOP = –1 then Write('STACK UNDERFLOW') Return 2. [Decrement pointer] TOP TOP – 1 3. [Return former top element of stack and Finish] Return (S [TOP + 1]) 7
8. 8. int empty() { if(top = = –1) { cout<<"Stack underflow!"<<endl; return 1; } return 0; } 8
9. 9. int pop() { int num; num = list[top--]; return (num); } 9
10. 10. void display() { if(!empty()) { cout<<"Index Element"<<endl; for(int i=0; i<=top; i++) cout<<setw(3)<<i<<setw(10)<<list[i]<<endl; } } 10
11. 11. void display() { if(!empty()) { cout<<"Index Element"<<endl; for(int i=0; i<=top; i++) cout<<setw(3)<<i<<setw(10)<<list[i]<<endl; } } 11
12. 12.  Infix Notation  Operand1 Operator Operand2  A+B  Polish (Prefix) Notation  Operator Operand1 Operand2  +AB  Reverse Polish (Postfix or Suffix) Notation  Operand1 Operand2 Operator  AB+ 12
13. 13.  (A – B / C) * (A * K – L) (A – [/BC]) * ([* AK] – L) [–A/BC] * [–*AKL] *–A/BC–*AKL 13
14. 14.  (A – B / C) * (A * K – L) (A – [BC/]) * ([AK*] – L) [ABC/–] * [AK*L –] ABC/–AK*L–* 14
15. 15.   The order of operations determined by the positions of the operators and operands Parentheses are not needed 15
16. 16.  Programs that perform some sort of translation often use stacks     Example: The translation of an arithmetic expression from infix notation to prefix or postfix notation. Using infix notation, one cannot tell the order in which the operators should be applied by looking at the expression Multiple operations use precedence rules The expression in the postfix notation is much easy to evaluate 16
17. 17.   As the operands appear before the operator, there is no need for operator precedence or for parentheses to group/order operations. In order to evaluate a postfix expression  It is scanned from left to right.  Operands are pushed onto a stack  When an operator is encountered  pop top one or two operands (depending on the operator)  perform the operation and place the result back on the stack.  At the end of the expression, the stack contains only one element (if expression is correct) and that is the resulting value of the expression 17
18. 18. Input: Infix string. Output: Postfix string. 1. Examine the next element in the input. 2. If it is an operand, output it, go to step 6. 3. If it is an opening parenthesis, push it, go to step 6. 4. If it is an operator, then a. If stack is empty, or the top of the stack is opening parenthesis, push the operator. b. If the operator has higher priority than the top of stack, push the operator. Else pop the operator from the stack and output it, repeat step 4a. 5. If it is a closing parenthesis, pop operators from the stack and output them until an opening parenthesis is encountered. Pop and discard the opening parenthesis. 6. If there is more input, go to step 1. Else pop the remaining operators to output 18
19. 19. Character Scanned 2 * 3 / ( 2 1 ) + 5 * ( 4 1 ) Stack Contents(Top on right) Empty * * / /( /( /(/(/ + + +* +*( +*( +*(+*(+* Empty Postfix Expression 2 2 23 23* 23* 23*2 23*2 23*21 23*2123*21-/ 23*21-/5 23*21-/5 23*21-/5 23*21-/54 23*21-/54 23*21-/541 23*21-/54123*21-/541-*+ 19
20. 20. Initialize an empty stack and scan the input string from right to left. Examine the next element in the input. If it is an operand, add it to output string, go to step 7. If it is a closing parenthesis, push it, go to step 7. If it is an operator, then a) If stack is empty, or if the top of stack is a closing parenthesis, push the operator. b) If the operator has same or higher priority than the top of stack, push the operator. Else pop the operator from the stack and add it to output string, go to step 5a. 6. If it is an opening parenthesis, pop operators from stack and add them to output string until a closing parenthesis is encountered. Pop and discard the closing parenthesis. 7. If there is more input, go to step 2. Else pop the remaining operators and add them to output string. 8. Reverse the output string. 1. 2. 3. 4. 5. 20
21. 21. Char Scanned ) 1 4 ( * 5 + ) 1 2 ( / 3 * 2 Stack Contents(Top on right) ) ) ))Empty * * + +) +) +)+)+ +/ +/ +/* +/* Empty Prefix Expression(right to left) 1 1 14 141414-5 14-5* 14-5* 14-5*1 14-5*1 14-5*12 14-5*1214-5*1214-5*12-3 14-5*12-3 14-5*12-32 14-5*12-32*/+ 21
22. 22.  Function Calls   Implementing Recursion    Stacks are used to pass parameters between functions. On call to a function, the parameters and local variables are stored on a stack. There are many problems whose algorithmic description is best described in a recursive manner. Many high-level programming languages provide support for recursion use stack. Checking the validity or correctness of mathematical expressions. Conversion and evaluation of mathematical expressions. 22
23. 23.  An ordered collection of items    deletion at one end (front) insertion at the other end (rear) Elements are processed in the same order as they join the queue  first-in, first-out (FIFO)  first-come, first-served (FCFS) 23
24. 24. X X X X X X X X Deletion Front Insertion Rear 24
25. 25. 1. [Check for Queue Overflow] If R N then Write ('OVERFLOW') Return 2. [Increment rear pointer] R R+1 3. [Insert element] Q(R) X 4. [Is front pointer properly set?] If F = 0 then F 1 5. [Finish] Return 25
26. 26. 1. 2. 3. 4. [Check for Queue Underflow] If F = 0 then Write ('UNDERFLOW') Return (0) (0 denotes an empty queue) [Delete element] Y Q(F) [Queue empty?] If F = R then F R 0 else F F+1 [Return element and Finish] Return (Y) 26
27. 27. Empty (i) F R A R Insert B F C (v) Delete A R (ii) B C Insert C R (iv) C Delete B F R (vi) C D F (vii) A F (iii) B Insert A F R B F A R Insert D C F (viii) D Insert E R (Overflow) 27
28. 28. R ... D F Q[n-2] C Q[n-1] Q[2] Q[n] Q[1] 28
29. 29. 1. 2. 3. 4. 5. [Reset rear pointer] If R = N then R 1 else R R+1 [Overflow?] If F = R then Write ('OVERFLOW') and Return [Insert element] Q[R] X [Is front pointer properly set?] If F = 0 then F 1 [Finish] Return 29
30. 30. This algorithm deletes and returns the last element of the queue. Y is a temporary variable. 1. [Underflow?] If F = 0 then Write('UNDERFLOW'), Return (0) 2. [Delete element] Y Q[F] 3. [Queue empty?] If F = R then F R 0 4.[Increment front pointer] If F = N then F 1 else F F + 1 5. [Return element and Finish] Return (Y)  30
31. 31.    A queue in which we are able to insert items or remove items from any position based on some property (such as priority of the task to be processed) is often referred to as a priority queue. Priorities of 1, 2, and 3 are attached to jobs of type real-time, on-line, and batch, respectively. If a job is initiated with priority i, it is inserted immediately at the end of the list of other jobs with priority i, for i = 1, 2, or 3. 31
32. 32.     Ascending / descending priority In an ascending priority queue, elements are processed in the increasing order of their priority values. In a descending priority queue, elements are processed in the decreasing order of their priority values. In both the types, elements with same priority are processed according to the order in which they are inserted. 32
33. 33.      In simulations and operating systems. Operating systems often maintain a queue of processes that are ready to execute or that are waiting for a particular event to occur. “holding area” for messages between two processes, two programs, or even two systems is (called a “buffer”) often implemented as a queue. A file/print server in a computer network handles file access request from many clients throughout the network. Servers have a limited capacity to service request from clients. When that capacity is exceeded, client requests wait in queues. 33
34. 34.     A homogeneous collection of elements, with a linear relationship between the elements. Lists can be unordered - their elements can be placed without a particular order, or ordered – their elements are placed in a particular order. Stacks and queues are lists whose elements are ordered based on the sequence in which they have been inserted. The lists can also be ordered by the value. 34
35. 35.       Ex. A list ordered alphabetically, and a list ordered numerically (in ascending or descending order). Value-ordered lists or sorted lists. On one of the fields in the record, the record key. Ex. A list of students in a class can be ordered alphabetically by name, or numerically by roll number. Key-ordered-lists. Lists can be implemented using Linear Arrays or Linked Lists 35
36. 36.     A linked list is a linear collection of similar elements, called nodes. The linear order is given by pointers. Each node is divided into two or more parts. A linked list can be one of the following types: Linear linked list, or singly linked list, or one-way list  Doubly linked list or two-way list  Circular linked list  36
37. 37. NextPointer info Structure of a node head 4148 4156 4164 4172 X A Linear Linked List with four nodes 37