stack

4

Click here to load reader

Upload: swarup-boro

Post on 25-May-2015

138 views

Category:

Education


5 download

DESCRIPTION

This for the students of computer science and for those who want to learn about the concepts

TRANSCRIPT

Page 1: Stack

Page 1 of 4

Prepared By Sumit Kumar Gupta, PGT Computer Science

STACK ( Array & Linked Implementation)

STACK : In computer science, a stack is an abstract data type and data structure based on the principle of Last In First Out (LIFO). Stacks are used extensively at every level of a modern computer system. For example, a modern PC uses stacks at the architecture level, which are used in the basic design of an operating system for interrupt handling and operating system function calls. Among other uses, stacks are used to run a Java Virtual Machine, and the Java language itself has a class called "Stack", which can be used by the programmer. The stack is ubiquitous.

A stack-based computer system is one that stores temporary information primarily in stacks, rather than hardware 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 as a linked list called dynamic data structure. A stack implemented as an array inherits all the properties of an array and if implemented as a linked list , all characteristics of a linked list are possessed by it. But whatever way a stack may be implemented , insertions and deletions occur at the top only. An insertion in a stack is called 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 {

Page 2: Stack

Page 2 of 4

Prepared By Sumit Kumar Gupta, PGT Computer Science

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 - - ;

Page 3: Stack

Page 3 of 4

Prepared By Sumit Kumar Gupta, PGT Computer Science

} } 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 is difficult for the machine to know and keep track of precedence of operators. A postfix expression itself takes care of the precedence of operators as the placement of operators. Thus, for the machine , it is easier to carry out a postfix expression than an infix expression. 13. Given the following class:

char *msg [ ] = {“overflow”, “underflow” } ; class Stack { int top , stk [ 5 ] ;

Page 4: Stack

Page 4 of 4

Prepared By Sumit Kumar Gupta, PGT Computer Science

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 AND

15. 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 ) * 2

16. 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