Stacks using Linked Lists. Stack Data Structure As we already know, stacks are linear data structures. This means that their contexts are stored in what.

Slides:



Advertisements
Similar presentations
Linked Lists.
Advertisements

Stacks, Queues, and Linked Lists
Review Learn about linked lists
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.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Data Structures: A Pseudocode Approach with C
Ceng-112 Data Structures I 1 Chapter 3 Linear Lists.
Chapter 3: Abstract Data Types Lists, Stacks Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists.
Linked Lists
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
COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Summary of lectures (1 to 11)
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof.
Objectives of these slides:
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
1 Joe Meehean.  Conceptual Picture access only to top item last-in-first-out (LIFO) item 1 item 2 item 3 Values in Values out 2.
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
1 Joe Meehean.  Conceptual Picture N items chained together using pointers pointed to by head variable  Advantage allows list to grow indefinitely without.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
FIST, Multi Media University Lecture 5 Stack (Array Implementation) Queue (Array Implementation )
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
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.
The List ADT A sequence of zero or more elements A 1, A 2, A 3, … A N-1 N: length of the list A 1 : first element A N-1 : last element A i : position i.
Linked List by Chapter 5 Linked List by
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Queues. Like Stacks, Queues are a special type of List for storing collections of entities. Stacks are Lists where insertions (pushes) and deletions (pops)
Data Structures Using C++1 Chapter 5 Linked Lists.
CHP-3 STACKS.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)
Linked Lists. Array List Issues Painful insert/remove at start/middle.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Linear Data Structures
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the basic concepts of linked lists ❏ To introduce the basic concepts.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
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.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
STACKS & QUEUES for CLASS XII ( C++).
Review Array Array Elements Accessing array elements
Queues.
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Stacks and Queues Chapter 4.
Chapter 15 Lists Objectives
Queues Queues Queues.
Stacks.
CSCI 3333 Data Structures Linked Lists.
Data Structures and Database Applications Stacks in C#
Queue and Priority Queue Implementations
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Program to find equivalence classes
Stack Implementations
Presentation transcript:

Stacks using Linked Lists

Stack Data Structure As we already know, stacks are linear data structures. This means that their contexts are stored in what looks like a line. An array, too, is a sort of linear data structure in which you can access any element directly. However, in a stack, you can only access the element at its top.

Stack Implementation with Linked Lists One disadvantage of using an array to implement a stack is the wasted space---most of the time most of the array is unused. A more elegant and economical implementation of a stack uses a linked list, which is a data structure that links together individual data objects as if they were ``links'' in a ``chain'' of data. ABC pStack

Stack Implementation with Linked Lists The question here is, where should we consider the top of the stack to be, the beginning or end of the list, and why? ABC pStack

Where should the top of the stack be? Since the Stack ADT uses a LIFO manner to retrieve data, the top of the stack is where all objects are added, and also retrieved from. Therefore, it is necessary that the programmer evaluates the pros and cons on figuring out if the top of the stack should be at the head of the linked list or at the tail.

Top of the stack at the tail Imagine the scenario where the top of the stack is at the end of the linked list. Since the Stack ADT uses a LIFO manner to retrieve data, in this case, new objects would have to be added to the end of the linked list, and retrieved from the end of the linked list too.

When the Top is at the Tail – push() In order to add a new object, it would be necessary to traverse the linked list and find the end. However, this could be taken care of with the help of a tail pointer (pTail), along with the head pointer (pStack). ABC pStack D pTail

When the Top is at the Tail – push() algorithm pushStack ( object ) { allocate ( pNew ) assign object data to pNew if ( Stack is empty ) { pStack = pNew pTail = pNew } else { pTail->next = pNew pTail = pNew } increment size of stack }

When the Top is at the Tail – pop() In order to delete a new object, it would be necessary to traverse the linked list and find the end. ABC pStack D pTail pPre delete node

When the Top is at the Tail – pop() In order to delete a new object, it would be necessary to traverse the linked list and find the end. Can this be taken care of with the help of a tail pointer (pTail) and another pointer that always points to the previous pointer from the tail (pPre), along with the head pointer (pStack)? Since there is no way to go backwards with this type of linked list, you will see that you would have to traverse the whole list to get to the previous pointer of pPre. This would result in a algorithm with a big O of n O(n)!

When the Top is at the Head – push() In this case, in order to add a new object, it would just add it to the head in this manner. ABC pStack D pNew

When the Top is at the Head – push() algorithm pushStack ( object ) { allocate ( pNew ) assign object data to pNew pNew->next = pStack pStack = pNew; increment size of stack }

When the Top is at the Head – pop() In order to delete a new object, it would be necessary to traverse the linked list and find the end. Can this be taken care of with the help of a tail pointer (pTail) and and another point that always points to the previous pointer from the tail (pPre), along with the head pointer (pStack)? ABC pStack D delete node

When the Top is at the Head – pop() algorithm popStack ( object ) { if ( stack is empty ) print stack exception message else pKill = pHead pStack = pKill->next assign data from pKill to object delete pKill decrement size of stack }