37 views

Uploaded by Cindy Jones

stack ADT

- 1.Principles of Security and Integrity of Databases
- DSA_LAB_1-6%268-10%281%29
- Data Structures Lab 15CSL38
- Linked Lists
- 22_parallelprog
- Data Structures slides
- mcqs
- Spicework API Reference Version
- Microprocessor Note
- ARM Chap 3_last
- A Programming Model for Business Applications reference
- Data Structure
- Php
- PIC 16f506
- DataStructures Notes (1)
- Data Structures
- load_002.txt
- DSLAB
- Link List
- modula 2.1

You are on page 1of 16

(1)

Stack ADT

Dr Muhammad Hussain

(2)

Stack ADT

An ADT that manages data elements linearly but provides access at only one end i.e. data elements can be inserted and removed from one end only. insert (Push) remove (pop)

34 14 23 8 6 2

34 14 23 8 6 2 14 23 8 6 2 34

NOTE: In Stack ADT, the elements are removed only in the inverse order of f th their i arrival i li i.e. th the last l t inserted i t d element l t is i removed d first fi t of f all. ll So, it has lastlast-in/firstin/first-out (LIFO) behavior.

Dr Muhammad Hussain Lecture - Stack ADT

(3)

There are many practical situations which can be best handled using stack The p papers p put p in the tray y of a p printer. The p papers p from the tray are removed by the printer in reverse order they are put in the tray. Stack is used to evaluate expressions like 10 10x( (12+ 12+7) A runtime system uses a stack to handle function calls and return. return

Dr Muhammad Hussain

(4)

Elements: Any valid data type Elements: Structure: Structure : An arrangement of elements that allows to remove first of all the element that is stored last of all Domain: Domain : Number of elements is bounded Operations :

Operation boolean empty() boolean full() Specification Precondition/Requires: none Processing/Results: returns true if the stack is empty otherwise false. Precondition/Requires: none Processing/Results: returns true if no more elements can be inserted into the stack otherwise false. Precondition/Requires: stack is not full Processing/Results: inserts a given element into the stack at the top and top points to it. Precondition/Requires: stack is not empty Processing/Results: the element at the top of the stack is removed from the stack and returned.

Lecture - Stack ADT

void push(Type)

Type pop()

Dr Muhammad Hussain

(5)

Stack ADT can be represented as - Array - Linked List

0 1 2 3

n -1 1

Array

Dr Muhammad Hussain Lecture - Stack ADT

(6)

public class ArrayStack ArrayStack<T> { //Data Members private int maxsize; private i t int i t top; t private T[] nodes; // Operations public ArrayStack(int n) Public boolean empty() public boolean full() public void push(T e) public T pop() }

Dr Muhammad Hussain Lecture - Stack ADT

(7)

Implementation of Operations

public ArrayStack(int n) { maxsize = n; top = -1; nodes = (T[]) new Object[n]; } public boolean empty() { return top == -1; }

Dr Muhammad Hussain

(8)

Implementation of Operations

public boolean full() { return top == maxsize - 1; } public void push(T e) { if(full()) return; top++; nodes[top] d [t ] = e; }

Dr Muhammad Hussain

(9)

Implementation of Operations

public T pop(){ if(!empty()) return nodes[top--]; }

Dr Muhammad Hussain

(10)

public class LinkStack LinkStack<T> { // Data Memebrs private Node<T> top; // Operations public LinkStack() public boolean empty() public boolean full() public void push(T e) p blic T pop() public }

Dr Muhammad Hussain

(11)

Implementation of Operations

public LinkStack() { top = null; } public boolean empty() { return t top t == null; ll } public bli b boolean l f ll() full { return false; }

Dr Muhammad Hussain Lecture - Stack ADT

(12)

Implementation of Operations

public void push(T e) { Node<T> tmp = new Node(e); tmp.next = top; top = tmp; } public T pop() { Node<T> tmp = top; T e = top.data; top = top.next; return e; }

Dr Muhammad Hussain Lecture - Stack ADT

(13)

Performance Comparison

Array Based Implementation - An array y based implementation p suffers from the drawback that all the storage space must be reserved in advance and the maximum depth of the stack is limited to this arrays size. - Time complexity of all stack operations is O(1). O(1) Linked List based Implementation - The time complexity of all operations is O(1). For applications in which the maximum stack size is known ahead of time, an array is suitable If the maximum stack size is not known beforehand beforehand, we can use a linked list

Dr Muhammad Hussain

(14)

Infix Expressions - An expression in which every binary operation appears between its operands Example: (i) a+b + is a binary operation and a and b are its operands (ii) (a+b) (a+b)*c c Prefix Expressions - An expression in which operator comes before its operands Example: (i) a+b +ab (ii) (a+b)*c *+abc (iii) a+(b*c) +a*bc Postfix Expressions - An expression in which operator comes after its operands Example: (i) a+b ab+ (ii) (a+b)*c ( +b)* ab+c* b+ * (iii) a+(b*c) abc*+

Dr Muhammad Hussain Lecture - Stack ADT

(15)

Some calculators require that the input expression be in postfix form Some computers accept expressions in infix form but convert them to postfix form

Problem: Convert the infix expression a - (b+c)*d into postfix Method: Peroform the following steps with a stack s write i aa push(s, -) p push(s, ( () write b ab push(s, +) write b abc When right parenthesis ( is found, pop(s, +) and write it abc+

Dr Muhammad Hussain Lecture - Stack ADT

(16)

pop(s, ( ) push(s, *) write d abc+d pop(s, *) write it abc+d* pop(s, pop(s -) ) write it abc+d abc+d* Exercise W it a function Write f ti that th t converts t an infix i fi expression i to t its it postfix form using a stack s and its operations given in the specification of stack ADT.

Dr Muhammad Hussain

- DSA_LAB_1-6%268-10%281%29Uploaded byVikneshwar Vicky
- Data Structures Lab 15CSL38Uploaded bytejaswini
- Linked ListsUploaded byapi-3801329
- mcqsUploaded byGaurav Khullar
- 1.Principles of Security and Integrity of DatabasesUploaded byCarlos A. Garcia
- 22_parallelprogUploaded bySrujan
- Spicework API Reference VersionUploaded byRajMohen
- A Programming Model for Business Applications referenceUploaded byGaneshkumar Sanur Gopalakrishnan
- Data Structures slidesUploaded byAlex Kraemer
- Microprocessor NoteUploaded byMd Isfaque
- Data StructureUploaded byWanja Waithaka
- DataStructures Notes (1)Uploaded byKande Archana K
- ARM Chap 3_lastUploaded byApurv Modi
- PhpUploaded byDaichi Shinigawa
- Data StructuresUploaded byPurva Paschima
- load_002.txtUploaded byNorina Takanori
- PIC 16f506Uploaded byKiran Kulkarni
- DSLABUploaded bykindrohit1989
- Link ListUploaded byHarsh Patel
- modula 2.1Uploaded byMohsan Naqi
- Accenture Interview Qustion Ans AnswerUploaded byRadheshyam Nayak
- LAB Manual 9Uploaded byrashid sharif
- 2008 Lab SyllabusUploaded byAshok Varadarajan
- 6.03_Apply_Parsing_for_validation_of_user_input_vb.pptxUploaded byShahanaz Shafiullah Khan
- documentationUploaded bysk_firdous
- CabotUploaded byregistrarse1974
- UTL PackageUploaded bydamodarvarde
- MIT6_01SCS11_chap02Uploaded byrnick2002ro
- 31 Thermochemical Data SourcesUploaded byalinori
- MIT15_082JF10_lec02Uploaded byeviroyer

- Anna university UG regulations 2013Uploaded bytskme
- is.13920.1993Uploaded bystructuralengineers
- GATE_CE_2011Uploaded byAbhinavMishra
- Diploma SyllabusUploaded byCindy Jones
- Circular Linked List.pptUploaded byCindy Jones
- UNITI SS.docUploaded byCindy Jones
- CE2303-QBUploaded byCindy Jones
- CE2303-NOLUploaded byCindy Jones
- project.docUploaded byCindy Jones
- Hypo SludgeUploaded byCindy Jones
- Engineering Economics and Cost AnalysisUploaded bydomda
- MIT6_701S10_notes.pdfUploaded byCindy Jones
- ELECTRICAL MACHINE DESIGN 3 1 0 4.docUploaded byCindy Jones
- ME 1403 Computer Integrated ManufacturingUploaded byDhileepan Kumarasamy
- abstracts_v3n1_Panel Discussion organizedUploaded byCindy Jones
- Linked ListsUploaded byapi-26091603
- Engineering physics I syllabus.docUploaded byCindy Jones
- Venturimeter & OrificemeterUploaded byRajib Das
- Energy EquipmentsUploaded byCindy Jones
- Taylor JournalsUploaded byCindy Jones

- 782 10682 y 782 10683Uploaded bytrbvm
- 6Uploaded byChakradhar Bezawada
- LED+REFLECTOR+JETAUploaded byCarlos Alberto Martínez Escobar
- Jerash Feasibility Report Draft Final SubmissionUploaded bynawajha
- 4051.pdfUploaded byyamaha640
- Optical embedded dust sensor for engine protection and early warning on M1 Abrams/ground combat vehiclesUploaded byRobert LaRue
- Katelyn KleinhenzUploaded byKaty Kleinhenz
- 11-2.3.15 Environmental ProtectionUploaded byFrancisco M. Ramos
- A nayakThesis.docUploaded byUtkarsh Dwivedi
- Bidprime v Smartprocure ComplaintUploaded byBen Miller
- Elbow Joint DislocationUploaded bystefanVas
- Mahb Ar16 Part 2Uploaded byaaiii
- order940_businessinstatistics.docxUploaded byVenus Marie Real-Catubay
- effective education use of edmodoUploaded byapi-239144588
- Seawater Line Design ReviewUploaded byMohammad Shahjahan
- pwc-10minutes-update-coso-committee-sponsoring-organizations-treadway-commission.pdfUploaded byWesley Charlotte
- Assessment Tool Dimension 2 Internal StakeholdersUploaded byMaydafe Cherryl Carlos
- PV10!14!20 Owners ManualUploaded byScridever
- Manual de pararrayos ABBUploaded byLuisAlbertoAlvaradoMontes
- Extra Problem1Uploaded byfestisio
- list of experimentsExperimentsUploaded byVenkatesan Sundaram
- Testing Considerations for Application Migration to Cloud ComputingUploaded byAbalone
- Prospectus Keam12Uploaded byShyam Sundar
- IP Rights Presentation - Patent (Final)Uploaded byGlargo Glargo
- MotorEffic&PF CM5Uploaded bySunil Singh
- BI Functional ComponentsUploaded byabacusdotcom
- Basics of WeldingUploaded byAmit Agarwal
- ACM Upload InstructionsUploaded byefnava
- 001 LTE Air Interface ISSUE 1Uploaded byEmerson Sneyder López Arias
- ECE 3 - 8 syllabusUploaded byEby Sam Stewart