DATA STRUCTURESTACKS AND QUEUESPrepared by : SELVIN JOSY BAI.S
STACK It is a linear data structure in which an elementsmay be inserted or deleted only at one end called thetop of the stack. This data structure implemented in LIFO (Last InFirst Out) manner LIFO means element last inserted would be the firstone to be deleted. The Stack is also a dynamic data structure as itcan grow or shrink.
Real life example : A stack OF DISCS IN HOLDER A stack of TOKENS IN HOLDER Two basic operations of stack PUSHING : An Insertion in a stack is calledPUSHING. POPPING : A Deletion from a stack is calledPOPPING.
PUSHING ALGORITHM1. Read ITEM2. If TOP = N then Print “OVERFLOW”and Stop3. Else set TOP = TOP + 1//Creating a space for a new element4. Set STACK [ TOP ] = ITEM//Inserting the new ITEM5. Stop1 2 3 4 5TOPN 5NULL
POPPING ALGORITHM1. If TOP = NULL thenPrint “UNDERFLOW” and return2. Else set ITEM =STACK [ TOP ]3. Set TOP = TOP - 14. Stop1 2 3 4 5TOP
APPLICATIONS OF STACK TO REVERSING A STRING TO EVALUATE NOTATIONALEXPRESSION(INFIX, PREFIX, & POSTFIX) TO CONVERT FROM ONE EXPRESSION TOANOTHER.
QUEUE It is a linear data structure in which an element maybe inserted only at one end, called the REAR end anddeletions can be take place at the other end, called theFRONT end. This data structure implemented in FIFO (First InFirst Out) manner FIFO means element first inserted would be the firstone to be deleted. The Queue is also a dynamic data structure as it cangrow or shrink. However, if additions and deletions are made at bothends of a queue, then such a queue is called DoubleEnded Queue or simply DEQUE.
The following operations can be performedon Queues1. Addition of an element at the REAR end of thequeue2. Deletion of an element from the FRONT end ofthe queueReal life example :• People waiting at a railway station counter fortaking tickets
Algorithm for INSERTION1. If FRONT = 1 and REAR = N orIf FRONT = REAR + 1 thenPrint “OVERFLOW” ; stop2. if FRONT = NULL thenset FRONT = 1 and REAR = 13. Else if REAR = N thenset REAR = 14. Else set REAR = REAR + 15. Endif6. Set QUEUE [ REAR ] = ITEM7. Stop
Algorithm for DELETION1. If FRONT = NULL thenPrint “UNDERFLOW” ; stop2. set ITEM = QUEUE [FRONT]3. If FRONT = REAR thenset FRONT = NULL and REAR = NULL4. Else if FRONT = N then set FRONT = 15. Else set FRONT = FRONT + 16. Stop
APPLICATIONS OF QUEUE IT OCCURS IN THE JOB SCHEDULING THE COMPUTER OS USE THIS CONCEPTIN SCHEDULING THE MEMORY,PROCESSOR AND THE FILES. USED FOR SCHEDULING JOBS ANDSERVICES.
EXERCISE - 1 In CD pack, the CDs are placed one over theother through a central axis. Assume that thereare 25 CDs in the pack.a. Name the data structure that resembles with theCD pack.b. Considering the CD pack as an array in C++, howwill you name the position of the last CD and whatwill be its value?c. Write an algorithm to remove a CD from the packand name the operation performed.
EXERCISE - 2 While waiting for a ticket at the railway stationticket counter, you are following the principleas that of a data structurea. Name the data structure and the principleb. Write a C++ function to add a new element in thisdata structure.c. Name the situation where there is no space foradding new element
Some more Questions1. What is overflow and underflow?2. Explain the terms PUSH and POP?3. What are the applications of stacks andqueues?4. Name the data structure that resembles aperson placing plates in a vessel. Writean algorithm for inserting a plate.