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.

Slides:



Advertisements
Similar presentations
Page 11 Solutions to Practice Problems Using a Linked List From Previous Days Notes Create C functions to solve the following problems with linked lists.
Advertisements

Stacks, Queues, and Linked Lists
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.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
The Stack Data Structure. Classic structure What is a Stack? An abstract data type in which accesses are made at only one end Last In First Out (LIFO)
Circular Linked List. COMP104 Circular Linked List / Slide 2 Circular Linked Lists * A Circular Linked List is a special type of Linked List * It supports.
CS 240Chapter 6 - StacksPage 21 Chapter 6 Stacks The stack abstract data type is essentially a list using the LIFO (last-in-first-out) policy for adding.
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Sparse Matrices Stack Queue.
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.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks.
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.
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.
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
Stack Data Structure By : Imam M Shofi. What is stack? A stack is a limited version of an array. A stack is a limited version of an array. New elements,
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
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 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 11 – Data Structures.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Circular Linked List Singly Circular Linked List Doubly Circular Linked List.
CSC 205 – Java Programming II Lecture 30 April 3, 2002.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
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.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2a. Simple Containers: The Stack.
Programming Circular Linked List.
More on Linked Lists, Stacks, and Queues
Chapter 12 – Data Structures
Stacks and Queues Chapter 4.
UNIT – I Linked Lists.
CC 215 Data Structures Stack ADT
Doubly Linked List Review - We are writing this code
Data Structures and Algorithms
Dr. Bernard Chen Ph.D. University of Central Arkansas
Stack and Queue APURBO DATTA.
Stack.
Programmazione I a.a. 2017/2018.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Chapter 16-2 Linked Structures
CSC 205 – Java Programming II
STACK By:- Rajendra ShakyawalP.G.T. Computer Science KV-No.1, AFS, Tambaram, Chennai.
Stack and Queues Stack implementation using Array
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Review & Lab assignments
Data Structures and Algorithms
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Pointers & Dynamic Data Structures
Stacks CS-240 Dick Steflik.
Stacks LIFO C C B B B B A A A A A Push (A) Push (B) Push (C) Pop Pop.
LAB#3 Stacks Nora Albabtin nora albabtin.
Abstract Data Types Stacks CSCI 240
Presentation transcript:

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 from stack S (and return its value). * A stack has the LIFO –Last in First out –property. AA B A B C A B A B D A B A Push(S,A)Push(S,B)Push(S,C)Pop(S,C)Push(S,D)Pop(S,D)Pop(S,A)

COMP104 Slide 3 Stack Operations * stackIsEmpty() //determine whether a stack is empty * Push() //add new item to a stack and return a //boolean value to indicate if the //insertion is successful * Pop() //remove most recent pushed item from stack //and return a pointer to the item * searchStack() //get the top item from top of stack //without changing the stack content

COMP104 Slide 4 Stack Application * Recognition of “balanced braces”. * A sequence of braces }, { is balanced if n Each time a “}” is encountered it matches a previously encountered “{“ n When reaching the end of the string, every “{“ is matched. * Examples {{ } { }}{ } balanced { } { }}{ } not balanced

COMP104 Slide 5 A linked list implementation * A stack can be implemented as a special type of linked list where: n New items are always inserted to the head of the linked list n The remove operation always removes the first node of the linked list * Empty Stack * Push Stack 20 pTop newPtr

COMP104 Slide 6 A Linked list Implementation * PushStack //same as the insertion to the head of linked //lists. 20 pTop newPtr pNew->next = pTop; pTop = pNew;

COMP104 Slide 7 A Linked List Implementation * Pop Node //same as delete the first node from linked list pTop = pTop -> next; (to delete) pTop

#include using namespace std; struct Node{ int data; Node* next; }; bool stackIsEmpty(Node* pTop){ return (pTop == NULL); } void pushStack(Node* &pTop, int item){ Node* pNew; pNew = new Node; if(pNew == NULL){ cout << "Failed at Memory Allocation" << endl; return; } pNew->data = item; pNew->next = pTop; pTop = pNew; }

void printStack(Node *pTop){ Node* pCur = pTop; while(pCur != NULL){ cout data << " "; pCur = pCur->next; } cout << endl; } Node* searchStack(Node *pTop, int item){ Node *pCur = pTop; while(pCur != NULL){ if(pCur->data == item) break; pCur = pCur->next; } return pCur; }

Node* popStack(Node* &pTop){ Node* pCur = NULL; if(!stackIsEmpty(pTop)){ pCur = pTop; pTop = pTop -> next; } return pCur; }

void main(){ Node *pTop = NULL; pushStack(pTop, 3); pushStack(pTop, 1); pushStack(pTop, -5); pushStack(pTop, 7); printStack(pTop); popStack(pTop); printStack(pTop); pushStack(pTop, 16); pushStack(pTop, -51); pushStack(pTop, 27); printStack(pTop); popStack(pTop); printStack(pTop); } l Result is