Implementing a Stack as a Linked Structure CS 308 – Data Structures.

Slides:



Advertisements
Similar presentations
Sorted Lists CS Data Structures Sections 4.1, 4.2 & 4.3.
Advertisements

Stacks, Queues, and Linked Lists
DATA STRUCTURES USING C++ Chapter 5
Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Lists + CS3240, L. Grewe 1. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked.
What is a Queue? A queue is a FIFO “first in, first out” structure.
Chapter 5 ADTs Stack and Queue. 2 Goals Describe a stack and its operations at a logical level Demonstrate the effect of stack operations using a particular.
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.
Inheritance CS 308 – Data Structures “the mechanism by which one class acquires the properties of another class” the properties of another class”
1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.
Doubly-Linked Lists Same basic functions operate on list Each node has a forward and backward link: What advantages does a doubly-linked list offer? 88.
1 Chapter 4 Stack and Queue ADT. 2 Stacks of Coins and Bills.
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.
Implementing an Unsorted List as a Linked Structure CS 308 – Data Structures.
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.
Recursion CS 308 – Data Structures. What is recursion? smaller version Sometimes, the best way to solve a problem is by solving a smaller version of the.
1 expanded by J. Goetz Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Stacks CS 308 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Doubly Linked Lists CS 308 – Data Structures. Node data info: the user's data next, back: the address of the next and previous node in the list.back.next.info.
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.
Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)
What is a Stack? n Logical (or ADT) level: A stack is an ordered group of homogeneous items in which the removal and addition of items can take place only.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Chapter 7 Stacks Dr. Youssef Harrath
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
C++ Classes and Data Structures Jeffrey S. Childs
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Modified from the slides by Sylvia Sorkin, Community College of Baltimore County -
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,
Inheritance CS 302 – Data Structures Section 2.4 (pp ) and Section 6.7.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
1 Chapter 7 The Linked List as a Data Structure. 2 The List ADT A list is a list of elements. The list of elements consist of the data acted upon by list.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.
Data Structures: Stacks Queues 1. 2 Stack ADT: What is a Stack? a stack is a varying-length, collection of homogeneous elements Insertion and Deletion.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Chapter 5 ADTs Stack and Queue. Stacks of Coins and Bills.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Data Structures Using C++1 Chapter 7 Stacks. Data Structures Using C++2 Chapter Objectives Learn about stacks Examine various stack operations Learn how.
What happens... l When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
1 Data Structures and Algorithms Stacks and Queues.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Chapter 6 Lists Plus Lecture 12. What is a Circular Linked List? A circular linked list is a list in which every node has a successor; the “last” element.
CS 302 – Data Structures Sections 3.1, 3.2, 3.4 & 3.5
CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus.
Chapter 5 (Part 1) ADT Stack 1 Fall Stacks TOP OF THE STACK.
Linked Lists Chapter 6 Section 6.4 – 6.6
C++ Plus Data Structures
Stack and Queue APURBO DATTA.
Chapter 5 ADTs Stack and Queue.
Chapter 16-2 Linked Structures
C++ Plus Data Structures
Chapter 5 ADTs Stack and Queue Fall 2013 Yanjun Li CS2200.
Chapter 16 Linked Structures
Stacks CS-240 Dick Steflik.
C++ Plus Data Structures
CSI 1340 Introduction to Computer Science II
Data Structures and Algorithms Memory allocation and Dynamic Array
Presentation transcript:

Implementing a Stack as a Linked Structure CS 308 – Data Structures

Implementing stacks using arrays Simple implementation The size of the stack must be determined when a stack object is declared Space is wasted if we use less elements We cannot "push" more elements than the array can hold

Dynamic allocation of each stack element Allocate memory for each new element dynamically ItemType* itemPtr;... itemPtr = new ItemType; *itemPtr = newItem;

Dynamic allocation of each stack element (cont.) How should we preserve the order of the stack elements?

Chaining the stack elements together

Chaining the stack elements together (cont.) Each node in the stack should contain two parts: –info: the user's data –next: the address of the next element in the stack

Node Type template struct NodeType { ItemType info; NodeType* next; };

First and last stack elements We need a data member to store the pointer to the top of the stack The next element of the last node should contain the value NULL

Stack class specification // forward declaration of NodeType (like function prototype) template struct NodeType; template class StackType { public: StackType(); ~StackType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType); void Pop(ItemType&); private: NodeType * topPtr; };

Pushing on a non-empty stack

Pushing on a non-empty stack (cont.) The order of changing the pointers is very important !!

Pushing on an empty stack

Function Push template void StackType ::Push(ItemType item) { NodeType * location; location = new NodeType ; location->info = newItem; location->next = topPtr; topPtr = location; }

Popping the top element

Popping the top element (cont.) Need to use a temporary pointer

Function Pop template void StackType ::Pop(ItemType& item) { NodeType * tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; }

Popping the last element on the stack

Other Stack functions template StackType() StackType ::StackType() { topPtr = NULL; } template MakeEmpty() void StackType ::MakeEmpty() { NodeType * tempPtr; while(topPtr != NULL) { tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; }

Other Stack functions (cont.) template IsEmpty() bool StackType ::IsEmpty() const { return(topPtr == NULL); } template IsFull() bool StackType ::IsFull() const { NodeType * location; location = new NodeType ; if(location == NULL) return true; else { delete location; return false; } template StackType() StackType ::~StackType() { MakeEmpty(); }

Copy Constructors: an example using stacks Suppose we want to make a copy of a stack, will the following work? template void StackType(StackType oldStack, StackType & copy) { StackType tempStack; ItemType item; while(!oldStack.IsEmpty()) { oldStack.Pop(item); tempStack.Push(item); } while(!tempStack.IsEmpty()) { tempStack.Pop(item); copy.Push(item); }

Copy Constructors (cont.) Shallow Copy: an object is copied to another object without copying any pointed-to data Deep Copy: makes copies of any pointed-to data When do you need a copy constructor? (1) When parameters are passed by value (2) Return the value of a function (return thisStack;) (3) Initializing a variable in a declaration (StackType myStack=yourStack;)

Copy constructor for stacks template Stack Type ::StackType(const StackType & anotherStack) { NodeType * ptr1; NodeType * ptr2; if(anotherStack.topPtr == NULL) topPtr = NULL; else { topPtr = new NodeType ; topPtr->info = anotherStack.topPtr->info; ptr1 = anotherStack.topPtr->next; ptr2 = topPtr; while(ptr1 !=NULL) { ptr2->next = new NodeType ; ptr2 = ptr2->next; ptr2->info = ptr1->info; ptr1 = ptr1->next; } ptr2->next = NULL; } Alternatively, copy one stack to another using the assignment operator (you need to overload it though!!)

Comparing stack implementations Big-O Comparison of Stack Operations OperationArray Implementation Linked Implementation Class constructorO(1) MakeEmptyO(1)O(N) IsFullO(1) IsEmptyO(1) PushO(1) PopO(1) DestructorO(1)O(N)

Exercises 2, 3