0% found this document useful (0 votes)
46 views

Sample - Bank Management System

This document discusses stacks and their implementation. It defines a stack as a list where insertions and deletions can only occur at one end, called the top. Fundamental stack operations are push, which inserts an element at the top, and pop, which removes the top element. Stacks follow the LIFO principle - the last element inserted is the first removed. The document discusses implementing stacks using arrays or linked lists. It provides details on how to program push and pop operations for an array-based stack implementation.

Uploaded by

Rizwan Ullah
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Sample - Bank Management System

This document discusses stacks and their implementation. It defines a stack as a list where insertions and deletions can only occur at one end, called the top. Fundamental stack operations are push, which inserts an element at the top, and pop, which removes the top element. Stacks follow the LIFO principle - the last element inserted is the first removed. The document discusses implementing stacks using arrays or linked lists. It provides details on how to program push and pop operations for an array-based stack implementation.

Uploaded by

Rizwan Ullah
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 9

DATA STRUCTURES

AND ALGORITHMS

Stack
What will be discussed
 Stack Overview
 Stack Operations
 Implementation of Stack
Stack Overview
 Stack ADT
 Basic operations of stack
 Pushing, popping etc.
 Implementations of stacks using
 array

 linked list
The Stack ADT
 A stack is a list with the restriction
 that insertions and deletions can only be performed at the top of the
list

 The other end is called bottom


 Fundamental operations:
 Push: Equivalent to an insert

 Pop: Deletes the most recently inserted element

 Top: Examines the most recently inserted element


Stack ADT
 Stacks are known as LIFO (Last In, First Out) lists.
 The last element inserted will be the first to be
retrieved
Push and Pop

 Primary operations: Push and Pop


 Push
 Add an element to the top of the stack
 Pop
 Remove the element at the top of the stack

empty stack push an element push another pop

top
B
top top
A A A
top
Implementation of Stacks
 Any list implementation could be used to implement
a stack
 Arrays (static: the size of stack is given initially)
 Linked lists (dynamic: never become full)

 We will explore implementations based on array


and linked list
 Let’s see how to use an array to implement a stack
first
Array Implementation
 Need to declare an array size ahead of time
 Associated with each stack is TopOfStack
 for an empty stack, set TopOfStack to -1

 Push
 (1) Increment TopOfStack by 1.

 (2) Set Stack[TopOfStack] = X

 Pop
 (1) Set return value to Stack[TopOfStack]

 (2) Decrement TopOfStack by 1

 These operations are performed in very fast constant time


Conclusion
In this Lecture we discussed…
 What is an ADT Stack?
 What are the operations of Stack?
 How Stack can be implemented?

ANY QUERY ?

You might also like