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.

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
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.
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.”
CS Data Structures II Review COSC 2006 April 14, 2017
Circular Arrays Neat trick: use a circular array to insert and remove items from a queue in constant time The idea of a circular array is that the end.
CMPT 225 Stacks.
Stacks A stack is a data structure that only allows items to be inserted and removed at one end We call this end the top of the stack The other end is.
Chapter 3 Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
Cmpt-225 Queues. A queue is a data structure that only allows items to be inserted at the end and removed from the front Queues are FIFO (First In First.
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 The Abstract Data Type Specifications of an abstract data type for a particular.
TCSS 342, Winter 2005 Lecture Notes
Implementing and Using Stacks
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
Definition Stack is an ordered collection of data items in which access is possible only at one end (called the top of the stack). Stacks are known.
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.”
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks (and a bit of generics for flavor)
Chapter 7 Stacks II CS Data Structures I COSC 2006
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 5: Stacks and Queues.
ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
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.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
1 Chapter 20 Lists, Stacks, Queues Lecture 7 Dr. Musab Zghoul برمجة هيكلية.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Stacks An Abstract Data Type. Restricted Access Unlike arrays, stacks only allow the top most item to be accessed at any time The interface of a stack.
April 27, 2017 COSC Data Structures I Review & Final Exam
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
CSC 205 – Java Programming II Lecture 30 April 3, 2002.
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.
Chapter 7 A Queues. © 2004 Pearson Addison-Wesley. All rights reserved7 A-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear,
 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.
ADT Stack & Queue - Case Studies TCP1201: 2013/2014.
Chapter 7 Stacks © 2006 Pearson Addison-Wesley. All rights reserved 7A-1.
Chapter 6 A Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 A-2 The Abstract Data Type: Developing an ADT During the Design of a Solution.
ADT description Implementations
COSC160: Data Structures: Lists and Queues
Cinda Heeren / Geoffrey Tien
Stacks.
Introduction to Data Structures
Stacks and Queues.
Building Java Programs
Stacks and Queues.
CSC 143 Queues [Chapter 7].
Stacks and Queues 1.
Data Structures and Algorithms for Information Processing
Chapter 8 Queues © 2006 Pearson Addison-Wesley. All rights reserved.
Queues Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks and Queues.
Chapter 7 © 2011 Pearson Addison-Wesley. All rights reserved.
Lecture 9: Stack and Queue
Queues.
CMPT 225 Lecture 8 – Queue.
Presentation transcript:

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 of the stack Nodes are removed from the front (top) of the list Straight-forward linked list implementation push and pop can be implemented fairly easily, e.g. assuming that head is a reference to the node at the front of the list public void push(int x){ // Make a new node whose next reference is // the existing list Node newNode = new Node(x, top); top = newNode; // top points to new node }

List Stack Example Java Code Stack st = new Stack(); st.push(6); top 6

List Stack Example Java Code Stack st = new Stack(); st.push(6); st.push(1); top 61

List Stack Example Java Code Stack st = new Stack(); st.push(6); st.push(1); st.push(7); top 617

List Stack Example Java Code Stack st = new Stack(); st.push(6); st.push(1); st.push(7); st.push(8); top 6178

List Stack Example Java Code Stack st = new Stack(); st.push(6); st.push(1); st.push(7); st.push(8); st.pop(); top 6178

List Stack Example Java Code Stack st = new Stack(); st.push(6); st.push(1); st.push(7); st.push(8); st.pop(); top 617

Stack: ADT List Implementation Push() and pop() either at the beginning or at the end of ADT List at the beginning: public void push(Object newItem) { list.add(1, newItem); } // end push public Object pop() { Object temp = list.get(1); list.remove(1); return temp; } // end pop

Stack: ADT List Implementation Push() and pop() either at the beginning or at the end of ADT List at the end: public void push(Object newItem) { list.add(list.size()+1, newItem); } // end push public Object pop() { Object temp = list.get(list.size()); list.remove(list.size()); return temp; } // end pop

Stack: ADT List Implementation Push() and pop() either at the beginning or at the end of ADT List Efficiency depends on implementation of ADT List (not guaranteed) On other hand: it was very fast to implement (code is easy, unlikely that errors were introduced when coding).

Applications of Stacks Call stack (recursion). Searching networks, traversing trees (keeping a track where we are). Examples: Checking balanced expressions Recognizing palindromes Evaluating algebraic expressions

Simple Applications of the ADT Stack: Checking for Balanced Braces A stack can be used to verify whether a program contains balanced braces An example of balanced braces abc{defg{ijk}{l{mn}}op}qr An example of unbalanced braces abc{def}}{ghij{kl}m abc{def}{ghij{kl}m

Checking for Balanced Braces Requirements for balanced braces Each time you encounter a “}”, it matches an already encountered “{” When you reach the end of the string, you have matched each “{”

Checking for Balanced Braces Figure 7-3 Traces of the algorithm that checks for balanced braces

Evaluating Postfix Expressions A postfix (reverse Polish logic) calculator Requires you to enter postfix expressions Example: * When an operand is entered, the calculator Pushes it onto a stack When an operator is entered, the calculator Applies it to the top two operands of the stack Pops the operands from the stack Pushes the result of the operation on the stack

Evaluating Postfix Expressions Figure 7-8 The action of a postfix calculator when evaluating the expression 2 * (3 + 4)

Evaluating Postfix Expressions Pseudo code: int evaluate(String expression) { Stack stack=new Stack(); // creaty empty stack while (true) { String c=expression.getNextItem(); if (c==ENDOFLINE) return stack.pop(); if (c is operand) stack.push(c); else { // operation int operand2=stack.pop(); int operand1=stack.pop(); stack.push(execute(c,operand1,operand2)); }

Queues A queue is a data structure that only allows items to be inserted at the end and removed from the front “Queue” is the British word for a line (or line-up) Queues are FIFO (First In First Out) data structures – “fair” data structures

Using a Queue

What Can You Use a Queue For? Processing inputs and outputs to screen (console) Server requests Instant messaging servers queue up incoming messages Database requests Print queues One printer for dozens of computers Operating systems use queues to schedule CPU jobs Simulations

Queue Operations A queue should implement (at least) these operations: enqueue – insert an item at the back of the queue dequeue – remove an item from the front peek – return the item at the front of the queue without removing it Like stacks it is assumed that these operations will be implemented efficiently That is, in constant time

Queue: Array Implementation First consider using an array as the underlying structure for a queue, one plan would be to Make the back of the queue the current size of the queue (i.e., the number of elements stored) Make the front of the queue index 0 Inserting an item can be performed in constant time But removing an item would require shifting all elements in the queue to the left which is too slow! Therefore we need to find another way

An Array-Based Implementation Figure 8-8 a) A naive array-based implementation of a queue; b) rightward drift can cause the queue to appear full