Stacks LIFO C C B B B B A A A A A Push (A) Push (B) Push (C) Pop Pop.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

DATA STRUCTURES ( C++ ) This PPT is Dedicated to my inner controller AMMA BHAGAVAN-Oneness Founders. Developed by,EDITED BY, S.V.G.REDDY,M.Siva Naga Prasad,
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Stacks. COMP104 Slide 2 Stacks * A stack, S, is a data structure that supports: n push(x) make x the top element in stack S n Pop Remove the top item.
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.
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.
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.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
Stacks Using Linked Lists Here, we use the same concept of the stack but eliminate the MAXIMUM data items constraint. Since we shall be using Linked Lists.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
Summary of lectures (1 to 11)
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.
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.
Call Stacks John Keyser. Stacks A very basic data structure. – Data structure: a container for holding data in a program. – Classes, structs can be thought.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Reference: Vinu V Das, Principles of Data Structures using C and C++
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
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,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
FIST, Multi Media University Lecture 5 Stack (Array Implementation) Queue (Array Implementation )
Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
Fall 2006 METU EEEEE 441 S. Ece (GURAN) SCH MIDT EE 441 Data Structures Lecture 6 Stacks and Queues.
Circular Linked List Singly Circular Linked List Doubly Circular Linked List.
STACK Data Structure
LINEAR LINKED LISTS The disadvantages of arrays: 1.The size of the array is fixed. 2.Large size of array??? 3. Inserting and deleting elements. If the.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Lecture 16 Stacks and Queues Richard Gesick. Sample test questions 1.Write a definition for a Node class that holds a number. 2.Write a method that sums.
STACKS & QUEUES for CLASS XII ( C++).
Stack: a Linked Implementation
Stacks.
Stacks Chapter 5.
MEMORY REPRESENTATION OF STACKS
Doubly Linked List Review - We are writing this code
Data Structures and Algorithms
Dr. Bernard Chen Ph.D. University of Central Arkansas
Stacks.
Problems with Linked List (as we’ve seen so far…)
Stacks.
Stack and Queue APURBO DATTA.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Pointers and Linked Lists
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
STACK By:- Rajendra ShakyawalP.G.T. Computer Science KV-No.1, AFS, Tambaram, Chennai.
Data Structures and Database Applications Stacks in C#
More Data Structures (Part 1)
Popping Items Off a Stack Lesson xx
Stack and Queues Stack implementation using Array
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
CSE 214 – Computer Science II Stacks
Queues FIFO Enqueue Dequeue Peek.
Data Structures and Algorithms
Stacks and Queues 1.
Recall: stacks and queues
Stacks and Queues Prof. Michael Tsai 2017/02/21.
More Data Structures (Part 1)
UNIT-II.
Stacks CS-240 Dick Steflik.
Lecture 16 Stacks and Queues CSE /26/2018.
Lists.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Lecture 16 Stacks and Queues CSE /26/2018.
Abstract Data Types Stacks CSCI 240
Stack Implementations
Presentation transcript:

Stacks LIFO C C B B B B A A A A A Push (A) Push (B) Push (C) Pop Pop

Array-based implementation array A; the variable top (-1…(capacity-1)); the capacity.

Linked List-based implementation struct STACK { int dann; STACK *prev; }; STACK *top=NULL;

Operations Push a new element onto the stack prev 3 q prev 2 q top void push(int a) {STACK *q; q=new STACK; q->prev=top;   q->dann=a; top=q; } // new q points to the stack struct // set q.prev field to the top // (add a new node to the top of the stack) // store data in the new node // set top to point to the new node prev 3 q prev 2 q top prev 1 q NULL

Remove the top element from the stack top prev 4 temp=4 Remove the top element from the stack top int pop(void) {STACK *q; int temp=-1; if (!top) cout << "\nEmpty"; else {temp=top->dann; q=top->prev; delete top; top=q; } return temp;   // store the top node data // the q holds the position of the // next node in the stack // the top is deleted // set top equal to the q prev 4 prev 3 q prev 2 prev 1 NULL

temp=4 Return the top element value int peek (void) {int temp; if (!top) { cout << "\nEmpty"; return -1; } temp=top->dann; return temp; top prev 4 prev 3 prev 2 prev 1 NULL

top Destroying the stack prev 4 prev 3 q prev 2 prev 1 NULL void clear (void) {STACK *q; while (top) { q=top->prev;   delete top; top=q; } // while the top pointer is not equal to the NULL // the q pointer is used to hold the position // of the next node in the stack // the top is deleted // set top equal to the q prev 3 q prev 2 prev 1 NULL

Stack applications Reverse a word. Palindrome strings “A man, a plan, a canal—Panama!” “Able I was ere, I saw Elba.” “Won ton? Not now!” “Madam, I’m Adam.” “Eve.”