Upload
gretel
View
26
Download
0
Embed Size (px)
DESCRIPTION
Data Structure Lecture-2. Prepared by: Shipra Shukla Assistant Professor Kaziranga University. Stack. A Stack is a linear data structure in which a data item is inserted and deleted at one record. - PowerPoint PPT Presentation
Citation preview
Data StructureLecture-2
Prepared by:Shipra Shukla
Assistant ProfessorKaziranga University
Stack
• A Stack is a linear data structure in which a data item is inserted and deleted at one record.
• A stack is called a Last In First Out (LIFO) structure because the data item inserted last is the data item deleted first from the stack.
Cont..
• Stacks are used most used is system software such as compilers, operating systems, etc. In fact, many compilers store the local variables inside a function on the stack.
• Stack is a specialized data storage structure (Abstract data type).
Functions in Stack
1. createStack function– This function takes the maximum number of elements (maxElements) the stack can hold as an argument, creates a stack according to it and returns a pointer to the stack. It initializes Stack S
2. push function - This function takes the pointer to the top of the stack S and the item (element) to be inserted as arguments. Check for the emptiness of stack
3. pop function - This function takes the pointer to the top of the stack S as an argument.
4. top function – This function takes the pointer to the top of the stack S as an argument and returns the topmost element of the stack S.
Example:
Create the stack S using createStack function, where S is the pointer to the structure Stack. Themaximum number of elements (maxElements) = 5.
Initially S->size = 0 and S->capacity = 5.
1. push(S,7): Since, S->size = S->capacity push 7 on the top of it and increase its size byone. Now, size = 1.7
2. push(S,5): Since, S->size = S->capacity push 5 on the top of it and increase its size byone. Now, size = 2.
57
3. push(S,21): Since, S->size = S->capacity push 21 on the top of it and increase its sizeby one. Now, size = 3.
2157
4. push(S,-1): Since, S->size = S->capacity push -1 on the top of it and increase its sizeby one. Now, size = 4.
-12157
5. top(S): Since, S->size(=4) is not equal to zero. It returns the topmost element i.e. -1.Output = ‘-1’.
6. pop(S): Since, S->size(=4) is not equal to zero. It removes the topmost element i.e. -1by simply reducing size of the stack S by 1. Now, size = 3.
2157
7. top(S): Since, S->size(=3) is not equal to zero. It returns the topmost element i.e. 21.Output = ‘21’.
8. pop(S): Since, S->size(=3) is not equal to zero. It removes the topmost element i.e. 21by simply reducing size of the stack S by 1. Now, size = 2.
57
9. pop(S): Since, S->size(=2) is not equal to zero. It removes the topmost element i.e. 5by simply reducing size of the stack S by 1. Now, size = 1.
7
10. pop(S): Since, S->size(=1) is not equal to zero. It removes the topmost element i.e. 7
by simply reducing size of the stack S by 1. Now, size = 0.
11. pop(S): Since, S->size(=0) is equal to zero. Output is ‘Stack is Empty’.
AlGORITHM OF PUSH
Push(stack[maxsize],item)Let stack [maxsize] is an array for implementing the stack1.if Top=maxsize-1 then print overflow and exit2.Set Top=Top+1 [increase by 1]3.Set Stack[Top]=item4.Exit
C Functionvoid push(){ int item;If (top== maxsize-1){ printf(“\n stack is full”); getch(); exit(0);}else{ printf(“\nEnter the element”); scanf(“%d”,&item); top=top+1; stack[top]=item;}}
AlGORITHM OF POPPOP(stack[maxsize],item)Let stack[maxsize] is an array for implementing the stack1.[CHECK FOR THE STACK UNDERFLOW]
If top<0 then print stack underflow and exit.
else [remove the top element]Set item=stack[top]
2.Decrement the stack topSet top=top-1
3.Return the deleted item from the stack
4.exit
C Functionint pop(){ int item;If (top== -1){ printf(“\n stack is empty”); getch(); exit(0);}else{ Item=stack[top];top=top-1;} printf(“\nThe element which has been deleted is:”);return(item);}
Polish Notations
• The process of writing the operators of an expression either before their operands or after them is called Polish notation.
• The notation was introduced by "Jan Lukasiewicz ".
• it assumes that an arithmetic operations can be take place between two operands only. For example, A+B, C*D, D/A etc.
• The computer system can understand and work only on binary paradigm
• The notation refers to these complex arithmetic expressions in three forms
• – If the operator symbols are placed between its operands, then the expression is in infix notation. Ex: A+B
•– If the operator symbols are placed before its operands, then the expression is in prefix notation. Ex: +AB
–If the operator symbols are placed after its operands, then the expression is in postfix notation Ex: AB+
Levels of precedence
• The binary operations in Q may have different levels of precedence.
• Highest : Exponentiation ( ↑ )
• Next highest : Multiplication ( * ) and division ( / )
• Lowest : Addition ( + ) and subtraction ( - )
Application of Stacks:Postfix Expression
Evaluation of a Postfix Expression AlgorithmThis algorithm finds the VALUE of an arithmetic expression P written in postfix notation.1. Add a right parenthesis ")"at the end of P. [This acts as a sentinel].2. Scan P from left to right and repeat Steps 3 and 4 for each element of until the sentinel ")" is encountered.3. If an operand is encountered, put it on STACK.4. If an operator (x) is encountered, then:a) Remove the two top elements of STACK, where A is the top element and B is the next-to-top element.b) Evaluate B (x) A.c) Place the result of (b) back on STACK [End of If structure.] [End of Step 2 loop.]5. Set VALUE equal to the top element on STACK.6. Exit.
Application of Stacks:Postfix Expression
Stack after pushing 6
Stack after pushing 3
Stack after retrieving the top two elements and popping twice
Stack after pushing the result of op1 + op2, which is 9
Application of Stacks:Postfix Expression
Stack after pushing 2
Stack after retrieving the top two elements and popping twice
Stack after pushing the result of op1 * op2, which is 18
Stack after popping the element
Example
• Consider the following arithmetic expression P written in postfix notation:
• P: 5, 6, 2, +, *, 12, 4, /, -
• We evaluate P using algorithm. First we add a sentinel right parenthesis at the end of P to obtain
• P: 5, 6, 2, +, *, 12, 4, /, -, ) (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)
Questions• 1. Stacks are sometimes called FIFO lists
True/False
• 2. Stack allows Push and Pop from both ends True/False
• 3. TOS (Top of the Stack) gives the bottom most element in the stack. True/False
Symbol Scanned STACK
(1) 5(2) 6(3) 2(4) +(5) *(6) 12(7) 4(8) /(9) -(10) )
55, 65, 6, 25, 84040, 1240, 12, 440, 337