0% found this document useful (0 votes)
15 views12 pages

DS 06 Stack

Stack

Uploaded by

kinshahra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views12 pages

DS 06 Stack

Stack

Uploaded by

kinshahra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Data Structure

Video Lecture

Lecture No. 6

Stack
Engr. Rashid Farid Chishti
http://youtube.com/rfchishti
http://sites.google.com/site/chis
hti International Islamic University H-10, Islamabad, Pakistan
Stacks
 Stacks in real life: stack of books, stack of plates
 Add new items at the top
 Remove an item at the top
 Stack data structure similar to real life: collection of elements arranged in a
linear order.
 Can only access element at the top

Stack Operations
 Push(X) – insert X as the top element of the stack
 Pop() – remove the top element of the stack and return it.
 Top() – return the top element without removing it from the stack.
Stacks Operations

top 1
top 7 7
top 5 5 5
top 2 2 2 2
top push(2) push(5) push(1)
push(7)

top 21
top 7 top 7
7
5 5 5 top 5
2 2 2 2 top 2

1 pop() push(21) 21 pop() 7 pop() 5 pop()


Stacks Operations
 The last element to go into the stack is the first to come out:
 LIFO – Last In First Out.
 Implement push() to push data on stack and pop() to get data from stack.

 What happens if we call pop() and there is no element?


 Have bool IsEmpty() function that returns true if stack is empty, false otherwise.

 What happens if we stack is full and we call push() function.


 Have a bool IsFull() function, which returns true is stack (or array) is full, false
otherwise.
Example 1: Stack Using Array
#include <iostream> return arr[top--];
using namespace std; }
class Stack{ void Push(int x){
private : if ( IsFull() )
enum { MAX = 3 }; cout << "Error: Stack is Full\n";
int arr[MAX]; else{
int top; arr[++top] = x;
public : }
Stack() { top = -1; } }
int Top() { return arr[top]; } };
bool IsEmpty(){ return top == - int main( ){
1 ;} Stack S; S.Push(11); S.Push(22);
bool IsFull() { return top == MAX- S.Push(33); S.Push(44);
1 ;} cout << S.Pop() << endl;
int Pop(){ cout << S.Pop() << endl;
if(IsEmpty()){ cout << S.Pop() << endl;
cout << "Error: Stack is Empty\n"; cout << S.Pop() << endl;
return -1; system("PAUSE"); return 0; }
}
else
1 5 2
Stack Using Linked List
 We can avoid the size limitation of a stack implemented with an array by using
a linked list to hold the stack elements.
 As with array, however, we need to decide where to insert elements in the list
and where to delete them so that push() and pop() will run the fastest.
 For a singly-linked list, inserting data at start using head pointer takes constant
time and inserting data at end using the current pointer also takes a constant
time.
 Removing an element at the start is constant time but removal at the end
required traversing the list to the node one before the last.
 So it make sense to place stack elements at the start of the list because insert
and removal are constant time.
Stack Using Linked List

top 1 head

7
1 7 5 2
5
2

Stack using an array. Stack using a Linked List.


Stack Operations
int pop(){
current = head ; Data = 1 head

Data = current -> data ; 1 7 5 2


head = head -> next ;
current
delete current ;
return Data ;
}

void Push ( int Data ) { head


node *newNode = new node ;
newNode 9 7 5 2
newNode -> data = Data ;
newNode -> next = head ;
head = newNode ;
} push(9)
Example 2: Stack Using Linked List
#include <iostream> Stack::Stack( ){
using namespace std; head = NULL;
typedef int Type; }
struct Node{ bool Stack::Is_Empty() {
Type data ; return head == NULL;
Node * next ; }
}; Type Stack::Top() {
class Stack{ if ( !Is_Empty() )
private : return head->data;
Node* head; return -1;
public : }
Stack( ); void Stack :: Push ( Type Data ) {
bool Is_Empty(); Node *newNode ;
Type Top(); newNode = new Node ;
void Push ( Type Data ); if ( newNode == NULL ){
Type Pop ( ); cout << endl << "Stack is full" ;
~Stack( ) ; return;
}; }

1 9 2
Example 2: Stack Using Linked List
newNode -> data = Data ; Stack :: ~Stack( ){
newNode -> next = head ; Node *current ;
head = newNode ; while ( head != NULL ){
} current = head ;
Type Stack :: Pop( ) { head = head -> next ;
if ( Is_Empty() ){ delete current ;
cout << "Stack is empty " ; }
return -1 ; }
} int main( ){
Node *current ;
Type Data ; Stack s ;
current = head ; s.Push (11); s.Push (22); s.Push (33);
Data = current -> data ; cout << s.Pop() << endl;
head = head -> next ; cout << s.Pop() << endl;
delete current ; cout << s.Pop() << endl;
return Data ; cout << s.Pop() << endl;
} system("PAUSE");
return 0;
}
3 10 4
Stack: Array or List Linked List
 Since both implementations support stack operations in constant time, any
reason to choose one over the other?
 Allocating and deallocating memory for list nodes does take more time than
pre-allocated array.
 Linked List uses only as much memory as required by the nodes; array requires
allocation ahead of time.
 Linked List Pointers (head, next) require extra memory.
 Array has an upper limit; List is limited by dynamic memory allocation.
Applications of Stack
 Infix expression into its postfix equivalent, or prefix equivalent so that We do
not need to maintain operator ordering, and parenthesis.
 After converting into prefix or postfix notations, we have to evaluate the
expression using stack to get the result.
 When we make a call from one function to the another function. The address
of the calling function gets stored in the Stack.
 Stack can be used for parenthesis checking in our code.
 Stack can be used to reverse a string by pushing all letters on stack and then
popping them out.
 We need stack in Backtracking algorithms so that we come back to the
previous state and go into some other paths.
 Page-visited history in a Web browser
 Undo sequence in a text editor

You might also like