Your SlideShare is downloading. ×
Stack
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Stack

619
views

Published on

Published in: Education

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
619
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Page 1 of 4 STACK ( Array & Linked Implementation)STACK : In computer science, a stack is an abstract data type and data structure based on the principle of LastIn First Out (LIFO). Stacks are used extensively at every level of a modern computer system. For example, amodern PC uses stacks at the architecture level, which are used in the basic design of an operating system forinterrupt handling and operating system function calls. Among other uses, stacks are used to run a Java VirtualMachine, and the Java language itself has a class called "Stack", which can be used by the programmer. Thestack is ubiquitous.A stack-based computer system is one that stores temporary information primarily in stacks, rather thanhardware CPU registers (a register-based computer system).A Stack ias a LIFO structure and physically it can be implemented as an array called static data structure or asa linked list called dynamic data structure. A stack implemented as an array inherits all the properties of anarray and if implemented as a linked list , all characteristics of a linked list are possessed by it. But whateverway a stack may be implemented , insertions and deletions occur at the top only. An insertion in a stack iscalled pushing and a deletion from a stack is called popping.Very short answer type questions. 1. What is the full form of LIFO ? What is a LIFO list technically called? Ans:- Last In First Out . Technically it is called STACK. 2. What is POP & PUSH operation in STACK? Ans: - POP operation means Deletion of an element from the Stack and PUSH means Insert an element to the Stack. 3. What is the situation called when an insertion and deletion takes place in a full list and from a blank list respectively? Ans:- Overflow and Underflow. 4. Explain INFIX, POSTFIX, PREFIX notations. Ans:- A + B  INFIX AB+  POSTFIX +AB  PREFIX 5. When elements os stack are joined using pointers, how is the stack termed as? Ans:- Linked Stack 6. Suppose STACK is allocated 6 memory locations and initially STACK is empty ( TOP = 0). Give the output of the program segment: AAA = 2; BBB = 5 ; PUSH(STACK,AAA); PUSH( STACK, 4 ); PUSH(STACK, BBB+ 2 ); PUSH ( STACK, AAA + BBB ); While TOP > 0 { POP ( STACK, ITEM ); TOP - - ; cout< item <”n”; }Long Answer Type Questions:- 7. Write a function in C++ to perform a PUSH and POP operation in a STACK as an array. Ans:- void PUSH ( int Stack [], int &Top, int Val ) { if ( Top = = size -1 ) { cout << “ Stack Overflow ” ; exit(0) ; } else { Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 2. Page 2 of 4 Top + + ; Stack [ Top ] = Val ; } } void POP ( int Stack [ ] , int & Top ) { int R ; if ( Top = = -1 ) { cout<< “ Stack Underflow” ; exit(0) ; } else { R = Stack [ Top ] ; Top - - ; } }8. Complete the class with all function definition. class Stack { int Data [ 10] ; int Top ; public: Stack ( ) { Top = -1; } void PUSH ( ) ; // to push an element into the stack . void POP ( ) ; // to pop an element from the stack. }; Ans:- void Stack :: PUSH ( ) { int Val ; cout <<”Enter the value to be push.” ; cin>> Val ; if ( Top = = 9 ) { cout << “ Stack Overflow”; exit ( 0) ; } else { Top + + ; Data [ Top ] = Val ; } } void Stack :: POP( ) { int R ; if ( Top = = -1 ) { cout<<”Stack Underflow”; exit (0) ; } else { R = Data [Top ] ; Top - - ; Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 3. Page 3 of 4 } } 9. Define functions Stackpush( ) to insert nodes and Stackpop ( ) to delete nodes for a linked implemented stack having the following structure for each node. struct Node { char name [ 20 ] ; int age ; Node * Link ; }; class Stack { Node * Top ; public : Stack ( ) { Top = NULL ; } void Stackpush ( ) ; void Stackpop ( ) ; }; void Stack :: Stackpush ( ) { Node * Ptr ; Ptr = new Node ; cin >>Ptr  name ; cin >> Ptr  age ; Ptr  Link = NULL ; if ( Top = = NULL ) Top = Ptr ; else { Ptr  Link = Top ; Top = Ptr ; } } void Stack :: Stackpop ( ) { Node * Ptr ; if ( Top = = NULL ) cout <<”Stack Underflow” ; else { Ptr = Top ; Top = Top  Link ; delete Ptr ; } } 10. Write a function in C++ to perform the PUSH and POP operation on a dynamically allocated stack containing real numbers. ( Hint : Same as Question No. 9 ) 11. Describe the similarities and differences between queues and stacks. Ans: - Similarities : i) Both queues and stacks are special cases of linear lists. ii) Both can be implemented as arrays or linked lists. Differences : i)A Stack is a LIFO list, a Queue is a FIFO list. ii) There are no variations of stack, a queue, however, may be circular or dequeue.12. What are the advantages of using postfix notation over infix notation?Ans:- An infix expression is evaluated keeping in mind the precedence of operators. An infix expression isdifficult for the machine to know and keep track of precedence of operators. A postfix expression itself takescare of the precedence of operators as the placement of operators. Thus, for the machine , it is easier to carry outa postfix expression than an infix expression.13. Given the following class: char *msg [ ] = {“overflow”, “underflow” } ; class Stack { int top , stk [ 5 ] ; Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 4. Page 4 of 4 void err_rep ( int e_num ) { cout<< msg [e_num] ; } public: void init ( ) { top = 0 ; } void push ( int) ; void pop ( ) ; }; Define push and pop outside the stack. In the definition take care of overflow condition in push and underflow condition in pop.Ans:- void Stack :: push ( int a ) { if ( top > 4 ) err_rep (0) ; else stk [ top + + ] = a ; } void Stack :: pop ( ) { if top = = 0 ) err_rep ( 1 ) ; else { cout<<”top element is “<<stk [top] ; top - - } }14. Translate , following infix expression into its equivalent postfix expression and show the stack status : i) ((A – B ) * ( D / E )) / ( F * G * H ) Ans: AB - DE / * FG * H * / ii) ( A + B ^ D ) / ( E – F ) + G Ans: ABD ^ + E F - / G + iii) A * ( B + D ) / E – F – ( G + H / K ) Ans: A B D + * E / F – G H K / + - iv) A * ( B + ( C + D ) * ( E + F ) / G ) * H Ans: A B C D + E F + * G / + * H * v) A + ( ( B + C ) + ( D + E ) * F ) / G Ans: A B C + D E + F * + G / + vi) NOT A OR NOT B AND NOT C Ans: A NOT B NOT C NOT AND OR vii) NOT ( A OR B ) AND C Ans: A B OR NOT C AND15. Write the equivalent infix expression for the following postfix expression. i) 10 , 3 , * , 7 , 1 , - , * , 23 , + Ans: 10 * 3 * ( 7 – 1 ) + 23 ii) 12, 7, 3, - , / , 2 , 1 , 5 + , * , + Ans: 12 / ( 7 – 3 ) + ( 1 + 5 ) * 216. Evaluate the following infix expression and show the contents of stack after each operation. i) 5, 3, + , 2 , * , 6 , 9 , 7 , - , / , - Ans : 13 ii) 3, 5, + , 6 , 4 , - , * , 4 , 1 , - , 2 , ^ , + Ans : 25 iii) 3, 1, + , 2, ^ 7 , 4, - , 2 , * , + , 5 , - Ans : 17 iv) 20, 45, + 20 , 10 , - , 15 , + , * Ans : 1625 Prepared By Sumit Kumar Gupta, PGT Computer Science