“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

CSE Lecture 12 – Linked Lists …
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
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.
COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues.
CHAPTER 4 Queues. Queue  The queue, like the stack, is a widely used data structure  A queue differs from a stack in one important way  A stack is.
CHAPTER 4 Queues MIDTERM THURSDAY, OCTOBER 17 IN LAB.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
COMP 103 Linked Stack and Linked Queue.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Stack Implementations Chapter Chapter Contents A Linked Implementation An Array-Based Implementation A Vector-Based Implementation.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Stacks, Queues, and Deques
Stacks, Queues, and Deques. 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.
Stacks, Queues, and Deques
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 3 Introduction to Collections – Stacks Modified
Stacks and Queues Introduction to Computing Science and Programming I.
Problem of the Day  What do you get when you cross a mountain climber and a grape?
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Chapter 16 Stacks & Queues. Objective In this chapter we will learn:  Stacks  Queues  Different implementations (arrays and linked list) of both 
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
ليست هاي پيوندي Linked Lists ساختمان داده ها و الگوريتم ها.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
18-1 Queues Data Structures and Design with Java and JUnit © Rick Mercer.
“When we quit thinking primarily about ourselves and our own self-preservation, we undergo a truly heroic transformation of consciousness.” – Joseph Campbell.
CS 367 Introduction to Data Structures Lecture 5.
Lab 6 Stack ADT. OVERVIEW The stack is one example of a constrained linear data structure. In a stack, the elements are ordered from most recently added.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Click to edit Master text styles Stacks Data Structure.
Linked Structures - Stacks. Linked Structures An alternative to array-based implementations are linked structures A linked structure uses object references.
Sections 3.4 Formal Specification
Elementary Data Structures
Thought for the Day “Without leaps of imagination, or dreaming, we lose the excitement of possibilities. Dreaming, after all, is a form of planning.”
Week 3 - Friday CS221.
Stacks.
Pointers and Linked Lists
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks, Queues, and Deques
More Data Structures (Part 1)
Stacks, Queues, and Deques
More Data Structures (Part 1)
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stacks, Queues, and Deques
CHAPTER 3: Collections—Stacks
Presentation transcript:

“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day

Chapter 5: Stacks and Queues Objectives –To consider stack, queue and deque ADTs –To look at their implementation using arrays and linked lists –To look at some of their applications –To study new implementation techniques for linked lists: doubly-linked lists, circularly- linked lists and list header nodes

Introduction Stacks, Queues –specialised lists –restricted ways of adding and removing elements Stack Queue

Introduction (cont.) Deques (Double-Ended Queues) –More general behaviour: add and remove from either end Deque

Stacks Elements added and removed at one end –the top LIFO (Last In, First Out) list Add: “push” Remove: “pop” Stack

The Stack Interface We will use a generic interface to specify the requirements for our stacks public interface Stack { public void push (T item); // Push the new item onto a stack public T pop (); // Pop item off top of stack public T top (); // Return a copy of top item public boolean isEmpty (); // Return TRUE if no items on stack } // interface Stack

Implementing a Stack: Array-Based Approach Very simple –One index to keep track of top item Empty stack: top stack max

Pushing Elements –Increment index –Store item top stack 0 G max e 1 o 2

Popping Elements –Return element –Decrement index top stack 2 Geo max 1 Return ’o’

Array Implementation Usual space problems: –Too big: waste space –Too small: run out of space

ArrayStack data, topIndex push, pop, top, isEmpty The ArrayStack Class Class Diagram Methods as required by the Stack interface

The ArrayStack Class public class ArrayStack implements Stack { private T[] data; // Array of data private int topIndex; // Top element public ArrayStack (int sz) { data = (T[])new Object[sz]; topIndex = -1; } // Constructor public ArrayStack () { this(100); } // Constructor... } // class ArrayStack Very similar to IntegerVector

The push and pop operations Very simple in Java –use the decrement and increment operators public void push (T item) // Push the new item onto an ArrayStack { if (topIndex >= data.length-1) throw new …Exception…(); data[++topIndex] = item; } // push public T pop () // Pop item off top of stack { if (topIndex < 0) throw new …Exception…(); return data[topIndex--]; } // pop Order is important

top and isEmpty public T top () // Return a copy of top item { if (topIndex < 0) throw new …Exception…(); return data[topIndex]; } // top public boolean isEmpty () // Return TRUE if no items on stack { return topIndex < 0; } // isEmpty

Linked List Implementation Very easy –Stack is one of the simplest linked-list structures Need a pointer to the top element Empty stack: topNode

ListStacktopNode push, pop, top, isEmpty The ListStack Class Class Diagram public class ListStack implements Stack { private class StackNode { public T data; public StackNode next; } // class StackNode private StackNode topNode; // Top StackNode in the stack... } // class ListStack

Pushing a New Element Create new node Link new node to current top node Make topNode point to new node topNode newNode G

Pushing a New Element (cont.) Create new node Link to top element Make top point to new node topNode G newNode e

A Stack with Several Elements topNode George

The push Method public void push (T item) { StackNode node = new StackNode(); node.data = item; node.next = topNode; topNode = node; } // push

Popping an Element Also very simple –Copy data in top node –Reset top pointer to point to next element public T pop () { if (topNode == null) // Stack is empty throw new …Exception…(); T tmpData = topNode.data; topNode = topNode.next; return tmpData; } // pop

The top and isEmpty methods public T top () // Return copy of top item { if (topNode == null) throw new …Exception…(); return topNode.data; } // top public boolean isEmpty () // Return TRUE if no items on stack { return topNode == null; } // isEmpty

Applications of Stacks Many problem areas: –Compilers –Iterative versions of recursive programs –Problem-solving –etc. Example: Reversing a string

Reversing a String Read letters, pushing them onto a stack Pop the letters off the stack, printing them LIFO — Last In, First Out