Upcoming SlideShare
×

# Data structure stack&queue basics

3,349 views

Published on

It is for beginners.

Published in: Education, Technology
9 Likes
Statistics
Notes
• Full Name
Comment goes here.

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

Views
Total views
3,349
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
272
0
Likes
9
Embeds 0
No embeds

No notes for slide

### Data structure stack&queue basics

1. 1. DATA STRUCTURESTACKS AND QUEUESPrepared by : SELVIN JOSY BAI.S
2. 2. 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.
3. 3.  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.
4. 4. 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
5. 5. POPPING ALGORITHM1. If TOP = NULL thenPrint “UNDERFLOW” and return2. Else set ITEM =STACK [ TOP ]3. Set TOP = TOP - 14. Stop1 2 3 4 5TOP
6. 6. APPLICATIONS OF STACK TO REVERSING A STRING TO EVALUATE NOTATIONALEXPRESSION(INFIX, PREFIX, & POSTFIX) TO CONVERT FROM ONE EXPRESSION TOANOTHER.
7. 7. 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.
8. 8. 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
9. 9. 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
10. 10. 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
11. 11. 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.
12. 12. 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.
13. 13. 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
14. 14. 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.