Upcoming SlideShare
×

# Stacks & Queues

4,441 views
4,307 views

Published on

Published in: Technology
2 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• good work keep it up

Are you sure you want to  Yes  No
Your message goes here
• good

Are you sure you want to  Yes  No
Your message goes here
• Fantastic work!!!
Will you please send it to me (~_~) my mail is
(bawaziranuha@hotmail.com)
It will be a great help form you .
Thanks....

Are you sure you want to  Yes  No
Your message goes here
• Great work friend..Will u send me this,please..I m a student of computer science..I need it.Thanx...

Are you sure you want to  Yes  No
Your message goes here
Views
Total views
4,441
On SlideShare
0
From Embeds
0
Number of Embeds
123
Actions
Shares
0
0
4
Likes
2
Embeds 0
No embeds

No notes for slide

### Stacks & Queues

1. 1. Let’s learn TACKS & UEUES for CLASS XII( C++) SENIOR SECONDARY GROUP Presented By : NITI ARORA
2. 2. STACKS AND QUEUES
3. 3. Objective <ul><li>Introduction to Data Structure </li></ul><ul><li>Array and Link list </li></ul><ul><li>Stack </li></ul><ul><li>Array implementation of Stack </li></ul><ul><li>Linked Implementation of Stack </li></ul><ul><li>Queue </li></ul><ul><li>Array implementation of Queue </li></ul><ul><li>Linked Implementation of Queue </li></ul>
4. 4. A mathematical and logical model of data is known as Data Structure .   Primitive data structure : The data structure, which is available in the compiler, is known as a primitive data structure . Non-primitive data structure : The data structure, which is not available in the compiler, is known as non-primitive data structure .   DATA STRUCTURE: INTRODUCTION
5. 5.   Linear Data Structure : The data structure in which each element has access to maximum of one predecessor element and maximum of one successor element is known as linear data structure . Example: Stack, Queue, etc . Non-linear Data Structure : The data structure in which each element can access any number of predecessor elements and any number of successor elements is known as Non-linear data structure . Example: Tree, Graphs, etc .   DATA STRUCTURE: INTRODUCTION
6. 6. Static Data Structure: The data structure in which the number of elements is fixed, is known as Static Data Structure . Example: Arrays Dynamic Data Structure: The data structure in which the number of elements is not fixed, is known as Dynamic Data Structure . Example: Linked List. TYPES OF DATA STRUCTURE
7. 7. It is a static data structure . It is a homogeneous collection of data. The elements in the array are stored on consecutive memory locations. Array is also known as a subscripted variable, e.g., A[i] is i th element of the array A. ARRAY
8. 8. STACK It is a non-primitive linear data structure in which insertion and deletion of elements takes place from only one end , known as top. It is a non-primitive linear data structure in which insertion and deletion of elements takes place from two opposite ends rear and front respectively. QUEUE
9. 9. STACKS Stacks is LIFO (Last In First Out) structure and physically can be implemented as an array or as a linked list. Stack, when implemented as an array is functionally same as any other array except that here, adding an element and deletion is done from the same direction just like a pile of books.
10. 10. STACK Inserting an element in an array is known as PUSH . Deleting an element from an array is known as POP . Implementation of STACK in computers <ul><li>When functions are called. </li></ul><ul><li>To convert a infix expression to postfix. </li></ul><ul><li>To evaluate a postfix expression. </li></ul>
11. 11. STACK A stack is a list in which insertion and deletion takes place only at one end called top. Thus, called LIFO. Representation of STACK Each one of the above has one open and one close end and data movement takes place from open end . data4 TOP data3 data2 data1 data2 data1 data3 data4 TOP TOP data4 data3 data2 data1 data1 data2 data3 data4 TOP
12. 12. Basic operation and implementation of stacks <ul><li>Creation of stack </li></ul><ul><li>Check for empty stack </li></ul><ul><li>Check for full stack </li></ul><ul><li>Add element in stack </li></ul><ul><li>Delete element in stack </li></ul><ul><li>Print stack </li></ul>
13. 13. STACKS The fundamental operations that can be performed on stack are PUSH and POP . When element is added on the stack top is called PUSH . And When Data is removed from the stack top, the operation is called POP.
14. 14. STACK The stack operation can be explained as follows: Stack operation Content of array Push(a) a Push(b) ba Push( c) cba Pop() ba Pop() a Push (a) a b a Push(b) b a C Push( c) b a Pop( c) a Pop(b)
15. 15. STACKS <ul><li>A stack is a list, any list implementation can be used to implement stack. </li></ul><ul><li>We can implement stack by the following data structures: </li></ul><ul><li>Array called Linear Stack </li></ul><ul><li>Linked List called Linked Stack </li></ul>
16. 16. Linear Stack int S[5]; When PUSH is selected, TOP is incremented, And data is added at that subscript location When POP is selected, TOP is decremented, And data is removed from that subscript location Stack array int TOP; To hold address of location where data is inserted or deleted
17. 17. Lets see working of Linear Stack Push 7 Push 20 Push 14 <ul><li>OVERFLOW </li></ul><ul><li>ARRAY IS FULL </li></ul>TOP TOP TOP Top is incremented TOP++ 10 9 8 10 9 8 7 10 9 8 7 20
18. 18. CONTINUED…. Pop 20 Pop 7 <ul><li>UNDERFLOW OCCURS WHEN STACK IS EMPTY </li></ul>TOP Top Top TOP is decremented TOP -- 10 9 8 7 20 10 9 8 7 10 9 8
19. 19. Lets see this using a program Program Code for the Same is Click here to execute program Click here to see program code
20. 20. A variable which holds an address of a memory location of another variable is known as a Pointer Variable (or only pointer). Example int amt, *p; amt Requires 2 bytes 0x8ffebab4 *P Requires 2 bytes Pointer P holds address of amt POINTER     900     0x8ffebab4
21. 21. NEW operator in C++ returns the address of a block of unallocated bytes (depending on data type a pointer pointing to). DELETE operator in C++ reverses the process of new operator, by releasing the memory location from a pointer. It de allocates memory assigned by NEW. DYNAMIC ALLOCATION
22. 22. A pointer, which stores the address of struct type data, is known as Pointer to structure. struct abc { int X,Y; }; struct *g=new abc; Holds address of dynamic object of struct abc   G   0x8ff134ab G->X 0x8ff134ab G->X G->Y LINK LIST To allocate dynamic allocation and store address in point g
23. 23. struct STACK // structure for stack { int data; STACK *link; }; struct *TOP; LINK STACK To hold address of First node of the list TOP pointer to holds address of dynamic objects of link stack. As we push a node TOP element get shifted and new node becomes first node. LIFO implementation every new node becomes first node. When we pop Top node is deleted and next node becomes first node.
24. 24. Lets see working of Linked stack * TOP * Temp NULL 0x8ffab2e6 A new memory is allocated and address is stored in temp X NULL data link 0x8ffab2e6 Top = Temp Top will hold address of new location * TOP 0x8ffab2e6 Thus, TOP will have this address. Push operation Initially top is assigned NULL Temp holds address of new location
25. 25. Cont….. *TOP * Temp 0x8ffab2e6 0x8ffab2e8 Another new memory is allocated to an object Y data link 0x8ffab2e8 * TOP 0x8ffab2e8 X NULL data link 0x8ffab2e6 temp-> link = Top Top=temp 0x8ffab2e6 Now TOP is TOP will get shifted Y becomes first node X becomes second node
26. 26. Cont….. * TOP * Temp 0x8ffab2e8 0x8ffab2e8 An object is deleted from top Y data link 0x8ffab2e8 Thus Top will be * TOP 0x8ffab2e6 X NULL data link Temp=TOP TOP=TOP->link 0x8ffab2e6 delete temp (to release memory) 0x8ffab2e6 TOP will get shifted X becomes first node Y will be released POP operation
27. 27. Lets see this using a program Program Code for the Same is Click here to execute program Click here to see program code
28. 28. Queues <ul><li>Queue is FIFO (First In First Out) structure and physically can be implemented as an array or as a linked list. </li></ul>Queue , when implemented as an array is functionally same as any other array except that here, adding an element and deletion is done from the one direction and deletion from other just like any queue of peoples.
29. 29. Queues <ul><li>Inserting an element in an array is known as insert. </li></ul><ul><li>Deleting an element from an array is known as delete </li></ul><ul><li>But this is done with the help of two parameters rear and front. </li></ul>Implementation of queue in computers <ul><li>When program is executed. </li></ul>
30. 30. Queue A Queue is a data structure in which insertion is done at the end and deletion is done from the front of queue. It is FIFO . Representation of Queue Each one of the above has two open end Front and Rear. Insertion is done from Rear and deletion form Front Rear Front Front Rear Front Rear Front Rear data4 data3 data2 data2 data3 data4 data4 data3 data2 data2 data3 data4
31. 31. Basic operation and implementation of QUEUE <ul><li>Creation of Queue </li></ul><ul><li>Check for empty Queue </li></ul><ul><li>Check for full Queue </li></ul><ul><li>Add element in Queue </li></ul><ul><li>Delete element in Queue </li></ul><ul><li>Print Queue </li></ul>
32. 32. QUEUE The fundamental operations that can be performed on Queue are Insert and Delete . When element is added on the Queue Front is called Insert . And When Data is removed from the Queue Rear, the operation is called Delete.
33. 33. QUEUE The Queue operation can be explained as follows: Queue operation Content of array Insert(a) Front=0 Rear=0 Insert(b) Front=0 Rear=1 Insert( c) Front=0 Rear=2 Delete() Front=1 Rear=2 Delete() Front=2 Rear=2 If we try to insert Overflow occurs Though first two cells are empty a b a c b a c b c
34. 34. Linear Queue int Q[5]; When INSERT is selected, Rear is incremented, And data is added at that subscript location When DELETE is selected, Front is decremented, And data is removed from that subscript location Queue array int Front, Rear; To hold address of location where data is inserted or deleted
35. 35. QUEUE <ul><li>A Queue is a list, any list implementation can be used to implement Queue. </li></ul><ul><li>We can implement Queue by the following data structures: </li></ul><ul><li>Array called Linear Queue </li></ul><ul><li>Linked List called Linked Queue </li></ul>
36. 36. Lets see working of LINEAR QUEUE Insert 7 Insert 20 Insert 14 OVERFLOW QUEUE is full Front rear rear rear Front Front Rear is incremented Rear++ 10 9 8 10 9 8 7 10 9 8 7 20
37. 37. Lets see working of Queue as an array Delete Delete Underflow occurs when QUEUE is empty Rear Front Rear Rear Front Front Front is incremented Front++ 10 9 8 7 20 9 8 7 20 8 7 20
38. 38. Lets see this using a program Program Code for the Same is Click here to execute program Click here to see program code
39. 39. struct QUEUE // structure for QUEU { int data; QUEUE *link; }; struct *Front,*Rear; LINKED QUEUE To hold address of First and Last node of the list Front and Rear pointer to holds address of dynamic objects of link stack. As we insert a node Rear element get shifted and new node becomes next node. FIFO implementation every new node added at end. When we Delete Front node is deleted and next node becomes first node.
40. 40. Lets see working of Linked Queue * Front * Rear * Temp NULL NULL 0x8ffab2e6 A new memory is allocated and address is stored in temp X NULL data link 0x8ffab2e6 Front=Rear = Temp Front and Rear will hold address of First location * Front * Rear 0x8ffab2e6 0x8ffab2e6 Thus, Front and Rear will have this address . Insert operation Initially Front and Rear is assigned NULL Temp holds address of new location
41. 41. Cont….. *Front * Rear * Temp 0x8ffab2e6 0x8ffab2e6 0x8ffab2e8 Another new memory is allocated to an object X data link 0x8ffab2e6 * Rear 0x8ffab2e8 Y NULL data link 0x8ffab2e8 temp-> link = Rear Rear=temp 0x8ffab2e8 Now Rear is Rear will get shifted Y becomes Last node
42. 42. Cont….. * Front * Rear * Temp 0x8ffab2e6 0x8ffab2e8 0x8ffab2e6 An object is deleted from Front X data link 0x8ffab2e6 Thus Front will be * Front 0x8ffab2e8 Y NULL data link Temp=Front Front=Front->link 0x8ffab2e8 delete temp (to release memory) 0x8ffab2e8 Front will get shifted Y becomes first node X will be released Delete operation
43. 43. Lets see this using a program Program Code for the Same is Click here to execute program Click here to see program code
44. 44. CIRCULAR QUEUE The fundamental operations that can be performed on Circular Queue are Insert and Delete . When overflow occurs though the free cells are available, Rear reaches ends Circular Queue is implemented to avoid this drawback. In Circular Queue as soon as Rear reaches maximum it should reset to 0.
45. 45. QUEUE The Queue operation can be explained as follows: Queue operation Content of array Insert(a) Front=0 Rear=0 Insert(b) Front=0 Rear=1 Insert( c) Front=0 Rear=2 Delete() Front=1 Rear=2 Insert (d) Front=2 Rear=0 Overflow occurs only when Array is FULL. Rear moves to 0 if array is empty a b a c b a c b c d
46. 46. Lets see this using a program Program Code for the Same is Click here to execute program Click here to see program code
47. 47. Do you have any QUESTIONS ?
48. 48. TEST YOUR KNOWLEDGE <ul><li>What is difference between Stack & Queue? </li></ul><ul><li>What is Dynamic Allocation? </li></ul><ul><li>What is the significance of Top? </li></ul><ul><li>What is the significance of Front & Rear? </li></ul><ul><li>What is Overflow? </li></ul><ul><li>What is Underflow? </li></ul><ul><li>Where Stack is Implemented? </li></ul>
49. 49. THANKS NITI ARORA FROM