stack & queue

26
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 Level(UG/PG): UG Author(s) : Phani Swathi Chitta Mentor: Aruna Adil *The contents in this ppt are licensed under Creative Commons Attribution-NonCommercial- ShareAlike 2.5 India license

Upload: borna

Post on 19-Mar-2016

39 views

Category:

Documents


0 download

DESCRIPTION

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 Level(UG/PG): UG - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Stack & Queue

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 Level(UG/PG): UG Author(s) : Phani Swathi ChittaMentor: Aruna Adil

*The contents in this ppt are licensed under Creative Commons Attribution-NonCommercial-ShareAlike 2.5 India license

Page 2: Stack & Queue

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

Page 3: Stack & Queue

Definitions of the components/Keywords:

5

3

2

4

1 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.

Page 4: Stack & Queue

Definitions of the components/Keywords:

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:

•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

3

2

4

1

Page 5: Stack & Queue

Definitions of the components/Keywords:

5

3

2

4

1 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.

Page 6: Stack & Queue

Definitions of the components/Keywords:

5

3

2

4

1 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.

Page 7: Stack & Queue

Master Layout 1

5

3

2

4

1

• 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

Fig. BFig. A

Stack Queue

Page 8: Stack & Queue

Step 1: 1

5

32

4

Stack

Instruction for the animator Text to be displayed in the working area (DT)

• 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

• The text in DT should appear in parallel to the figure

• The value pushed into the stack is ______ (give the value corresponding to the value entered by the user).

Page 9: Stack & Queue

Step 2: 1

5

32

4Instruction for the animator Text to be displayed in the working area (DT)

• 3rd and 4th points in the previous slide will be repeated depending on the size of the stack.

• The value pushed into the stack is ______

Page 10: Stack & Queue

Step 3: 1

5

32

4

“ STACK IS IN OVERFLOW STATE BECAUSE IT IS FULL”

Instruction for the animator Text to be displayed in the working area (DT)• 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

• “ STACK IS IN OVERFLOW STATE BECAUSE IT IS FULL”

Page 11: Stack & Queue

Step 4: 1

5

32

4Instruction for the animator Text to be displayed in the working area (DT)

• When the user clicks POP then that value should be removed from the stack as shown in the figure above

• 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

• The value popped from the stack is ______

Page 12: Stack & Queue

Step 5: 1

5

32

4Instruction for the animator Text to be displayed in the working area (DT)

• First two points in the previous slide will be repeated depending on the size of the stack.

• The value popped from the stack is ______

Page 13: Stack & Queue

Step 6: 1

5

32

4

“ STACK IS IN UNDERFLOW STATE BECAUSE IT IS EMPTY”

Instruction for the animator Text to be displayed in the working area (DT)• 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

• When the user clicks POINTER VALUE, give the value of the item to which the pointer is pointing to.

• “ STACK IS IN UNDERFLOW STATE BECAUSE IT IS EMPTY”

• Value of the item being pointed to is _______

Page 14: Stack & Queue

Step 7: 1

5

32

4

Queue

Instruction for the animator Text to be displayed in the working area (DT)

• 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

• 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

• The value enqueued into the queue is ______ (give the value corresponding to the value entered by the user).

Page 15: Stack & Queue

Step 8: 1

5

32

4Instruction for the animator Text to be displayed in the working area (DT)

• 3rd and 4th points in the previous slide will be repeated depending on the size of the queue.

• 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

• The value enqueued into the queue is ______

Page 16: Stack & Queue

Step 9: 1

5

32

4

“ QUEUE IS IN OVERFLOW STATE BECAUSE IT IS FULL”

Instruction for the animator Text to be displayed in the working area (DT)• 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

• “ QUEUE IS IN OVERFLOW STATE BECAUSE IT IS FULL”

Page 17: Stack & Queue

Step 10: 1

5

32

4Instruction for the animator Text to be displayed in the working area (DT)

• When the user clicks DEQUEUE then that value which H points to should be removed from the queue as shown in the figure above

• 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

• The value dequeued from the queue is ______

Page 18: Stack & Queue

Step 11: 1

5

32

4Instruction for the animator Text to be displayed in the working area (DT)

• First two points in the previous slide will be repeated depending on the size of the stack.

• The value dequeued from the queue is ______

Page 19: Stack & Queue

Step 12: 1

5

32

4

“ QUEUE IS IN UNDERFLOW STATE BECAUSE IT IS EMPTY”

Instruction for the animator Text to be displayed in the working area (DT)• 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

• When the user clicks POINTERS VALUE, give the value of the items to which the pointers H and T are pointing to.

• “ 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 _______

Page 20: Stack & Queue

Introduction

Credits

20

Definitions Test your understanding (questionnaire) Lets Sum up (summary) Want to know more…

(Further Reading)

Try it yourself

Interactivity:

Analogy

Slide 1

Slide 3

Slide 21-25

Slide 26

Electrical Engineering

In this LO demo itself is interactive session.

Page 21: Stack & Queue

Questionnaire1. 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 c) ABCD d) None

1

5

2

4

3

Page 22: Stack & Queue

Questionnaire

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: a) ABCD b) DCBA c) DCAB d) CDBA

1

5

2

4

3

Page 23: Stack & Queue

Questionnaire

3. One example of stack operation:

Answers: a) Luggage checking in airports b) CDs in a case

1

5

2

4

3

Page 24: Stack & Queue

Questionnaire

4. One example of queue operation: Answers: a) Luggage checking in airports b) CDs in a case

1

5

2

4

3

Page 25: Stack & Queue

Questionnaire

5. The operation for adding an entry to a stack is traditionally called :

Answers: a) add b) appendc) insertd) push

1

5

2

4

3

Page 26: Stack & Queue

Links for further readingReference websites:http://en.wikipedia.org/wiki/Stack_%28abstract_data_type%29http://en.wikipedia.org/wiki/Queue_%28data_structure%29http://www.cmpe.boun.edu.tr/~akin/cmpe223/chap2.htmhttp://www2.roguewave.com/support/docs/hppdocs/stdug/10.html

Books: