Stacks and Queues Chapter 4.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

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.
Ceng-112 Data Structures I Chapter 5 Queues.
CHAPTER 7 Queues.
Data Structures: A Pseudocode Approach with C
CS Data Structures II Review COSC 2006 April 14, 2017
Chapter 3: Abstract Data Types Lists, Stacks Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
Data Structures & Algorithms
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.
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
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.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Ali Abdul Karem Habib Kufa University / mathematics & Science Of Computer.
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.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,
Chapter 7 Stacks Dr. Youssef Harrath
Stacks and queues Basic operations Implementation of stacks and queues Stack and Queue in java.util Data Structures and Algorithms in Java, Third EditionCh04.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
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.
CSC 202 Analysis and Design of Algorithms Lecture 06: CSC 202 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List, Stack and.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Data Structures Using C++1 Chapter 7 Stacks. Data Structures Using C++2 Chapter Objectives Learn about stacks Examine various stack operations Learn how.
Data Structures Using Java1 Chapter 6 Stacks. Data Structures Using Java2 Chapter Objectives Learn about stacks Examine various stack operations Learn.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Data Structures: A Pseudocode Approach with C1 Chapter 3 Objectives Upon completion you will be able to Explain the design, use, and operation of a stack.
STACK Data Structure
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Stacks Chapter 3 Objectives Upon completion you will be able to
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.
Stacks II David Lillis School of Computer Science and Informatics
Data Structures Using C++ 2E
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
Stacks.
Queues Chapter 4.
Chapter 12 – Data Structures
CC 215 Data Structures Stack ADT
Stacks and Queues.
Stacks.
Stack and Queue APURBO DATTA.
Queues Chapter 4.
Data Structures Array Based Stacks.
Stacks Stack: restricted variant of list
Algorithms and Data Structures
Stack.
CMSC 341 Lecture 5 Stacks, Queues
Stacks.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Stacks: Implemented using Linked Lists
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Stacks Data structure Elements added, removed from one end only
Stacks CS-240 Dick Steflik.
Stack.
LAB#3 Stacks Nora Albabtin nora albabtin.
Presentation transcript:

Stacks and Queues Chapter 4

4.1 Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called LIFO. A stack is defined in terms of operations that change its status and operations that check this status.: Clear() isEmpty() Push(el) Pop() topEl()

Stack is useful when data have to be stored and then retrieved in reverse order. An application example of the stack is in matching delimiters in a program. parentheses, square brackets, curly brackets, and comment delimiters. Ex: a= b + (c-d) * (e-f) The program could have nested delimitiers.

The delimiters matching algorithm in C++

A stack is a last in, first out (LIFO) abstract data type and data structure . A stack can have any abstract data type as an element ,but is characterized by only two fundamental operations: push and pop. Elements are removed from the stack in the reverse order to the order of their addition :therefore, the lower elements are those that have been on the stack the longest.

Basic Operations on a Stack InitializeStack: Initializes the stack to an empty state. DestroyStack: Removes all the elements from the stack, leaving the stack empty. IsEmptyStack: Checks whether the stack is empty. If empty, it returns true; otherwise, it returns false. IsFullStack: Checks whether the stack is full. If full, it returns true; otherwise, it returns false

Basic Operations on a Stack Push: Add new element to the top of the stack The input consists of the stack and the new element. Prior to this operation, the stack must exist and must not be full Top: Returns the top element of the stack. Prior to this operation, the stack must exist and must not be empty. Pop: Removes the top element of the stack.

Operations: initialize destroy build from given data (set of elements) check if it is empty get the total size of the stack add an element to the top of the stack [PUSH] delete an element from the top of the stack [POP] get the data from the element at the top of the stack update the data of the top element print the data of the top element print the entire stack

In stack , no search, no adding in arbitrary positions, no sorting, no access to anything beyond the top element.

Check for enough room, (no overflow) Data Push Top Top operation

Check if empty, (no underflow) Data Top Pop Top operation

Check if empty, (no underflow) Data Top Top Stack top operation

Top (a) Conceptual count Head Data nodes (a) Physical

Stack Linked List Implementation count <integer> top <node pointer> end stack node data <datatype> next <node pointer> end node count top Stack head structure data next Stack node structure

Stack Algorithms Create stack algorithm createStack ? count (a) Before Create top stack algorithm createStack Initializes metadata for a stack. stack.head = null stack.count = 0 Return end createStack count (b) After Create top stack

Push stack If (stack full) else end if Return successes end pushStack algorithm pushStack Insert (push) data into a new node in the liked list. Post data have been pushed in stack Return true if successful, false if memory overflow If (stack full) successes = false else allocate (newptr) newptr->data = data newptr->next = stack.top stack.top = newptr stack.count = stack.count + 1 successes = true end if Return successes end pushStack Push stack

Pop stack If (stack empty) else end if return successes end popStack algorithm popStack This algorithm pops the item on the top of the stack and returns it to the user Post data have been returned to calling algorithm Return true if successful, false if underflow If (stack empty) successes = false else dptr = stack.top dataout = stack.top->data stack.top = stack.top->next stack.count = stack.count – 1 Recycle (dptr) successes = true end if return successes end popStack Pop stack

Stack Top If (stack empty) else end if return successes end Stacktop algorithm Stacktop This algorithm receives the data from the top of the stack without changing the stack. Post data have been returned to calling algorithm Return true if data returned, false if underflow If (stack empty) successes = false else dataout = stack.top->data successes = true end if return successes end Stacktop

Empty Stack If (stack not empty) else end if return result algorithm emptyStack Determines if stack is empty and returns a Boolean. Post returns stack status Return Boolean, true: stack empty, false: stack contains data If (stack not empty) result = false else result = true end if return result end emptyStack

Full Stack If (memory available) else end if return result algorithm fullStack Determines if stack is full and returns a Boolean. Post returns stack status Return Boolean, true: stack full, false: stack is empty If (memory available) result = false else result = true end if return result end fullStack

Stack Count return (stack.count) end Stackcount algorithm Stackcount Returns the number of elements currently in stack. Post returns stack count Return integer count of number of elements in stack return (stack.count) end Stackcount

Destroy Stack Loop (stack.top not null) end loop Stack.count = 0 algorithm destroyStack This algorithm releases all nodes back to dynamic memory Loop (stack.top not null) temp = stack.top Stack.top = stack.top->next recycle (temp) end loop Stack.count = 0 return end destroyStack