Stacks Written by J.J. Shepherd.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

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.
CS Data Structures II Review COSC 2006 April 14, 2017
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Data Structures & Algorithms
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Stacks and Queues Introduction to Computing Science and Programming I.
ECE 103 Engineering Programming Chapter 61 Abstract Data Types Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Stacks. What is a Stack? A stack is a type of data structure (a way of organizing and sorting data so that it can be used efficiently). To be specific,
Linked Lists and Generics Written by J.J. Shepherd.
Click to edit Master text styles Stacks Data Structure.
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.
Queues Written by J.J. Shepherd. Queues This data structure works on the principle first in first out (FIFO) Waiting in a line is a great example – Steam.
Prefix notation in action
STACKS & QUEUES for CLASS XII ( C++).
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
COSC160: Data Structures: Lists and Queues
Stacks and Queues Chapter 4.
12 C Data Structures.
UNIT-3 LINKED LIST.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Stacks.
Problems with Linked List (as we’ve seen so far…)
Stacks and Queues CMSC 202.
Queues Queues Queues.
Programming Abstractions
Stack and Queue APURBO DATTA.
Stacks Stack: restricted variant of list
Stack and Queue.
Linked list insertion Head. Linked list insertion Head.
Stacks A stack is a data structure that is similar in spirit to a pile of cafeteria trays. Think about the trays in the dining halls: when the dining staff.
Data Structures and Database Applications Queues in C#
Stacks and Queues.
Queues A queue is a data structure that is similar in spirit to a fair line. As you can see in this photo, the first dog in line can expect to be the first.
Abstract Data Types (ADTs)
Lists.
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks, Queues, and Deques
Data Structures and Database Applications Stacks in C#
Lecture 21 Stacks and Queues Richard Gesick.
Queues.
Stacks, Queues, and Deques
Lesson Objectives Aims
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Lesson 6. Types Equality and Identity. Collections.
Queues: Implemented using Arrays
CSE 214 – Computer Science II Stacks
Stacks: Implemented using Linked Lists
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Data Structures and Algorithms for Information Processing
Abstract Data Type Abstract Data Type as a design tool
Recall: stacks and queues
Priority Queues & Heaps
Abstract Data Type (ADT)
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
List Iterator Implementation
CS210- Lecture 6 Jun 13, 2005 Announcements
Queues: Implemented using Linked Lists
Stacks, Queues, and Deques
Lecture 9: Stack and Queue
Stack Implementations
Presentation transcript:

Stacks Written by J.J. Shepherd

Stacks This data structure works on the principle last in first out (LIFO) Any kind of pile is a good way to think of a stack A stack of games Pile of exams

Stacks Stacks can be represented by any simple abstract data structure Arrays Linked Lists There are two main pointers used while working with stacks Head: Points to the first element in the stack Tail: Points to the last element in the stack

Stacks There are four methods that are always in any representation of a stack Push: Adds a new element to the stack before the head Pop: Removes the element at the head of the stack (IE first element) Peek: Returns the value of the first element of the stack but does not remove it Print: Prints all elements in the queue

Arrays as Stacks The head is always index 0 The tail is the index of the first element inserted into the stack Index 1 2 3 4 5 6 Value 7 9 - Uninitialized Head Tail

Arrays as Stacks Push adds a new element to stack at the head Index 1 1 2 3 4 5 6 Value 7 9 - Uninitialized Head Tail

Arrays as Stacks First check whether or not the stack is full Index 1 if(tail >= data.length){return;} T tData = (T)aData; for(int i = tail;i>0;i--) { data[i] = data[i-1]; } data[0] = tData; tail++; Index 1 2 3 4 5 6 Value 7 9 - Head Tail

Arrays as Stacks Starting from the tail shift all values right Index 1 if(tail >= data.length){return;} for(int i = tail;i>0;i--) { data[i] = data[i-1]; } data[0] = (T)aData; tail++; Index 1 2 3 4 5 6 Value 7 9 - Head i Tail

Arrays as Stacks Starting from the tail shift all values right Index 1 if(tail >= data.length){return;} for(int i = tail;i>0;i--) { data[i] = data[i-1]; } data[0] = (T)aData; tail++; Index 1 2 3 4 5 6 Value 7 9 - Head i Tail

Arrays as Stacks Starting from the tail shift all values right Index 1 if(tail >= data.length){return;} for(int i = tail;i>0;i--) { data[i] = data[i-1]; } data[0] = (T)aData; tail++; Index 1 2 3 4 5 6 Value 7 9 - Head i Tail

Arrays as Stacks Starting from the tail shift all values right Index 1 if(tail >= data.length){return;} for(int i = tail;i>0;i--) { data[i] = data[i-1]; } data[0] = (T)aData; tail++; Index 1 2 3 4 5 6 Value 7 9 - Head i Tail

Arrays as Stacks Starting from the tail shift all values right Index 1 if(tail >= data.length){return;} for(int i = tail;i>0;i--) { data[i] = data[i-1]; } data[0] = (T)aData; tail++; Index 1 2 3 4 5 6 Value 7 9 - Head i Tail

Arrays as Stacks Starting from the tail shift all values right Index 1 if(tail >= data.length){return;} for(int i = tail;i>0;i--) { data[i] = data[i-1]; } data[0] = (T)aData; tail++; Index 1 2 3 4 5 6 Value 7 9 - Head i Tail

ETC

Arrays as Stacks Starting from the tail shift all values right Index 1 if(tail >= data.length){return;} for(int i = tail;i>0;i--) { data[i] = data[i-1]; } data[0] = (T)aData; tail++; Index 1 2 3 4 5 6 Value 7 9 - Head i Tail

Arrays as Stacks Set the head to the new data Index 1 2 3 4 5 6 Value if(tail >= data.length){return;} for(int i = tail;i>0;i--) { data[i] = data[i-1]; } data[0] = (T)aData; tail++; Index 1 2 3 4 5 6 Value 69 7 9 - Head Tail

Arrays as Stacks Move the tail forward Index 1 2 3 4 5 6 Value 69 7 9 if(tail >= data.length){return;} for(int i = tail;i>0;i--) { data[i] = data[i-1]; } data[0] = (T)aData; tail++; Index 1 2 3 4 5 6 Value 69 7 9 - Head Tail

Example

Arrays as Stacks Pop removes the element at the head and returns it Index 1 2 3 4 5 6 Value 69 7 9 - Head Tail

Arrays as Stacks Return null if the array stack is uninitialized Index if(data == null){return null;} T retVal = data[0]; for(int i=0;i<tail-1;i++) { data[i] = data[i+1]; } tail--; return retVal; Index 1 2 3 4 5 6 Value 69 7 9 - Head Tail

Arrays as Stacks Store the value at the head Index 1 2 3 4 5 6 Value if(data == null){return null;} T retVal = data[0]; for(int i=0;i<tail-1;i++) { data[i] = data[i+1]; } tail--; return retVal; Index 1 2 3 4 5 6 Value 69 7 9 - retVal 69 Head Tail

Arrays as Stacks Starting from the head shift everything left Index 1 if(data == null){return null;} T retVal = data[0]; for(int i=0;i<tail-1;i++) { data[i] = data[i+1]; } tail--; return retVal; Index 1 2 3 4 5 6 Value 69 7 9 - retVal 69 Head i Tail

Arrays as Stacks Starting from the head shift everything left Index 1 if(data == null){return null;} T retVal = data[0]; for(int i=0;i<tail-1;i++) { data[i] = data[i+1]; } tail--; return retVal; Index 1 2 3 4 5 6 Value 7 9 - retVal 69 Head i Tail

Arrays as Stacks Starting from the head shift everything left Index 1 if(data == null){return null;} T retVal = data[0]; for(int i=0;i<tail-1;i++) { data[i] = data[i+1]; } tail--; return retVal; Index 1 2 3 4 5 6 Value 7 9 - retVal 69 Head i Tail

Arrays as Stacks Starting from the head shift everything left Index 1 if(data == null){return null;} T retVal = data[0]; for(int i=0;i<tail-1;i++) { data[i] = data[i+1]; } tail--; return retVal; Index 1 2 3 4 5 6 Value 7 9 - retVal 69 Head i Tail

Arrays as Stacks Starting from the head shift everything left Index 1 if(data == null){return null;} T retVal = data[0]; for(int i=0;i<tail-1;i++) { data[i] = data[i+1]; } tail--; return retVal; Index 1 2 3 4 5 6 Value 7 9 - retVal 69 Head i Tail

Arrays as Stacks Starting from the head shift everything left Index 1 if(data == null){return null;} T retVal = data[0]; for(int i=0;i<tail-1;i++) { data[i] = data[i+1]; } tail--; return retVal; Index 1 2 3 4 5 6 Value 7 9 - retVal 69 Head i Tail

ETC

Arrays as Stacks Starting from the head shift everything left Index 1 if(data == null){return null;} T retVal = data[0]; for(int i=0;i<tail-1;i++) { data[i] = data[i+1]; } tail--; return retVal; Index 1 2 3 4 5 6 Value 7 9 - retVal 69 Head i Tail

Arrays as Stacks Move the tail back Index 1 2 3 4 5 6 Value 7 9 - Head if(data == null){return null;} T retVal = data[0]; for(int i=0;i<tail-1;i++) { data[i] = data[i+1]; } tail--; return retVal; Index 1 2 3 4 5 6 Value 7 9 - retVal 69 Head Tail

Arrays as Stacks Return the value Index 1 2 3 4 5 6 Value 7 9 - Head if(data == null){return null;} T retVal = data[0]; for(int i=0;i<tail-1;i++) { data[i] = data[i+1]; } tail--; return retVal; Index 1 2 3 4 5 6 Value 7 9 - retVal 69 Head Tail

Example

Stacks with Linked Lists Hmm…

It’s Dumbly Easy