SlideShare ist ein Scribd-Unternehmen logo
1 von 25
Data Structures

             Stack
Submitted to :
       Dr. Mohammed Belal
Data Structures & Algorithms


A data structure




Algorithms
Characteristics of Data Structures

Data Structure       Advantages              Disadvantages
                 Quick insertion, very       Slow search,
     Array       search,                     slow deletion, fixed size.
                 Fast access if index
                 known.
                 Quicker search than         Slow insertion and
 Ordered array   unsorted array.             deletion, fixed size.

                 Provides last-in, first-    Slow access to other
     Stack       out access.                 items.

                 Provides first-in, first-   Slow access to other
    Queue        out access.                 items.

  Linked list    Quick insertion ,quick      Slow search.
                 deletion.
Characteristics of Data Structures (cont’d)
Data Structure        Advantages             Disadvantages

                  Quick search, insertion,   Deletion algorithm is
  Binary tree     deletion (if tree is       complex.
                  complex.
                  remains balanced).
                  Quick search, insertion,   Complex.
 Red-black tree   deletion. Tree always
                  balanced.
                  Quick search, insertion,   Complex.
   2-3-4 tree     deletion. Tree always
                  balanced. Similar trees
                  good for disk storage.
                  Quick insertion ,quick     Slow deletion,
   Hash table     deletion.                  nefficient
                                             memory usage.
Characteristics of Data Structures (cont’d)

 Data Structure        Advantages              Disadvantages
                   Fast insertion, deletion,   Slow access to other
      Heap         Slow access to largest      items.
                   item.
                   Models real-world           Some algorithms are
      Graph        situations.                 slow and complex.



The data structures shown in Table , except the arrays,
can be thought of asAbstract Data Types, or ADTs.
Stack
A stack is a list like a structure in which all insertions
and deletions are made at one end, called the top.
The last element to be inserted into the stack will be
the first to be removed. Thus stacks are sometimes
referred to as Last In First Out (LIFO) lists.


Basic operations are push and pop.
Often top and isEmpty are available, too.
Also known as "last-in, first-out" or LIFO
                                                Top
Stack
A stack is a list like a structure in which all insertions
and deletions are made at one end, called the top.
The last element to be inserted into the stack will be
the first to be removed. Thus stacks are sometimes
referred to as Last In First Out (LIFO) lists.


Basic operations are push and pop.
Often top and isEmpty are available, too.
                                         Top                 B
Also known as "last-in, first-out" or LIFO
Stack
A stack is a list like a structure in which all insertions
and deletions are made at one end, called the top.
The last element to be inserted into the stack will be
the first to be removed. Thus stacks are sometimes
referred to as Last In First Out (LIFO) lists.


Basic operations are push and pop.
                                       Top                   C
Often top and isEmpty are available, too.
                                                             B
Also known as "last-in, first-out" or LIFO
Stack
A stack is a list like a structure in which all insertions
and deletions are made at one end, called the top.
The last element to be inserted into the stack will be
the first to be removed. Thus stacks are sometimes
referred to as Last In First Out (LIFO) lists.


Basic operations are push and pop.Top                        D
Often top and isEmpty are available, too.                    C
                                                             B
Also known as "last-in, first-out" or LIFO
Stack
A stack is a list like a structure in which all insertions
and deletions are made at one end, called the top.
The last element to be inserted into the stack will be
the first to be removed. Thus stacks are sometimes
referred to as Last In First Out (LIFO) lists.
                                 Top                         E
Basic operations are push and pop.                           D
Often top and isEmpty are available, too.                    C
                                                             B
Also known as "last-in, first-out" or LIFO
Stack Applications

  Real life

       Pile of books
       Plate trays
 More applications related to computer
science

       Program execution stack
       Evaluating expressions
Stack Operations
• Push(x)
insert the element x to the top of the stack
• Pop()
remove element from the top
• Top()
check the next element to be removed (do
not remove)
• IsEmpty()
check whether the stack is empty
Push Function :
                            Top
Top                 Top




 Algorithm push(o)
 if t = S.length − 1 then
 throw FullStackException
 else
 t←t+1
 S[t] ← o
Pop Function :

Top
                Top          Top




 Algorithm pop( )
 if isEmpty( ) then
 throw EmptyStackException
 else
 t←t−1
 return S[t + 1]
Top Function :
 Algorithm Top()        Top
 {
 Return s [ Top] ;
 }

Is Empty Function :
Algorithm Is Empty()

If S null
{
Return true ;
else
Return false ;
}                      Top   0
Stack Code For Java
class Stack X
{
Private int maxSize ;                      // size of stack array
Private long [] stackArray ;
Private int top ;                           // top of stack
--------------------------------------------------------------
Public Stack X ( int s )                         // constructor
{
maxSize = s ;                   // set array size
stackAr ray = new long [ maxSize ] ;                                // create array
 top = -1 ;                      // no items yet


--------------------------------------------------------------
public void push ( long j )                      // put item on top of stack
{
stackArray [ ++ top ] = j ;                  // increment top, insert item
Stack Code For Java (cont’d)
public long pop( )                                          // take item from top of stack

Return stackArray [ top-- ] ;                               // access item, decrement top

--------------------------------------------------------------
public long top( )                                           // top of stack

Return stackArray [ top ] ;

--------------------------------------------------------------
public boolean isEmpty( )                                    // true if stack is empty

return ( top == -1 ) ;

--------------------------------------------------------------
public boolean isFull( )                         // true if stack is full

return ( top == maxSize-1 ) ;

}                                               // end class StackX
Array-based Stack
Array-based Stack Implementation
 Allocate an array of some size (pre-defined)

  Maximum N elements in stack

 Bottom stack element stored at element 0

 last index in the array is the top

 Increment top when one element is pushed,
decrement after pop
Array-based Stack Implementation
Array-based Stack Implementation (cont’d)
Array-based Stack Implementation (cont’d)
Array-based Stack Implementation (cont’d)
Array-based Stack Implementation (cont’d)
Stack project

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Terminology of tree
Terminology of treeTerminology of tree
Terminology of tree
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Stack
StackStack
Stack
 
Queues
QueuesQueues
Queues
 
stack presentation
stack presentationstack presentation
stack presentation
 
Stack
StackStack
Stack
 
Linked list
Linked listLinked list
Linked list
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Data structure by Digvijay
Data structure by DigvijayData structure by Digvijay
Data structure by Digvijay
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Data structure Stack
Data structure StackData structure Stack
Data structure Stack
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
 
The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And Recursion
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturer
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
 
Stacks
StacksStacks
Stacks
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 

Andere mochten auch

Open stack glance
Open stack glanceOpen stack glance
Open stack glance宛儒 余
 
Openstack glance
Openstack glanceOpenstack glance
Openstack glanceSHAMEEM F
 
Glance image-cacheコマンドを使ってみた
Glance image-cacheコマンドを使ってみたGlance image-cacheコマンドを使ってみた
Glance image-cacheコマンドを使ってみたharubelle
 
Openstack study-nova-02
Openstack study-nova-02Openstack study-nova-02
Openstack study-nova-02Jinho Shin
 
第9回 OpenStack 勉強会(Glance)
第9回 OpenStack 勉強会(Glance)第9回 OpenStack 勉強会(Glance)
第9回 OpenStack 勉強会(Glance)Hiroki Ishikawa
 

Andere mochten auch (9)

OpenStack Glance
OpenStack GlanceOpenStack Glance
OpenStack Glance
 
Open stack glance
Open stack glanceOpen stack glance
Open stack glance
 
OpenStack Glance
OpenStack GlanceOpenStack Glance
OpenStack Glance
 
Openstack glance
Openstack glanceOpenstack glance
Openstack glance
 
OpenStack Icehouse構築手順書
OpenStack Icehouse構築手順書OpenStack Icehouse構築手順書
OpenStack Icehouse構築手順書
 
Glance image-cacheコマンドを使ってみた
Glance image-cacheコマンドを使ってみたGlance image-cacheコマンドを使ってみた
Glance image-cacheコマンドを使ってみた
 
Openstack study-nova-02
Openstack study-nova-02Openstack study-nova-02
Openstack study-nova-02
 
第9回 OpenStack 勉強会(Glance)
第9回 OpenStack 勉強会(Glance)第9回 OpenStack 勉強会(Glance)
第9回 OpenStack 勉強会(Glance)
 
MEAN Stack
MEAN StackMEAN Stack
MEAN Stack
 

Ähnlich wie Stack project

Ähnlich wie Stack project (20)

Stacks Data structure.pptx
Stacks Data structure.pptxStacks Data structure.pptx
Stacks Data structure.pptx
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
 
Lecture5
Lecture5Lecture5
Lecture5
 
Stacks in data structure
Stacks  in data structureStacks  in data structure
Stacks in data structure
 
5.-Stacks.pptx
5.-Stacks.pptx5.-Stacks.pptx
5.-Stacks.pptx
 
Stacks overview with its applications
Stacks overview with its applicationsStacks overview with its applications
Stacks overview with its applications
 
Stack & Queue
Stack & QueueStack & Queue
Stack & Queue
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
U3.stack queue
U3.stack queueU3.stack queue
U3.stack queue
 
5 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart25 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart2
 
Ds stack & queue
Ds   stack & queueDs   stack & queue
Ds stack & queue
 
2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS
 
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queue
 
Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9
 
Rana Junaid Rasheed
Rana Junaid RasheedRana Junaid Rasheed
Rana Junaid Rasheed
 
Stack data structure in Data Structure using C
Stack data structure in Data Structure using C Stack data structure in Data Structure using C
Stack data structure in Data Structure using C
 
Data Structures
Data StructuresData Structures
Data Structures
 

Kürzlich hochgeladen

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 

Kürzlich hochgeladen (20)

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 

Stack project

  • 1. Data Structures Stack Submitted to : Dr. Mohammed Belal
  • 2. Data Structures & Algorithms A data structure Algorithms
  • 3. Characteristics of Data Structures Data Structure Advantages Disadvantages Quick insertion, very Slow search, Array search, slow deletion, fixed size. Fast access if index known. Quicker search than Slow insertion and Ordered array unsorted array. deletion, fixed size. Provides last-in, first- Slow access to other Stack out access. items. Provides first-in, first- Slow access to other Queue out access. items. Linked list Quick insertion ,quick Slow search. deletion.
  • 4. Characteristics of Data Structures (cont’d) Data Structure Advantages Disadvantages Quick search, insertion, Deletion algorithm is Binary tree deletion (if tree is complex. complex. remains balanced). Quick search, insertion, Complex. Red-black tree deletion. Tree always balanced. Quick search, insertion, Complex. 2-3-4 tree deletion. Tree always balanced. Similar trees good for disk storage. Quick insertion ,quick Slow deletion, Hash table deletion. nefficient memory usage.
  • 5. Characteristics of Data Structures (cont’d) Data Structure Advantages Disadvantages Fast insertion, deletion, Slow access to other Heap Slow access to largest items. item. Models real-world Some algorithms are Graph situations. slow and complex. The data structures shown in Table , except the arrays, can be thought of asAbstract Data Types, or ADTs.
  • 6. Stack A stack is a list like a structure in which all insertions and deletions are made at one end, called the top. The last element to be inserted into the stack will be the first to be removed. Thus stacks are sometimes referred to as Last In First Out (LIFO) lists. Basic operations are push and pop. Often top and isEmpty are available, too. Also known as "last-in, first-out" or LIFO Top
  • 7. Stack A stack is a list like a structure in which all insertions and deletions are made at one end, called the top. The last element to be inserted into the stack will be the first to be removed. Thus stacks are sometimes referred to as Last In First Out (LIFO) lists. Basic operations are push and pop. Often top and isEmpty are available, too. Top B Also known as "last-in, first-out" or LIFO
  • 8. Stack A stack is a list like a structure in which all insertions and deletions are made at one end, called the top. The last element to be inserted into the stack will be the first to be removed. Thus stacks are sometimes referred to as Last In First Out (LIFO) lists. Basic operations are push and pop. Top C Often top and isEmpty are available, too. B Also known as "last-in, first-out" or LIFO
  • 9. Stack A stack is a list like a structure in which all insertions and deletions are made at one end, called the top. The last element to be inserted into the stack will be the first to be removed. Thus stacks are sometimes referred to as Last In First Out (LIFO) lists. Basic operations are push and pop.Top D Often top and isEmpty are available, too. C B Also known as "last-in, first-out" or LIFO
  • 10. Stack A stack is a list like a structure in which all insertions and deletions are made at one end, called the top. The last element to be inserted into the stack will be the first to be removed. Thus stacks are sometimes referred to as Last In First Out (LIFO) lists. Top E Basic operations are push and pop. D Often top and isEmpty are available, too. C B Also known as "last-in, first-out" or LIFO
  • 11. Stack Applications Real life  Pile of books  Plate trays  More applications related to computer science  Program execution stack  Evaluating expressions
  • 12. Stack Operations • Push(x) insert the element x to the top of the stack • Pop() remove element from the top • Top() check the next element to be removed (do not remove) • IsEmpty() check whether the stack is empty
  • 13. Push Function : Top Top Top Algorithm push(o) if t = S.length − 1 then throw FullStackException else t←t+1 S[t] ← o
  • 14. Pop Function : Top Top Top Algorithm pop( ) if isEmpty( ) then throw EmptyStackException else t←t−1 return S[t + 1]
  • 15. Top Function : Algorithm Top() Top { Return s [ Top] ; } Is Empty Function : Algorithm Is Empty() If S null { Return true ; else Return false ; } Top 0
  • 16. Stack Code For Java class Stack X { Private int maxSize ; // size of stack array Private long [] stackArray ; Private int top ; // top of stack -------------------------------------------------------------- Public Stack X ( int s ) // constructor { maxSize = s ; // set array size stackAr ray = new long [ maxSize ] ; // create array top = -1 ; // no items yet -------------------------------------------------------------- public void push ( long j ) // put item on top of stack { stackArray [ ++ top ] = j ; // increment top, insert item
  • 17. Stack Code For Java (cont’d) public long pop( ) // take item from top of stack Return stackArray [ top-- ] ; // access item, decrement top -------------------------------------------------------------- public long top( ) // top of stack Return stackArray [ top ] ; -------------------------------------------------------------- public boolean isEmpty( ) // true if stack is empty return ( top == -1 ) ; -------------------------------------------------------------- public boolean isFull( ) // true if stack is full return ( top == maxSize-1 ) ; } // end class StackX
  • 19. Array-based Stack Implementation  Allocate an array of some size (pre-defined) Maximum N elements in stack  Bottom stack element stored at element 0  last index in the array is the top  Increment top when one element is pushed, decrement after pop