Stack Queue Stack is an abstract data type

  • Slides: 26
Download presentation
Stack & Queue Stack is an abstract data type and data structure based on

Stack & Queue Stack is an abstract data type and data structure based on the principle of Last In First Out(LIFO). Queue is an ordered collection of items which is based on the principle of First In First Out(FIFO). Course Name: Networking Author(s) : Phani Swathi Chitta Mentor: Aruna Adil Level(UG/PG): UG *The contents in this ppt are licensed under Creative Commons Attribution-Non. Commercial-Share. Alike 2. 5 India license

Learning Objectives After interacting with this Learning Object, the learner will be able to:

Learning Objectives After interacting with this Learning Object, the learner will be able to: • Explain the working operations of Stack and Queue

Definitions of the components/Keywords: 1 2 3 4 5 Stack: • A stack is

Definitions of the components/Keywords: 1 2 3 4 5 Stack: • A stack is an ordered list in which all insertions and deletions are made at one end, called the top. • A stack can have any abstract data type as an element, but is characterized by two fundamental operations, called push and pop. • The push operation adds a new item to the top of the stack, or initializes the stack if it is empty. If the stack is full and does not contain enough space to accept the given item, the stack is then considered to be in an overflow state. • The pop operation removes an item from the top of the stack. A pop either reveals previously concealed items, or results in an empty stack, but if the stack is empty then it goes into underflow state (It means no items are present in stack to be removed). • A stack pointer is the register which holds the value of the stack. The stack pointer always points the tops value of the stack. • A stack is a restricted data structure, because only a small number of operations are performed on it. • Elements are removed from the stack in the reverse order to the order of their addition.

Definitions of the components/Keywords: 1 2 Stack example: Plates kept over each other (Stack

Definitions of the components/Keywords: 1 2 Stack example: Plates kept over each other (Stack of plates). Stack Applications: 1. Matching Balancing Parentheses 2. Evaluating a postfix expression 3. Depth first search of graphs Queue: 3 4 • A queue is an ordered list in which all insertions take place at one end, the rear, while all deletions take place at the other end, the front. • The elements are removed in the order of their insertion • Head pointer always points to the front of the queue, from where elements can be removed • Tail pointer always points to the end of the queue, where a new element can be inserted Queue Examples: 1. People standing in a queue for reservation 2. Operating systems maintain a queue of processes that are ready to execute 5

Definitions of the components/Keywords: 1 2 3 4 5 Glossary: • Data Structure –

Definitions of the components/Keywords: 1 2 3 4 5 Glossary: • Data Structure – A data structure is a specialized format for organizing and storing data. General data structure types include the array, the file, the record, the table, the tree and so on. In programming, a data structure may be selected or designed to store data for the purpose of working on it with various algorithms. • Stack – A stack is an ordered set in which all insertions and deletions are made at one end called stack pointer S. • Abstraction – Abstraction is the process of taking away or removing characteristics from something in order to reduce it to a set of essential characteristics. • Object - An object is a software bundle of variables and related methods • Element – It defines the data object or item that is inserted at the top of the stack or head of queue. • List – List is an ordered group of similar types of objects or items. • Ordered List – List is a collection of similar types of objects or items that is having a specific numerical order or ranking. • Stack Pointer S – It always points to the top element of the stack that can be removed. • Push – It inserts the given item at the top of the stack. • Pop – It removes the top most element from the stack.

Definitions of the components/Keywords: 1 2 3 4 5 Glossary: • Pointer Value –

Definitions of the components/Keywords: 1 2 3 4 5 Glossary: • Pointer Value – It displays the value of the pointer S of the stack. • Stack Overflow – It defines the error, when tried to push into a fully filled stack. • Stack Underflow – It defines error, when tried to pop from an empty stack. • Queue – A queue is an ordered set in which all insertions take place at one end, the Tail, while all deletions take place at the other end, the Head. • Tail – It always points to the value of the pointer Tail (T) of the queue, where a new element can be inserted. • Head - It always points to the value of the pointer Head (H) of the queue, from where elements can be removed. • Pointers Value – It displays the value for the pointer Head (H) and Tail (T) of the queue. • Enqueue – It inserts the given item at the location pointed by pointer Tail (T). • Dequeue – It removes the element which is pointed by pointer Head (H). • Queue Overflow – It defines the error, when tried to push into a fully filled queue. • Queue Underflow – It defines error, when tried to pop from an empty queue.

1 Master Layout 1 Stack Queue 2 Fig. A Fig. B 3 4 5

1 Master Layout 1 Stack Queue 2 Fig. A Fig. B 3 4 5 • Give two buttons STACK and QUEUE to select the operation • Give PAUSE and RESTART buttons • Give dropdown box to select the size from 1 - 10 and the default size is 5 • Give a box to enter the value of the item

1 Step 1: Stack 2 3 4 Instruction for the animator • When the

1 Step 1: Stack 2 3 4 Instruction for the animator • When the user selects Stack option in the main page, show fig. A in the master layout • Also give the below options in the right side of fig. A PUSH, POP and POINTER VALUE • When the user enters a value and clicks PUSH then that value should appear inside the stack as shown in the figure above • Then the pointer should move upwards such that it points to the new item in the stack 5 • The text in DT should appear in parallel to the figure Text to be displayed in the working area (DT) • The value pushed into the stack is ______ (give the value corresponding to the value entered by the user).

1 Step 2: 2 3 4 Instruction for the animator • 3 rd and

1 Step 2: 2 3 4 Instruction for the animator • 3 rd and 4 th points in the previous slide will be repeated depending on the size of the stack. • 5 Text to be displayed in the working area (DT) • The value pushed into the stack is ______

1 Step 3: “ STACK IS IN OVERFLOW STATE BECAUSE IT IS FULL” 2

1 Step 3: “ STACK IS IN OVERFLOW STATE BECAUSE IT IS FULL” 2 3 4 5 Instruction for the animator • When the number of elements in the stack equals the stack size and if another item is pushed then show the statement given in DT Text to be displayed in the working area (DT) • “ STACK IS IN OVERFLOW STATE BECAUSE IT IS FULL”

1 Step 4: 2 3 4 Instruction for the animator • When the user

1 Step 4: 2 3 4 Instruction for the animator • When the user clicks POP then that value should be removed from the stack as shown in the figure above 5 • Then the pointer should move downwards such that it points to the next item in the stack • The text in DT should appear in parallel to the figure Text to be displayed in the working area (DT) • The value popped from the stack is ______

1 Step 5: 2 3 4 Instruction for the animator • First two points

1 Step 5: 2 3 4 Instruction for the animator • First two points in the previous slide will be repeated depending on the size of the stack. • 5 Text to be displayed in the working area (DT) • The value popped from the stack is ______

1 Step 6: “ STACK IS IN UNDERFLOW STATE BECAUSE IT IS EMPTY” 2

1 Step 6: “ STACK IS IN UNDERFLOW STATE BECAUSE IT IS EMPTY” 2 3 4 Instruction for the animator • When the number of elements in the stack reaches zero and if another item is popped from the stack then show the statement given in DT 5 • When the user clicks POINTER VALUE, give the value of the item to which the pointer is pointing to. Text to be displayed in the working area (DT) • “ STACK IS IN UNDERFLOW STATE BECAUSE IT IS EMPTY” • Value of the item being pointed to is _______

1 Step 7: Queue 2 3 4 Instruction for the animator • When the

1 Step 7: Queue 2 3 4 Instruction for the animator • When the user selects Queue option in the main page, show fig. B in the master layout • Also give the below options in the right side of fig. B ENQUEUE, DEQUEUE and POINTERS VALUE • When the user enters a value and clicks ENQUEUE then that value should appear inside the QUEUE as shown in the figure above 5 • Then the H and T pointers should move upwards such that they point to the new item in the queue • The text in DT should appear in parallel to the figure Text to be displayed in the working area (DT) • The value enqueued into the queue is ______ (give the value corresponding to the value entered by the user).

1 Step 8: 2 3 4 Instruction for the animator • 3 rd and

1 Step 8: 2 3 4 Instruction for the animator • 3 rd and 4 th points in the previous slide will be repeated depending on the size of the queue. 5 • But H pointer always points to the first item in the queue and only the T pointer points to the new item in the queue Text to be displayed in the working area (DT) • The value enqueued into the queue is ______

1 Step 9: “ QUEUE IS IN OVERFLOW STATE BECAUSE IT IS FULL” 2

1 Step 9: “ QUEUE IS IN OVERFLOW STATE BECAUSE IT IS FULL” 2 3 4 5 Instruction for the animator • When the number of elements in the queue equals the queue size and if another item is enqueued then show the statement given in DT Text to be displayed in the working area (DT) • “ QUEUE IS IN OVERFLOW STATE BECAUSE IT IS FULL”

1 Step 10: 2 3 4 Instruction for the animator • When the user

1 Step 10: 2 3 4 Instruction for the animator • When the user clicks DEQUEUE then that value which H points to should be removed from the queue as shown in the figure above 5 • Then the items in the queue should move downwards such that H points to the next item in the queue • The text in DT should appear in parallel to the figure Text to be displayed in the working area (DT) • The value dequeued from the queue is ______

1 Step 11: 2 3 4 Instruction for the animator • First two points

1 Step 11: 2 3 4 Instruction for the animator • First two points in the previous slide will be repeated depending on the size of the stack. • 5 Text to be displayed in the working area (DT) • The value dequeued from the queue is ______

1 Step 12: “ QUEUE IS IN UNDERFLOW STATE BECAUSE IT IS EMPTY” 2

1 Step 12: “ QUEUE IS IN UNDERFLOW STATE BECAUSE IT IS EMPTY” 2 3 4 Instruction for the animator • When the number of elements in the queue reaches zero and if another item is dequeued from the queue then show the statement given in DT 5 • When the user clicks POINTERS VALUE, give the value of the items to which the pointers H and T are pointing to. Text to be displayed in the working area (DT) • “ QUEUE IS IN UNDERFLOW STATE BECAUSE IT IS EMPTY” • Value of the item being pointed by Head(H) is _______ • Value of the item being pointed by Tail(T) is _______

Electrical Engineering Slide 1 Slide 3 Introduction Definitions Slide 21 -25 Analogy Slide 26

Electrical Engineering Slide 1 Slide 3 Introduction Definitions Slide 21 -25 Analogy Slide 26 Want to know more… Test your understanding Lets Sum up (summary) (Further Reading) (questionnaire) Interactivity: In this LO demo itself is interactive session. Try it yourself 20 Credits

Questionnaire 1 2 3 1. If the characters ‘D’, ‘C’, ‘B’, ‘A’ are pushed

Questionnaire 1 2 3 1. If the characters ‘D’, ‘C’, ‘B’, ‘A’ are pushed in a stack(in that order), and then popped one at a time, in what order will they be removed? Answers: a) ACBD b) ABDC 4 5 c) ABCD d) None

Questionnaire 1 2 2. If the characters ‘D’, ‘C’, ‘B’, ‘A’ are inserted/enqueued in

Questionnaire 1 2 2. If the characters ‘D’, ‘C’, ‘B’, ‘A’ are inserted/enqueued in a queue(in that order), and then deleted/dequeued one at a time, in what order will they be removed? Answers: 3 a) ABCD b) DCBA 4 5 c) DCAB d) CDBA

Questionnaire 1 2 3. One example of stack operation: Answers: 3 4 5 a)

Questionnaire 1 2 3. One example of stack operation: Answers: 3 4 5 a) Luggage checking in airports b) CDs in a case

Questionnaire 1 2 4. One example of queue operation: Answers: 3 4 5 a)

Questionnaire 1 2 4. One example of queue operation: Answers: 3 4 5 a) Luggage checking in airports b) CDs in a case

Questionnaire 1 2 5. The operation for adding an entry to a stack is

Questionnaire 1 2 5. The operation for adding an entry to a stack is traditionally called : Answers: 3 a) add b) append 4 5 c) insert d) push

Links for further reading Reference websites: http: //en. wikipedia. org/wiki/Stack_%28 abstract_data_type%29 http: //en. wikipedia.

Links for further reading Reference websites: http: //en. wikipedia. org/wiki/Stack_%28 abstract_data_type%29 http: //en. wikipedia. org/wiki/Queue_%28 data_structure%29 http: //www. cmpe. boun. edu. tr/~akin/cmpe 223/chap 2. htm http: //www 2. roguewave. com/support/docs/hppdocs/stdug/10. html Books: