Stacks.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Stacks.
Stacks, Queues, and Linked Lists
COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues.
Stacks. 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
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.
Introduction to Stacks What is a Stack Stack implementation using arrays. Application of Stack.
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.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
Chapter 7 Stacks II CS Data Structures I COSC 2006
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
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,
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.
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
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,
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
Lecture6: Stacks Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
Click to edit Master text styles Stacks Data Structure.
Stacks Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
CSCE 3110 Data Structures & Algorithm Analysis
Stacks (and Queues).
Stacks.
Stacks Stacks.
Stacks and Queues Chapter 4.
CSCI 3333 Data Structures Stacks.
Stacks.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Stack and Queue APURBO DATTA.
Stack.
CSCE 3110 Data Structures & Algorithm Analysis
CMSC 341 Lecture 5 Stacks, Queues
CSCE 3110 Data Structures & Algorithm Analysis
Stacks.
Lecture 5 Stacks King Fahd University of Petroleum & Minerals
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Stacks 12/7/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
ADT list.
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Abstract Data Type Abstract Data Type as a design tool
Stacks and Queues DSA 2013 Stacks n Queues.
Stacks.
UNIT-II.
Stacks CS-240 Dick Steflik.
Introduction to Stacks
Introduction to Stacks
Stacks, Queues, and Deques
A type is a collection of values
Stacks and Linked Lists
CHAPTER 3: Collections—Stacks
Presentation transcript:

Stacks

Overview of Stacks Stack of books Stack of plates Stack of CD’s A stack is restricted list in which items are inserted or deleted only at the top of the list. . Example Stack of books Stack of plates Stack of CD’s

Overview of Stacks Operations are allowed only at one point of the structure, normally termed the “top” of the stack. The stack’s storage policy is Last-In, First-Out (LIFO). This means that the last element inserted will be the first to be removed. pop pop top push 85 top 85 230 -20 100 230 push -20 100

Stacks – As a conceptual aids Stack is created to carry out particular task during the operation of a program rather than data storage. Lifetime short – discarded when operation completed Stack interface – to enforce restricted access to other items. Only one item either to be read or remove at a given time. More abstract than arrays – defined with certain permissible operations and not visible to user. Only the top element of a stack is visible, therefore the number of operations performed by a stack are few… Inspect the top element Retrieve the top element Push a new element on the stack Test for an empty stack

Application of Stacks Stacks are used in many areas, which include the following: Page-Visited history in a web browser. Undo sequence in a text editor. Evaluation of Expression. Matching of Parentheses. Chain of method calls in JAVA virtual machine.

Stack Operations Stack operations are: Push -> inserts an element on to top of stack Pop -> deletes an element from the top of the stack

Stack Operations Stack operations are: Name: Mohammed Rafi Couse Code: MIS201 Title: Data and Information Structures Stack operations are: Push -> inserts an element on to top of stack Pop -> deletes an element from the top of the stack isFull() is False for this Stack. isEmpty() is False for this Stack and Size() is 2.

Implementation of Stacks Stacks can be implemented in two ways: Using arrays Using Linked Lists

Using Arrays First the size has to be defined. For example: take size of stack is 6. Size of the stack is 6 push(10) push(-5) 10 -5 10 top top

Using Arrays size() = 3 Elements push(100) push(55) push(-20) push(25) isfull() = False isempty() = False top() = 100 push(100) 100 -5 10 push(55) top push(-20) 55 -20 100 -5 10 -20 100 -5 10 push(25) top size() = 6 Elements isfull() = True isempty() = False top() = 25 25 55 -20 100 -5 10 top top

Using Arrays push(75) pop() pop() pop() size() = 6 Elements isfull() = True isempty() = False top() = 25 No space is available, this state is called overflow. push(75) 25 55 -20 100 -5 10 top pop() pop() pop() 55 -20 100 -5 10 -20 100 -5 10 100 -5 10 top top top size() = 3 Elements isfull() = Fasle isempty() = False top() = 100

Using Arrays pop() push(75) pop() pop() pop() size() = No Elements 100 -5 10 75 100 -5 10 top top pop() pop() pop() -5 10 10 top top size() = No Elements isfull() = Fasle isempty() = True top() = Error This state is called underflow.

Stacks - Exceptions Attempting the execution of an operation of ADT may sometimes cause an error condition, called an exception Exceptions are said to be “thrown” by an operation that cannot be executed In the Stack ADT, operations pop and top cannot be performed if the stack is empty Attempting the execution of pop or top on an empty stack throws an EmptyStackException.

Stacks - Exceptions Stack overflow Stack underflow The condition resulting from trying to push an element onto a full stack if(!stack.IsFull()) stack.Push(item); The condition resulting from trying to pop an empty stack. if(!stack.IsEmpty()) stack.Pop(item);

The Algorithm Using Array Inserting Algorithm: Deleting Algorithm: push(stack, top, item){ if(top == max – 1) Display “Stack Overflow” else top = top + 1 stack[top] = item } pop(stack, top){ if(top < 0) Display “Stack Uderflow” else item = stack[top] top = top – 1 return(item) }

Stacks – Evaluation Performance Each operation runs in time O(1) The time is not dependent on how many items are in the stack and is therefore very quick retrieval, no comparisons and moves necessary. Limitations The maximum size of the stack must be defined a priori and cannot be changed Trying to push a new element into a full stack causes an implementation-specific exception

Stacks – Evaluation Efficiency Push -> O(1) (Insertion is fast, but only at the top) Pop -> O(1) (Deletion is fast, but only at the top) Peek -> O(1) (Access is fast, but only at the top)

Stack LinkedList Implementation public class Node<T> extends Object { public T data; public Node<T> next; public Node () { data = null; next = null; }//constructor }

Stack LinkedList Implementation public class LinkListStack<T> extends Object { private Node<T> top; public LinkListStack () { top = null; } //constructor //operation public boolean isEmpty () { return top == null; } public boolean isFull () { return false; }

Stack LinkedList Implementation public T topE () { if(top.data==null) throw EmptyStackException return top.data; } public void push (T val) { Node n = new Node(val); n.next = top; top = n;

Stack LinkedList Implementation public T pop (){ if(top.data==null) throw EmptyStackException Node temp = top; top = top.next; return temp.data; }

Homewrk Implement stack (the array based stack ) given the following specification: The array based stack class should contain only the following ( max – top – the array itself) The classes should contain the following methods : -push(int e): inserts an element. -int pop(): removes and returns the last inserted element. -int topE(): returns the last inserted element without removing it (data). -boolean isEmpty(): indicates whether no elements are stored. -boolean isFull(): indicates whether the stack full or not.