Objectives of these slides:

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Sample PMT online… Browse 1120/sumII05/PMT/2004_1/ 1120/sumII05/PMT/2004_1/
Lec 7 Sept 17 Finish discussion of stack infix to postfix conversion Queue queue ADT implementation of insert, delete etc. an application of queue.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Stack and Queue COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
Lecture 11 Sept 26, 2011 Goals convert from infix to postfix.
More About Stacks: Stack Applications Dan Nguyen CS 146, Spring 2004 Professor Sin-Min Lee.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Stack Applications.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
Stack and Queue.
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
CHP-3 STACKS.
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
Data Structures: A Pseudocode Approach with C1 Chapter 3 Objectives Upon completion you will be able to Explain the design, use, and operation of a stack.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Stacks Chapter 3 Objectives Upon completion you will be able to
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
Stacks and Queues Chapter 4.
Homework 4 questions???.
Objectives In this lesson, you will learn to: Define stacks
Stacks and Queues.
Stacks.
Stacks Stack: restricted variant of list
Visit for more Learning Resources
PART II STACK APPLICATIONS
CMSC 341 Lecture 5 Stacks, Queues
Principles of Computing – UFCFA3-30-1
More About Stacks: Stack Applications
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Stacks and Queues 1.
Data Structures and Algorithms for Information Processing
Queue Applications Lecture 31 Tue, Apr 11, 2006.
More About Stacks: Stack Applications
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Presentation transcript:

Objectives of these slides: 7 – Stack and Queue Objectives of these slides: Explain the design, use, and operation of a stack and a queue Implement a stack and a queue using a linked list structure

A Stack Collection Stack Implementation 3. Stack applications Overview: A Stack Collection Stack Implementation 3. Stack applications A queue Collection Queue Implementation

1. The Stack Collection A stack is a sequence of items that are accessible only from the top of the stack. A Stack (of plates) push pop Other operations: isEmpty peek size

1. The Stack Collection: Stack Operations A stack is a Last In, First Out (LIFO) data structure in which all insertions and deletion are restricted to one end called a top Three basic stack operations - Push - Pop - Stack Top Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

1.2 Stack Operations : Push adds an item at the top of the stack after the push, the new item becomes the top the stack is in an overflow state if there is no room for the new item Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

1.2 Stack Operations : Pop 2. Pop when a stack is popped, the item at the top of the stack is removed and return it to the user as the top item has been removed, the next older item in the stack becomes the top when the last item in the stack is deleted, it must be set to its empty state if pop is called when the stack is empty, then it is in an underflow state Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

1.2 Stack Operations : Pop Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

1.2 Stack Operations : Stack Top copies the item at the top of the stack it returns the data in the top element to the user but does not delete it stack top can also result in underflow if the stack is empty Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Stack operations Example (1/2) 0. with an empty stack push green into stack push blue into stack pop push red into stack Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Stack operations Example (2/2) stack top =? pop Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

2. Stack implementation There are several data structures that could be used to implement a stack, e.g. array or linked list In this section, we implement it as a linked list To implement the linked-list stack, we need two different structures: - a head node - a data node Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

2.1 Stack Head and Stack Data Node stack head requires at least 2 attributes a count of the number of elements in the stack a top pointer other stack attributes can be placed in a stack head such as the time the stack was created stack data node also requires at least 2 attributes data pointer to the next node Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Stack head node structure and data node structure Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

2.2 Stack Algorithms 8 operations are defined to solve any basic stack problem create stack destroy stack push stack pop stack Checking stack status full stack empty stack a number of member (stack count) stack top there may be additional stack operations Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Stack operations Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Create Stack Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Push Stack Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Pop Stack Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Stack Top Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Empty Stack Checking Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Full Stack Checking Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Stack Count Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Stack Destruction Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

3. Stack Applications 3.1 Reversing Data 3.2 Converting Decimal to Binary 3.3 Converting Infix to Postfix (optional) Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

3.1 Reversing Data given a set of data, the first and last elements are exchanged with all of the positions between the first and last being relatively exchanged also for example: {1, 2, 3, 4} becomes {4, 3, 2, 1} we examine 2 different reversing applications: reveres a list convert decimal to binary Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Algorithm Reverse a number series (1/2) Algorithm reverseNumber This program reverses a list of integers read from the keyboard by pushing them into a stack and retrieving them one by one Begin reverseNumber 1 stack = createStack 2 print(Enter a number) 3 read(number) Continued… Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Algorithm Reverse a number series (2/2) /*---- Fill stack ---* / 4 loop ( not end of data AND stack not full) 1 pushStack (number) 2 prompt(Enter next number: <EOF> to stop) 3 read( number ) /*---- Print numbers in reverse ---*/ 5 loop (not emptyStack (stack) 1 popStack (stack, dataOut) 2 print (dataOut) 6 stack = destroyStack (stack) End reverseNumber Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

3.2 Convert Decimal to Binary 1 read (number) 2 loop (number > 0) 1 digit = number modulo 2 2 print (digit) 3 number = number / 2 Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Algorithm Convert decimal to binary (1/2) algorithm decimalToBinary This algorithm reads an integer from the keyboard and print its binary equivalent. It uses a stack to reverse the order of 0’s and 1’s produces. Begin decimalToBinary 1 stack = createStack 2 prompt(Enter a decimal to convert to binary) 3 read (number) 4 loop (number > 0) 1 digit = number modulo 2 2 pushOK = push (stack, digit) 3 if (pushOK ≠ false) 1 print (Stack overflow creating digit) 2 quit algorithm 4 number = number/2 Continued… Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Algorithm Convert decimal to binary (2/2) /* Binary number create in stack. Now print it. */ 5  loop (not emptyStack(stack)) 1 popStack (stack, digit) 2 print(digit) /* Binary number create. Destroy stack and return */ 6 destroy(stack) End decimalToBinary Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

3.3 Convert Infix to Postfix (arithmetic expression) Prefix Notation (Polish notation): operator is always placed before its operands e.g. + 1 2 , * 3 – 4 5 , / 9 + 1 – 8 6 In the Postfix evaluation format for an expression, an operator comes after its operands. also called Reverse Polish Notation (RPN) Examples: Infix notation Postfix notation a + b RPN: a b + a * b + c RPN: a b * c + a + (b * c) RPN: a b c * + (a + b) * c RPN: a b + c * (a*b + c) / d RPN: a b * c + d / Original slides from http://fivedots.coe.psu.ac.th/Software.coe/ADSA/Slides/08.%20Stacks.ppt

Postfix Evaluation (1/4) To evaluate a postfix expression, execute the following steps until the end of the expression. If current input is an operand, push it on the stack. If current input is an operator, pop its two operands, apply the operator, and push the result onto the stack. At the end of input, the value of the postfix expression is on the top of the stack. Postfix and prefix (Polish) notations do not need the parenthesis and priority of operators. The order of evaluation is not ambiguous. One expression can only be evaluated in one way. Original slides from http://fivedots.coe.psu.ac.th/Software.coe/ADSA/Slides/08.%20Stacks.ppt

Postfix Evaluation (2/4) Example: evaluate "4 3 * 5 +" 4 12 17 3 5 Evaluate these postfix expressions: 8 2 – 3 / 4 2 * + 28 7 13 5 6 + - * / Original slides from http://fivedots.coe.psu.ac.th/Software.coe/ADSA/Slides/08.%20Stacks.ppt

Postfix evalutation: Error: too many operators (3/4) e.g. 3 8 + * 9 The stack will contain only one element when we reach "*". Original slides from http://fivedots.coe.psu.ac.th/Software.coe/ADSA/Slides/08.%20Stacks.ppt

Postfix evalutation: Error: too many operands (4/4) e.g. 9 8 + 7 The stack will contain too many operands at end of input. Original slides from http://fivedots.coe.psu.ac.th/Software.coe/ADSA/Slides/08.%20Stacks.ppt

Algorithm for coverting Infix to Postfix (1/2) Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Algorithm for coverting Infix to Postfix (2/2) 3 end loop 4 pushStack (Stack, token) 4 else Character is operand 1 Concatenate token to postFix 5 end if 3 end loop Input formula empty. Pop stack to postfix 4 loop ( not emptyStack(Stack) ) 1 popStack(Stack, character) 2 concatenate token to postFixExpr 5 end loop 6 return postFix end intoPostFix Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

4. Queue Collection Like a stack, a queue is also a list. However, with a queue, insertion is done at one end, while deletion is performed at the other end. Accessing the elements of queues follows a First In, First Out (FIFO) order. Like customers standing in a check-out line in a store, the first customer in is the first customer served (first come first serve). rear front Original slides from http://course.cs.ust.hk/comp171/Spring09/tut/T3.ppt

4.1 Enqueue and Dequeue Primary queue operations: Enqueue and Dequeue Like check-out lines in a store, a queue has a front and a rear. Enqueue insert an element at the rear of the queue Dequeue remove an element from the front of the queue Insert (Enqueue) Remove (Dequeue) rear front Original slides from http://course.cs.ust.hk/comp171/Spring09/tut/T3.ppt

5. Implementation of Queue Just as stacks can be implemented as arrays or linked lists, so with queues. Dynamic queues have the same advantages over static queues as dynamic stacks have over static stacks Example: Queue implemented with linked list data structure Queue Head Structure Count (number of elements) Front (pointer to front node) Rear (pointer to rear node) Queue Node Structure Data (node data) Link (pointer to next node) Original slides from http://course.cs.ust.hk/comp171/Spring09/tut/T3.ppt

5.1 Application of Queues In a multitasking operating system, the CPU time is shared between multiple processes. At a given time, only one process is running, all the others are ‘sleeping’. The CPU time is administered by the scheduler. The scheduler keeps all current processes in a queue with the active process at the front of the queue. Original slides from http://isg.cs.tcd.ie/giangt/Stack-Queue.pdf

5.2 Stack and Queue comparison List out one similarity and one difference between Stack and Queue Similarity Both ADTs can be implemented using array or linked list Both support fast insertion and extraction (although the rule to govern the insertion and extraction is different) And more… Difference Stack is a LIFO data structure, while Queue is a FIFO data structure Original slides from http://course.cs.ust.hk/comp171/Spring09/tut/T3.ppt