1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Stacks, Queues, and Linked Lists
Data Structures ADT List
CS201: Data Structures and Discrete Mathematics I Linked Lists, Stacks and Queues.
Stack & Queues COP 3502.
§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.
More on Stacks and Queues. As we mentioned before, two common introductory Abstract Data Type (ADT) that worth studying are Stack and Queue Many problems.
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.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
 Balancing Symbols 3. Applications
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.
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
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.
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.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks, Queues & Deques CSC212.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
1 Introduction to Stacks What is a Stack? Stack implementation using array. Stack implementation using linked list. Applications of Stacks.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Lecture 11 Sept 26, 2011 Goals convert from infix to postfix.
Implementing and Using Stacks
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
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,
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
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.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
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.
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.
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
Stacks This presentation shows – how to implement the stack – how it can be used in real applications.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
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.
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.
Data Structure By Amee Trivedi.
Objectives In this lesson, you will learn to: Define stacks
Stacks and Queues.
CSC 172 DATA STRUCTURES.
Priority Queue.
Stacks Stack: restricted variant of list
COMPUTER 2430 Object Oriented Programming and Data Structures I
CMSC 341 Lecture 5 Stacks, Queues
Queues.
Data Structures ADT List
Data Structures ADT List
ADT list.
Elementary Data Structures
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues 1.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Queues CSC212.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Data Structures ADT List
ADT Queue (Array Implementation)
Stacks CS-240 Dick Steflik.
Stacks and Queues CSE 373 Data Structures.
Stack.
Presentation transcript:

1 Stacks & Queues CSC212

2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First In First Out (FIFO). –Used in operating systems, simulations etc. Priority Queues: Highest priority item is served first. –Used in operating systems, printer servers etc.

3 Stack (Linked Implementation) TOP

4 ADT Stack: Specification Elements: The elements are of a variable type. In a linked implementation an element is placed in a node. public class Node extends Object { public T data; public Node next; public Node () { data = null; next = null; } public Node (T val) { data = val; next = null; } }

5 ADT Stack: Specification Structure: the elements are linearly arranged, and ordered according to the order of arrival, most recently arrived element called top. Domain: the number of elements in the stack is bounded therefore the domain is finite. Type of elements: Stack

6 ADT Stack: Specification Operations: 1.Procedure Push (Stack S, Type e) requires: Stack S is not full. input: Stack S, Type e. results: Element e is added to the stack as its most recently added elements. output: none. 2.Procedure Pop (Stack S, Type e) requires: Stack S is not empty. input: Stack S. results: the most recently arrived element in S is removed and its value assigned to e. output: Type e. 3.Procedure Empty (Stack S, boolean flag) input: Stack S. results: If Stack S is empty then flag is true, otherwise false. output: flag.

7 ADT Stack: Specification Operations: 4.Procedure Full (Stack S, Boolean flag). requires: input: Stack S. results: If S is full then Full is true, otherwise Full is false. output: flag.

8 ADT Stack (Linked Implementation) TOP Data Element Pointer A Linked Implementation of the Stack.

9 ADT Stack (Linked Implementation) public class LinkStack { private Node top; /* Creates a new instance of LinkStack */ public LinkStack() { top = null; } public boolean empty(){ return top == null; }

10 ADT Stack (Linked Implementation) public boolean full(){ return false; } public void push(T e){ Node tmp = new Node(e); tmp.next = top; top = tmp; }

11 ADT Stack (Linked Implementation) public T pop(){ Node tmp = top; T e = top.data; top = top.next; return e; }

12 Stack: Array Implementation public class ArrayStack { private int maxsize; private int top; private T[] nodes; /** Creates a new instance of ArrayStack */ public ArrayStack(int n) { maxsize = n; top = 0; nodes = (T[]) new Object[n]; }

13 Stack: Array Implementation public boolean empty(){ return top == 0; } public boolean full(){ return top == maxsize; }

14 Stack: Array Implementation public void push(T e){ nodes[top++] = e; } public T pop(){ return nodes[--top]; }

15 Applications of Stacks Some applications of stacks are: –Balancing symbols. –Computing or evaluating postfix expressions. –Converting expressions from infix to postfix.

16 1. Balancing Symbols Expressions: mathematical (a + ((b-c)*d)) or programs have delimiters. begin{S1 S2{ beginS2S3 begin} ….S4 end} end

17 1. Balancing Symbols Delimiters must be balanced. [()] is legal but [(]) illegal. A stack can be used to check if the delimiters are balanced. –Read characters from the start of the expression to the end. –If the token is a starting delimiter, push on to the stack, if closing delimiter pop the corresponding start delimiter from the stack.

18 1. Balancing Symbols –If the stack is empty or if the popped symbol does not correspond to the closing symbol: report error. –If stack is not empty at the end of file report an error.

19 2. Postfix Expressions Evaluating Postfix Expressions: –Infix expression: 4.99* *1.06 –Value correct  parenthesis used. –Value incorrect  no parenthesis used. –In postfix form, above expression becomes: * *+  Advantage: no brackets are needed and a stack can be used to compute the expression.

20 2. Postfix Expressions Example: –infix: 6*(5+((2+3)*8)+3) –postfix: * *. Algorithm to compute postfix expression: –Read the postfix expression left to right. When a number is read push it on the stack; when a operator is read, pop two numbers from the stack and carry out the operation on them, push the result back on the stack.

21 3. Infix to Postfix Conversion A stack can also be used to convert an infix expression to postfix expression. (See handout) Example: infix expression a + b * c + (d * e + f) * g to postfix expression a b c * + d e * f + g * +

22 Queues Front Tail

23 ADT Queue: Specification Elements: The elements are of a variable type. In a linked implementation elements are placed in nodes. public class Node extends Object { public T data; public Node next; public Node () { data = null; next = null; } public Node (T val) { data = val; next = null; } }

24 ADT Queue: Specification Structure: the elements are linearly arranged, and ordered according to the order of arrival, most recently arrived element is called the tail and least recently arrived element the front or head. Domain: the number of elements in the queue is bounded therefore the domain is finite. Type of elements: Queue

25 ADT Queue: Specification Operations: 1.Procedure Enqueue (Queue Q, Type e) requires: Queue Q is not full. input: Queue Q, Type e. results: Element e is added to the queue at its tail. output: none. 2.Procedure Serve (Queue Q, Type e) requires: Queue Q is not empty. input: Queue Q. results: the element at the head of Q is removed and its value assigned to e. output: Type e. 3.Procedure Length (Queue Q, int length) input: Queue Q. results: The number of element in the Queue Q is returned. output: length.

26 ADT Queue: Specification Operations: 4.Procedure Full (Queue Q, Boolean flag). requires: input: Queue Q. results: If Q is full then flag is set to true, otherwise flag is set to false. output: flag.

27 ADT Queue (Linked Implementation) public class LinkQueue { private Node head, tail; private int size; /** Creates a new instance of LinkQueue */ public LinkQueue() { head = tail = null; size = 0; }

28 ADT Queue (Linked Implementation) public boolean full() { return false; } public int length (){ return size; }

29 ADT Queue (Linked Implementation) public void enqueue (Type e) { if (tail == null){ head = tail = new Node(e); } else { tail.next = new Node(e); tail = tail.next; } size++; }

30 ADT Queue (Linked Implementation) public Type serve() { Node tmp; Type x; tmp = head; x = head.data; head = head.next; size--; if (size == 0) tail = null; return x; }

31 ADT Queue (Array Implementation) Array implementation of the queue…a fixed size array is used to store the data elements. As data elements are enqueued & served the queue crawls through the array from low to high index values. As the queue crawls forward, it also expands and contracts.

32 ADT Queue (Array Implementation) HeadTail HeadTail After one En-queue and one Serve

33 ADT Queue (Array Implementation) HeadTail Where to En-queue this?

34 ADT Queue (Array Implementation) HeadTail Wrap Round 0 MaxSize-1

35 ADT Queue (Array Implementation) 0MaxSize - 1 Head Tail

36 ADT Queue (Array Implementaion) public class ArrayQueue { private int maxsize; private int size; private int head, tail; private T[] nodes; /** Creates a new instance of ArrayQueue */ public ArrayQueue(int n) { maxsize = n; size = 0; head = tail = 0; nodes = (T[]) new Object[n]; }

37 ADT Queue (Array Implementation) public boolean full () { return size == maxsize ? true : false; } public int length () { return size; } public void enqueue(T e) { nodes[tail] = e; tail = (tail + 1) % maxsize; size++; }

38 ADT Queue (Array Implementation) public T serve () { T e = nodes[head]; head = (head + 1) % maxsize; size--; return e; }

39 Priority Queue Each data element has a priority associated with it.Highest priority item is served first. Real World Priority Queues: hospital emergency rooms…most sick patients treated first, events in a computer system, etc. Priority Queue can be viewed as: –View 1: Priority queue as an ordered list. –View 2: Priority queue as a set.

40 ADT Priority Queue Specification: Elements: The elements are of type PQNode. Each node has in it a data element of variable type and priority of type Priority ( which could be int type). public class PQNode { private T data; private Priority priority; public PQNode next; public PQNode() { next = null; } public PQNode(T e, Priority p) { data = e; priority = p; }

41 ADT Priority Queue Structure: the elements are linearly arranged, and may be ordered according to a priority value, highest priority element is called the tail and least priority element the front or head. Domain: the number of nodes in the queue is bounded therefore the domain is finite. Type of elements: PriorityQueue

42 ADT Priority Queue Operations: 1.Procedure Enqueue (PriorityQueue PQ, Type e, Priority p) requires: PQ is not full. input: PQ, e. results: Element e is added to the queue according to its priority. output: none. 2.Procedure Serve (PriorityQueue PQ, Type e, Priority p) requires: PQ is not empty. input: PQ. results: the element at the head of PQ is removed and returned. output: e, p. 3.Procedure Length (PriorityQueue PQ, int length) input: PQ. results: The number of element in the PQ is returned. output: length.

43 ADT Priority Queue Operations: 4.Procedure Full (PrioriytQueue PQ, Flag flag). requires: input: PQ. results: If PQ is full then flag is set to true, otherwise flag is set to false. output: flag.

44 ADT Priority Queue el 10 el HeadTail Array Implementation el 7 Insert Where? el 10 el 10 el Head Tail Linked Implementation el

45 ADT Priority Queue (Linked) public class LinkPQ { private int size; private PQNode head, tail; /* tail is of no use here. */ public LinkPQ() { head = tail = null; size = 0; } public int length (){ return size; }

46 ADT Priority Queue (Linked) public int length (){ return size; } public boolean full () { return false; }

47 ADT Priority Queue (Linked) public void enqueue(T e, int pty) { PQNode p, q, tmp; if ((size == 0) || (pty > head.Priority())) { tmp = new PQNode (e, pty); tmp.next = head; head = tmp; } else { p = head; q = null; while ((p != null) && (p.Priority() > pty)) { q = p; p = p.next; } tmp = new PQNode (e, pty); tmp.next = p; q.next = tmp; } }

48 ADT Priority Queue (Linked) public T serve (Priority pty){ T e = head.get_data(); pty.set_value(head.get_priority().get_value()); head = head.next; size--; return(e); }

49 ADT Priority Queue Implementations –Array Implementation: Enqueue is O(n), Serve is O(1). –Linked List: Enqueue is O(n), Serve is O(1). –Heap: Enqueue is O(log n), Serve is O(log n)  Heaps to be discussed later.