SlideShare a Scribd company logo
Data Structure
Lecture No. 6
Stack
Engr. Rashid Farid Chishti
http://youtube.com/rfchishti
http://sites.google.com/site/chishti
International Islamic University H-10, Islamabad, Pakistan
Video Lecture
 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
Stacks Operations
push(2)
top 2
push(5)
2
5
push(7)
top
2
5
7
push(1)
top
2
5
7
1
1 pop()
top
2
5
7
push(21)
top
2
5
7
21
21 pop()
top
2
5
7
7 pop()
2
5
top
5 pop()
2
top
top
top
 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.
Stacks Operations
#include <iostream>
using namespace std;
class Stack{
private :
enum { MAX = 3 };
int arr[MAX];
int top;
public :
Stack() { top = -1; }
int Top() { return arr[top]; }
bool IsEmpty(){ return top == -1 ;}
bool IsFull() { return top == MAX-1 ;}
int Pop(){
if(IsEmpty()){
cout << "Error: Stack is Emptyn";
return -1;
}
else
5
Example 1: Stack Using Array
return arr[top--];
}
void Push(int x){
if ( IsFull() )
cout << "Error: Stack is Fulln";
else{
arr[++top] = x;
}
}
};
int main( ){
Stack S; S.Push(11); S.Push(22);
S.Push(33); S.Push(44);
cout << S.Pop() << endl;
cout << S.Pop() << endl;
cout << S.Pop() << endl;
cout << S.Pop() << endl;
system("PAUSE"); return 0; }
1 2
 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
Stack using an array. Stack using a Linked List.
Stack Using Linked List
top
2
5
7
1
1 7 5 2
head
int pop(){
current = head ;
Data = current -> data ;
head = head -> next ;
delete current ;
return Data ;
}
void Push ( int Data ) {
node *newNode = new node ;
newNode -> data = Data ;
newNode -> next = head ;
head = newNode ;
}
Stack Operations
1 7 5 2
head
9 5 2
head
push(9)
newNode 7
current
Data = 1
#include <iostream>
using namespace std;
typedef int Type;
struct Node{
Type data ;
Node * next ;
};
class Stack{
private :
Node* head;
public :
Stack( );
bool Is_Empty();
Type Top();
void Push ( Type Data );
Type Pop ( );
~Stack( ) ;
};
9
Example 2: Stack Using Linked List
Stack::Stack( ){
head = NULL;
}
bool Stack::Is_Empty() {
return head == NULL;
}
Type Stack::Top() {
if ( !Is_Empty() )
return head->data;
return -1;
}
void Stack :: Push ( Type Data ) {
Node *newNode ;
newNode = new Node ;
if ( newNode == NULL ){
cout << endl << "Stack is full" ;
return;
}
1 2
newNode -> data = Data ;
newNode -> next = head ;
head = newNode ;
}
Type Stack :: Pop( ) {
if ( Is_Empty() ){
cout << "Stack is empty " ;
return -1 ;
}
Node *current ;
Type Data ;
current = head ;
Data = current -> data ;
head = head -> next ;
delete current ;
return Data ;
}
10
Example 2: Stack Using Linked List
Stack :: ~Stack( ){
Node *current ;
while ( head != NULL ){
current = head ;
head = head -> next ;
delete current ;
}
}
int main( ){
Stack s ;
s.Push (11); s.Push (22); s.Push (33);
cout << s.Pop() << endl;
cout << s.Pop() << endl;
cout << s.Pop() << endl;
cout << s.Pop() << endl;
system("PAUSE");
return 0;
}
3 4
 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.
Stack: Array or List Linked List
 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
Applications of Stack

More Related Content

Similar to Data Structures and Agorithm: DS 06 Stack.pptx

04 stacks
04 stacks04 stacks
04 stacks
Rajan Gautam
 
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.pptlecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
partho5958
 
Data Structure.pptx
Data Structure.pptxData Structure.pptx
Data Structure.pptx
SajalFayyaz
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
AliRaza899305
 
C++ Program It is only 1 rotation. Up-rotation of a stack. Write a.pdf
C++ Program It is only 1 rotation. Up-rotation of a stack.  Write a.pdfC++ Program It is only 1 rotation. Up-rotation of a stack.  Write a.pdf
C++ Program It is only 1 rotation. Up-rotation of a stack. Write a.pdf
pallavi953613
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
Rajkiran Nadar
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
DurgaDeviCbit
 
Stack
StackStack
Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applications
Ahsan Mansiv
 
Algo>Stacks
Algo>StacksAlgo>Stacks
Algo>Stacks
Ain-ul-Moiz Khawaja
 
Stack and queue
Stack and queueStack and queue
Stack and queue
Katang Isip
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacks
maamir farooq
 
MO 2020 DS Stacks 1 AB.ppt
MO 2020 DS Stacks 1 AB.pptMO 2020 DS Stacks 1 AB.ppt
MO 2020 DS Stacks 1 AB.ppt
shashankbhadouria4
 
Stack queue
Stack queueStack queue
Stack queue
Tony Nguyen
 
Stack queue
Stack queueStack queue
Stack queue
James Wong
 
Stack queue
Stack queueStack queue
Stack queue
Harry Potter
 
Stack queue
Stack queueStack queue
Stack queue
Young Alista
 
Stack queue
Stack queueStack queue
Stack queue
Fraboni Ec
 
Stack queue
Stack queueStack queue
Stack queue
Luis Goldster
 
Stack queue
Stack queueStack queue
Stack queue
Hoang Nguyen
 

Similar to Data Structures and Agorithm: DS 06 Stack.pptx (20)

04 stacks
04 stacks04 stacks
04 stacks
 
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.pptlecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
 
Data Structure.pptx
Data Structure.pptxData Structure.pptx
Data Structure.pptx
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
C++ Program It is only 1 rotation. Up-rotation of a stack. Write a.pdf
C++ Program It is only 1 rotation. Up-rotation of a stack.  Write a.pdfC++ Program It is only 1 rotation. Up-rotation of a stack.  Write a.pdf
C++ Program It is only 1 rotation. Up-rotation of a stack. Write a.pdf
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
Stack
StackStack
Stack
 
Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applications
 
Algo>Stacks
Algo>StacksAlgo>Stacks
Algo>Stacks
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacks
 
MO 2020 DS Stacks 1 AB.ppt
MO 2020 DS Stacks 1 AB.pptMO 2020 DS Stacks 1 AB.ppt
MO 2020 DS Stacks 1 AB.ppt
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 

More from RashidFaridChishti

Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docx
RashidFaridChishti
 
Object Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docxObject Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docx
RashidFaridChishti
 
Lab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docxLab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docx
RashidFaridChishti
 
Data Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptxData Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptxData Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptxData Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptxData Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptxData Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptxData Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptxData Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptxData Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptxData Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptxData Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptxData Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptxData Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptx
RashidFaridChishti
 
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptxObject Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
RashidFaridChishti
 
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxObject Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
RashidFaridChishti
 
Object Oriented Programming using C++: Ch10 Pointers.pptx
Object Oriented Programming using C++: Ch10 Pointers.pptxObject Oriented Programming using C++: Ch10 Pointers.pptx
Object Oriented Programming using C++: Ch10 Pointers.pptx
RashidFaridChishti
 

More from RashidFaridChishti (20)

Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docx
 
Object Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docxObject Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docx
 
Lab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docxLab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docx
 
Data Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptxData Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptx
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
 
Data Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptxData Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptx
 
Data Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptxData Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptx
 
Data Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptxData Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptx
 
Data Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptxData Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptx
 
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptxData Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
 
Data Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptxData Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptx
 
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptxData Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
 
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptxData Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
 
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptxData Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
 
Data Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptxData Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptx
 
Data Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptxData Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptx
 
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptxObject Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
 
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxObject Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
 
Object Oriented Programming using C++: Ch10 Pointers.pptx
Object Oriented Programming using C++: Ch10 Pointers.pptxObject Oriented Programming using C++: Ch10 Pointers.pptx
Object Oriented Programming using C++: Ch10 Pointers.pptx
 

Recently uploaded

basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
nooriasukmaningtyas
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
mahammadsalmanmech
 
Recycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part IIRecycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part II
Aditya Rajan Patra
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
Rahul
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
mamunhossenbd75
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
co23btech11018
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
camseq
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
jpsjournal1
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
KrishnaveniKrishnara1
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball playEric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
enizeyimana36
 

Recently uploaded (20)

basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
 
Recycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part IIRecycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part II
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball playEric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
 

Data Structures and Agorithm: DS 06 Stack.pptx

  • 1. Data Structure Lecture No. 6 Stack Engr. Rashid Farid Chishti http://youtube.com/rfchishti http://sites.google.com/site/chishti International Islamic University H-10, Islamabad, Pakistan Video Lecture
  • 2.  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
  • 3. Stacks Operations push(2) top 2 push(5) 2 5 push(7) top 2 5 7 push(1) top 2 5 7 1 1 pop() top 2 5 7 push(21) top 2 5 7 21 21 pop() top 2 5 7 7 pop() 2 5 top 5 pop() 2 top top top
  • 4.  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. Stacks Operations
  • 5. #include <iostream> using namespace std; class Stack{ private : enum { MAX = 3 }; int arr[MAX]; int top; public : Stack() { top = -1; } int Top() { return arr[top]; } bool IsEmpty(){ return top == -1 ;} bool IsFull() { return top == MAX-1 ;} int Pop(){ if(IsEmpty()){ cout << "Error: Stack is Emptyn"; return -1; } else 5 Example 1: Stack Using Array return arr[top--]; } void Push(int x){ if ( IsFull() ) cout << "Error: Stack is Fulln"; else{ arr[++top] = x; } } }; int main( ){ Stack S; S.Push(11); S.Push(22); S.Push(33); S.Push(44); cout << S.Pop() << endl; cout << S.Pop() << endl; cout << S.Pop() << endl; cout << S.Pop() << endl; system("PAUSE"); return 0; } 1 2
  • 6.  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
  • 7. Stack using an array. Stack using a Linked List. Stack Using Linked List top 2 5 7 1 1 7 5 2 head
  • 8. int pop(){ current = head ; Data = current -> data ; head = head -> next ; delete current ; return Data ; } void Push ( int Data ) { node *newNode = new node ; newNode -> data = Data ; newNode -> next = head ; head = newNode ; } Stack Operations 1 7 5 2 head 9 5 2 head push(9) newNode 7 current Data = 1
  • 9. #include <iostream> using namespace std; typedef int Type; struct Node{ Type data ; Node * next ; }; class Stack{ private : Node* head; public : Stack( ); bool Is_Empty(); Type Top(); void Push ( Type Data ); Type Pop ( ); ~Stack( ) ; }; 9 Example 2: Stack Using Linked List Stack::Stack( ){ head = NULL; } bool Stack::Is_Empty() { return head == NULL; } Type Stack::Top() { if ( !Is_Empty() ) return head->data; return -1; } void Stack :: Push ( Type Data ) { Node *newNode ; newNode = new Node ; if ( newNode == NULL ){ cout << endl << "Stack is full" ; return; } 1 2
  • 10. newNode -> data = Data ; newNode -> next = head ; head = newNode ; } Type Stack :: Pop( ) { if ( Is_Empty() ){ cout << "Stack is empty " ; return -1 ; } Node *current ; Type Data ; current = head ; Data = current -> data ; head = head -> next ; delete current ; return Data ; } 10 Example 2: Stack Using Linked List Stack :: ~Stack( ){ Node *current ; while ( head != NULL ){ current = head ; head = head -> next ; delete current ; } } int main( ){ Stack s ; s.Push (11); s.Push (22); s.Push (33); cout << s.Pop() << endl; cout << s.Pop() << endl; cout << s.Pop() << endl; cout << s.Pop() << endl; system("PAUSE"); return 0; } 3 4
  • 11.  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. Stack: Array or List Linked List
  • 12.  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 Applications of Stack