Your SlideShare is downloading.
×

×
Saving this for later?
Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.

Text the download link to your phone

Standard text messaging rates apply

Like this presentation? Why not share!

- Timing and-control-unit by Anuj Modi 12335 views
- Types of instructions by ihsanjamil 24985 views
- Computer instructions by Anuj Modi 4883 views
- Stacks and queues by Trupti Agrawal 151 views
- Stacks by Sujeet Singh 226 views
- Ds 3 by Niit Care 686 views
- VTU 3RD SEM CSE DATA STRUCTURES WIT... by vtunotesbysree 727 views
- 03 stacks and_queues_using_arrays by tameemyousaf 4383 views
- Chapter03 by 春男 洪 638 views
- Presentation on Elementary data str... by Kuber Chandra 469 views
- ELEMENTARY DATASTRUCTURES by Malikireddy Bramh... 138 views
- Lecture 2d queues by Victor Palmar 216 views

Like this? Share it with your network
Share

699

Published on

U3.stack queue

U3.stack queue

No Downloads

Total Views

699

On Slideshare

0

From Embeds

0

Number of Embeds

0

Shares

0

Downloads

70

Comments

0

Likes

2

No embeds

No notes for slide

- 1. Stacks
- 2. What is a stack? Stores a set of elements in a particular order Stack principle: LAST IN FIRST OUT = LIFO It means: the last element inserted is the first one to be removed Example Which is the first element to pick up?
- 3. Last In First Out top top E D D D C top C C C B top B B B B A A A A A A top
- 4. Stack Applications Real life Pile of books, files, plates TOH More applications related to computer science stack Program execution Evaluating expressions Palindrome finder Parentheses matcher A Palindrome is a string that reads the same in either direction Examples: “Able was I ere I saw Elba”
- 5. Stackobjects: a finite ordered list with zero or more elements.methods: Stack createS(max_stack_size) ::= create an empty stack whose maximum size is max_stack_size Boolean isFull(stack, max_stack_size) ::= if (number of elements in stack == max_stack_size) return TRUE else return FALSE Stack push(stack, item) ::= if (IsFull(stack)) stack_full else insert item into top of stack and return
- 6. Stack (cont’d)Boolean isEmpty(stack) ::= if(stack == CreateS(max_stack_size)) return TRUE else return FALSEElement pop(stack) ::= if(IsEmpty(stack)) return else remove and return the item on thetop of the stack.
- 7. Array-based Stack Implementation Allocate an array of some size (pre-defined) Maximum N elements in stack Bottom stack element stored at element 0 last index in the array is the top Increment top when one element is pushed, decrement after pop
- 8. Stack Implementation #include <stdio.h> #include<conio.h> # define MAXSIZE 200 int stack[MAXSIZE]; int top; //index pointing to the top of stack void main() { void push(int); int pop(); int will=1,i,num; clrscr(); while(will ==1) { printf(" MAIN MENU:n 1.Add element to stackn2.Delete element from the stack"); scanf("%d",&will); 8 Chapter 5: Stacks
- 9. switch(will) { case 1: printf("Enter the data... "); scanf("%d",&num); push(num); break; case 2: i=pop(); printf("Value returned from pop function is %d ",i); break; default: printf("Invalid Choice . "); } printf(" Do you want to do more operations on Stack ( 1 for yes, any other key to exit) "); scanf("%d" , &will); } //end of outer while } //end of main 9 Chapter 5: Stacks
- 10. void push(int y) { if(top>MAXSIZE) { printf("nSTACK FULL"); return; } else { top++; stack[top]=y; } } 10 Chapter 5: Stacks
- 11. int pop() { int a; if(top<=0) { printf("STACK EMPTY "); return 0; } else { a=stack[top]; top--; } return(a); } 11 Chapter 5: Stacks
- 12. The Towers of HanoiA Stack-based Application GIVEN: three poles a set of discs on the first pole, discs of different sizes, the smallest discs at the top GOAL: move all the discs from the left pole to the right one. CONDITIONS: only one disc may be moved at a time. A disc can be placed either on an empty pole or on top of a larger disc.
- 13. Towers of Hanoi
- 14. Towers of Hanoi
- 15. Towers of Hanoi
- 16. Towers of Hanoi
- 17. Towers of Hanoi
- 18. Towers of Hanoi
- 19. Towers of Hanoi
- 20. Towers of Hanoi
- 21. Polish(prefix) notation - * / 15 - 7 + 1 1 3 + 2 + 1 1 = - * / 15 - 7 2 3 + 2 + 1 1 = - * / 15 5 3 + 2 + 1 1 =-*33+2+11 =-9+2+11 =-9+22 =-94 =5 An equivalent in-fix is as follows: ((15 / (7 - (1 + 1))) * 3) - (2 + (1 + 1)) = 5 21 Chapter 5: Stacks
- 22. STACK OPERATIONSREVERSE POLISH NOTATION (postfix) Reverse polish notation :is a postfix notation (places operators after operands) (Example) Infix notation A+B Reverse Polish notation AB+ also called postfix.
- 23. STACK OPERATIONSREVERSE POLISH NOTATION (postfix) A stack organization is very effective for evaluating arithmetic expressions A*B+C*D (AB *)+(CD *) AB * CD * + (3*4)+(5*6) 34 * 56 * +
- 24. STACK OPERATIONSREVERSE POLISH NOTATION (postfix)n • Evaluation procedure:n 1. Scan the expression from left to right. 2. When an operator is reached, perform the operation with the two operands found on the left side of the operator. 3. Replace the two operands and the operator by the result obtained from the operation.n (Example) infix 3 * 4 + 5 * 6 = 42 postfix 3 4 * 5 6 * +n 12 5 6 * + 12 30 + 42
- 25. STACK OPERATIONSREVERSE POLISH NOTATION (postfix) • Reverse Polish notation evaluation with a stack. Stack is the most efficient way for evaluating arithmetic expressions. stack evaluation: Get value If value is data: push data Else if value is operation: pop, pop evaluate and push.
- 26. STACK OPERATIONSREVERSE POLISH NOTATION (postfix) (Example) using stacks to do this. 3 * 4 + 5 * 6 = 42=> 3 4 * 5 6 * +
- 27. Queue27
- 28. The Queue Operations A queue is like a line of people waiting for a bank teller. The queue has a front and a rear. $ $ Front Rear
- 29. The Queue Operations New people must enter the queue at the rear. The C++ queue class calls this a push, although it is usually called an enqueue operation. $ $ Front Rear
- 30. The Queue Operations When an item is taken from the queue, it always comes from the front. The C++ queue calls this a pop, although it is usually called a dequeue operation. $ $ Front Rear
- 31. The Queue Class The C++ template <class Item> standard class queue<Item> template library { has a queue public: template class. queue( ); void push(const Item& The template entry); parameter is the void pop( ); type of the items bool empty( ) const; that can be put in Item front( ) const; the queue. …
- 32. Array Implementation A queue can be implemented with an array, as shown here. For example, this queue contains the integers 4 (at the front), 8 and 6 (at the rear). [0] [1] [2] [3] [4] [5] ... 4 8 6An array ofintegers toimplement a We dont care whats inqueue of integers this part of the array.
- 33. Array Implementation The easiest implementation also 3 size keeps track of the number of items in the queue and the index of the first first 0 element (at the front of the queue), the last element (at the rear). 2 last [0] [1] [2] [3] [4] [5] ... 4 8 6
- 34. A Dequeue Operation When an element leaves the queue, 2 size size is decremented, and first changes, too. first 1 2 last [0] [1] [2] [3] [4] [5] ... 4 8 6
- 35. An Enqueue Operation When an element enters the queue, 3 size size is incremented, and last changes, too. first 1 3 last [0] [1] [2] [3] [4] [5] ... 8 6 2
- 36. At the End of the Array There is special behavior at the end 3 size of the array. For example, suppose we want to add a new element to this first 3 queue, where the last index is [5]: 5 last [0] [1] [2] [3] [4] [5] 2 6 1
- 37. At the End of the Array The new element goes at the front of 4 size the array (if that spot isn’t already used): first 3 0 last [0] [1] [2] [3] [4] [5] 4 2 6 1
- 38. Array Implementation Easy to implement 3 size But it has a limited capacity with a fixed array Or you must use a dynamic array for an 0 first unbounded capacity Special behavior is needed when the rear 2 last reaches the end of the array. [0] [1] [2] [3] [4] [5] ... 4 8 6
- 39. Linked List Implementation A queue can also be implemented with a linked list with both a head and a tail 13 pointer. 15 10 7 null head_ptr tail_ptr
- 40. Linked List Implementation Which end do you think is the front of the queue? Why? 13 15 10 7 null head_ptr tail_ptr
- 41. Linked List Implementation The head_ptr points to the front of the list. Because it is harder to remove Front 13 items from the tail of the list. 15 10 7 null head_ptr tail_ptr Rear
- 42. A priority queue is a container in which access or deletion is of the highest- priority item, according to some way of Assigning priorities to items.42 Priority Queues
- 43. FOR EXAMPLE, SUPPOSE A HOSPITALEMERGENCY ROOM HAS THEFOLLOWING FOUR PATIENTS, WITHNAME, PRIORITY, AND INJURY:43 Priority Queues
- 44. Matt 20 sprained ankleAndrew 45 broken legSamira 20 high blood pressureKerem 83 heart attackIN WHAT ORDER SHOULD THEPATIENTS BE TREATED? 44 Priority Queues
- 45. THERE ARE MANY APPLICATIONSOF PRIORITY QUEUES, IN AREAS ASDIVERSE AS SCHEDULING, DATACOMPRESSION, AND JPEG (JointPhotographic Experts Group) ENCODING.45 Priority Queues
- 46. Using Priority Queue toTrack Your Assignments Organize class or work assignments by due dates Early due date, higher priority diagram of class Assignment

Be the first to comment