Download presentation
Presentation is loading. Please wait.
1
Stacks
2
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
4
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
5
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
6
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.
7
Stack Operations Stack operations are:
Push -> inserts an element on to top of stack Pop -> deletes an element from the top of the stack
8
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.
9
Implementation of Stacks
Stacks can be implemented in two ways: Using arrays Using Linked Lists
10
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
11
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
12
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
13
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.
14
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.
15
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);
16
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) }
17
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
18
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)
19
Stack LinkedList Implementation
public class Node<T> extends Object { public T data; public Node<T> next; public Node () { data = null; next = null; }//constructor }
20
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; }
21
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;
22
Stack LinkedList Implementation
public T pop (){ if(top.data==null) throw EmptyStackException Node temp = top; top = top.next; return temp.data; }
23
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.