SlideShare a Scribd company logo
CS261
DATA STRUCTURES & ALGORITHMS
(WEEK-5)
LECTURE-9 & 10
INTRODUCTION TO DATA STRUCTURES &
ALGORITHMS
Lecturer
Azka Aziz
Azka.a@scocs.edu.pk
Data structures & Algorithms
Lecture#09 Stacks
Course contents
Stack
Stack ADT
Stack Operations
Array Implementation
List Implementation
Applications
Sample Examples
stack
Stack is a data structure that stores data
items. Data items when removed follow a
particular order i.e. the item that was
added to stack most recently will be
removed first (Last In First Out)
stack
Stack items cannot be accessed randomly regardless
of underlying data storage e.g. Array, Linked List
A stack can be imagined as a cylinder with one end
closed
All items can be added and/or removed from same
end (that is open)
Adding an item onto stack is called ‘push operation’
Removing an item from stack is called ‘pop operation’
Stack ADT … (implementing with Array)
Stack Operations …
Operation Description Pseudocode
int inspect_top(); Returns element at ‘top’
with removing it from Stack
 If stack is non-empty, return the element immediately
beneath the ‘top’
 Return -1 (indicator of Stack being empty) otherwise
void push(int); Adds the new element onto
the Stack
 If Stack is full, mention it someway
 Otherwise
 Add new element to current position of ‘top’
 Proceed ‘top’ position by 1
int pop(); Remove and return the
element at ‘top’ from Stack
 If Stack is empty, mention it someway
 Otherwise
 Decrease ‘top’ by 1 (making currently top
element inaccessible for sub-sequent calls)
 Return element at ‘top’ position
Stack using array … implementation
Implementation details and issues have been discussed during
class
Stack ADT … (implementing with Linked List)
Stack Operations …
Operation Description Pseudocode
StackUsingList() A constructor that creates an
underlying empty linked list
Create new instance of underlying linked list as ‘data’
int size(); Returns the number of
elements in the stack
 Uses size() function from linked list instance ‘data’ to
return the number of elements in stack
int is_empty(); Returns TRUE if Stack
contains no element. It
returns FALSE otherwise.
 Uses is_empty() function from underlying linked
list instance ‘data’ to return whether stack is empty
or not
int inspect_top() Returns the stack’s top
element without removing it
 If ‘start’ of ‘data’ points to NULL
 It returns -1
 Otherwise
 It returns value of data item in ‘start’
Stack Operations …
Operation Description Pseudocode
void push(int key); Adds the new element
onto the Stack
 Creates new node using ‘key’
 Adds it to ‘start’ of underlying linked list setting the
next pointer appropriately
int pop(); Remove and return the
element at start of
underlying list
 If Stack is empty, return -1
 Otherwise
 Create a new node pointer ‘temp’ and point it
to start of underlying list
 Move ‘start’ of underlying list to one step
forward
 Set next of temp to NULL
 Return data value of temp;
Stack using list … implementation
Implementation details and issues have been discussed during
class
Stack Operations(algorithms)
{
Repeat while true
[Menu option]
Write(“1: push”)
Write(“2: pop”)
Write(“3: peep top”)
Write(“4: peep stack”)
Write(“5: exit”)
[selection of option]
Selsct option opt
Option 1:
Write “Enter Value ”
Read value
Push(value)
Option 2:
Pop()
Option 3:
Peak top ()
Option 4:
Peak stack()
Option 5:
Exit
[end of selection option ]
[end of loop of step 1]
Push(value)
[check for stack overflow]
1. If Top== N
2. Write(“stack is full”)
3. Resturn
4. End if
5. Top =Top+1
6. Stack[Top]=value
7. Return
Pop ()
[check for stack underflow]
1. If Top==0
2. Write (“Stack is empty”)
3. Return
4. End if
5. Value = Stack[Top]
6. Delete Stack[Top]
7. Top =Top-1
8. Write (“Deleted value is ”, value)
9. Return
Peak Stack/ Stack Traversal
1. [check for underflow ]
if Top==0 then
write (“stack is empty”)
Return
end if
2. A=Top
3. Repeat while A>0
4. Write Stack[A]
5. A=A-1
[End of Loop]
6. Return
Data structures & Algorithms
Lecture#10 Stacks
Course contents
Stack
Stack ADT
Stack Operations
List Implementation
Applications
Sample Examples
Stack by using Link List
Struct stk
{
Int data;
Struct stk *link;
};
Struct stk *top = NULL;
Stack Operations(algorithms)
{
Repeat while true
[Menu option]
Write(“1: push”)
Write(“2: pop”)
Write(“3: peep top”)
Write(“4: peep stack”)
Write(“5: exit”)
[selection of option]
Selsct option opt
Option 1:
Write “Enter Value ”
Read value
Push(value)
Option 2:
Pop()
Option 3:
Peak top ()
Option 4:
Peak stack()
Option 5:
Exit
[end of selection option ]
[end of loop of step 1]
Push(Value)
Temp = new node
Temp -> data = value
Temp -> link = top
Top = temp
Return
Pop()
1. [check for underflow]
If( top == -1) then
Write(“stack underflow”)
Return;
[End of if structure]
2. Temp = top ;
3. Top = top -> link
4. Write(“Deleted value is ”);
5. Delete (temp)
6. Return ;
Peak stack()
1. [check underflow]
If (top = 0) then
Write(“underflow’’)
Return;
[ End of if structure]
2. A = top
Repeat while (A< top)
Write(top[A])
A = A+1
[End of loop structure]
3. Return;
Peak top()
1. [check for underflow]
If(top == -1)
Write(“stack is empty”)
Return ;
[end of if structure]
2. Write (stack[top])
3. Return ;
Stack implementations
Array Implementation Linked List Implementation
Stack is empty when ‘top’ is equal to ZERO Stack is empty if underlying list’s ‘start’ points
to NULL
Static array with fixed size i.e. is_full() can be
implemented
Dynamic data structure i.e. elements are
allocated memory on runtime
A variable ‘top’ is required to keep track of
number of elements in stack i.e. constant time
execution
A linear time function size() returns the
number of elements in Stack
‘top’ keeps track of position where elements
are being pushed / popped from Stack
‘start’ of underlying linked list keeps track of
position where elements are pushed / popped
Stack uses
Stack is used by operating systems to implement function calls
Recursive functions can be converted into non-recursive functions using
stacks
Stacks can be used to evaluate mathematical expressions in postfix form
Undo mechanism in text editors make use of stack

More Related Content

Similar to Data Structure.pptx

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
Balwant Gorad
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
RAtna29
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
Apurbo Datta
 
stack (1).pptx
stack (1).pptxstack (1).pptx
stack (1).pptx
CrazyKiller4
 
Stack data structure
Stack data structureStack data structure
Stack data structure
rogineojerio020496
 
Project of data structure
Project of data structureProject of data structure
Project of data structure
Umme habiba
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data Structure
Yaksh Jethva
 
data structure
data structuredata structure
data structure
Arvind Kumar
 
Stack push pop
Stack push popStack push pop
Stack push pop
A. S. M. Shafi
 
Chapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdfChapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdf
GirT2
 
Data Structure (Stack)
Data Structure (Stack)Data Structure (Stack)
Data Structure (Stack)
Adam Mukharil Bachtiar
 
chapter8-stack-161018120225.pdf
chapter8-stack-161018120225.pdfchapter8-stack-161018120225.pdf
chapter8-stack-161018120225.pdf
MarlonMagtibay2
 
Stacks
StacksStacks
Stacks
Sadaf Ismail
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
nikshaikh786
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
Vineeta Garg
 
Stacks
StacksStacks
Stacks
sweta dargad
 
stack presentation
stack presentationstack presentation
Please review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdfPlease review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdf
fathimafancyjeweller
 

Similar to Data Structure.pptx (20)

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.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
stack (1).pptx
stack (1).pptxstack (1).pptx
stack (1).pptx
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Project of data structure
Project of data structureProject of data structure
Project of data structure
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data Structure
 
data structure
data structuredata structure
data structure
 
Stack push pop
Stack push popStack push pop
Stack push pop
 
Chapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdfChapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdf
 
Data Structure (Stack)
Data Structure (Stack)Data Structure (Stack)
Data Structure (Stack)
 
chapter8-stack-161018120225.pdf
chapter8-stack-161018120225.pdfchapter8-stack-161018120225.pdf
chapter8-stack-161018120225.pdf
 
Stacks
StacksStacks
Stacks
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Stack
StackStack
Stack
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Algo>Stacks
Algo>StacksAlgo>Stacks
Algo>Stacks
 
Stacks
StacksStacks
Stacks
 
stack presentation
stack presentationstack presentation
stack presentation
 
Please review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdfPlease review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdf
 

More from SajalFayyaz

Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
SajalFayyaz
 
Data structure.ppt
Data structure.pptData structure.ppt
Data structure.ppt
SajalFayyaz
 
data structure 9.pptx
data structure 9.pptxdata structure 9.pptx
data structure 9.pptx
SajalFayyaz
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptx
SajalFayyaz
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
SajalFayyaz
 
Data structure 6.pptx
Data structure 6.pptxData structure 6.pptx
Data structure 6.pptx
SajalFayyaz
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
SajalFayyaz
 
data structure3.pptx
data structure3.pptxdata structure3.pptx
data structure3.pptx
SajalFayyaz
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
SajalFayyaz
 
Data Structure.pptx
Data Structure.pptxData Structure.pptx
Data Structure.pptx
SajalFayyaz
 

More from SajalFayyaz (10)

Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
Data structure.ppt
Data structure.pptData structure.ppt
Data structure.ppt
 
data structure 9.pptx
data structure 9.pptxdata structure 9.pptx
data structure 9.pptx
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptx
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
Data structure 6.pptx
Data structure 6.pptxData structure 6.pptx
Data structure 6.pptx
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
data structure3.pptx
data structure3.pptxdata structure3.pptx
data structure3.pptx
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
Data Structure.pptx
Data Structure.pptxData Structure.pptx
Data Structure.pptx
 

Recently uploaded

原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
u86oixdj
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
ewymefz
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Subhajit Sahu
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
TravisMalana
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
jerlynmaetalle
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
ocavb
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
John Andrews
 
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
pchutichetpong
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
vcaxypu
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
slg6lamcq
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
Subhajit Sahu
 
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
vcaxypu
 
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
g4dpvqap0
 
一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单
ewymefz
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
nscud
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
ewymefz
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
Timothy Spann
 
Empowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptxEmpowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptx
benishzehra469
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
ewymefz
 
一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单
enxupq
 

Recently uploaded (20)

原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
 
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
 
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
 
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
 
一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
 
Empowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptxEmpowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptx
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
 
一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单
 

Data Structure.pptx

  • 1. CS261 DATA STRUCTURES & ALGORITHMS (WEEK-5) LECTURE-9 & 10 INTRODUCTION TO DATA STRUCTURES & ALGORITHMS Lecturer Azka Aziz Azka.a@scocs.edu.pk
  • 2. Data structures & Algorithms Lecture#09 Stacks
  • 3. Course contents Stack Stack ADT Stack Operations Array Implementation List Implementation Applications Sample Examples
  • 4. stack Stack is a data structure that stores data items. Data items when removed follow a particular order i.e. the item that was added to stack most recently will be removed first (Last In First Out)
  • 5. stack Stack items cannot be accessed randomly regardless of underlying data storage e.g. Array, Linked List A stack can be imagined as a cylinder with one end closed All items can be added and/or removed from same end (that is open) Adding an item onto stack is called ‘push operation’ Removing an item from stack is called ‘pop operation’
  • 6. Stack ADT … (implementing with Array)
  • 7. Stack Operations … Operation Description Pseudocode int inspect_top(); Returns element at ‘top’ with removing it from Stack  If stack is non-empty, return the element immediately beneath the ‘top’  Return -1 (indicator of Stack being empty) otherwise void push(int); Adds the new element onto the Stack  If Stack is full, mention it someway  Otherwise  Add new element to current position of ‘top’  Proceed ‘top’ position by 1 int pop(); Remove and return the element at ‘top’ from Stack  If Stack is empty, mention it someway  Otherwise  Decrease ‘top’ by 1 (making currently top element inaccessible for sub-sequent calls)  Return element at ‘top’ position
  • 8. Stack using array … implementation Implementation details and issues have been discussed during class
  • 9. Stack ADT … (implementing with Linked List)
  • 10. Stack Operations … Operation Description Pseudocode StackUsingList() A constructor that creates an underlying empty linked list Create new instance of underlying linked list as ‘data’ int size(); Returns the number of elements in the stack  Uses size() function from linked list instance ‘data’ to return the number of elements in stack int is_empty(); Returns TRUE if Stack contains no element. It returns FALSE otherwise.  Uses is_empty() function from underlying linked list instance ‘data’ to return whether stack is empty or not int inspect_top() Returns the stack’s top element without removing it  If ‘start’ of ‘data’ points to NULL  It returns -1  Otherwise  It returns value of data item in ‘start’
  • 11. Stack Operations … Operation Description Pseudocode void push(int key); Adds the new element onto the Stack  Creates new node using ‘key’  Adds it to ‘start’ of underlying linked list setting the next pointer appropriately int pop(); Remove and return the element at start of underlying list  If Stack is empty, return -1  Otherwise  Create a new node pointer ‘temp’ and point it to start of underlying list  Move ‘start’ of underlying list to one step forward  Set next of temp to NULL  Return data value of temp;
  • 12. Stack using list … implementation Implementation details and issues have been discussed during class
  • 13. Stack Operations(algorithms) { Repeat while true [Menu option] Write(“1: push”) Write(“2: pop”) Write(“3: peep top”) Write(“4: peep stack”) Write(“5: exit”) [selection of option] Selsct option opt Option 1: Write “Enter Value ” Read value Push(value) Option 2: Pop()
  • 14. Option 3: Peak top () Option 4: Peak stack() Option 5: Exit [end of selection option ] [end of loop of step 1]
  • 15. Push(value) [check for stack overflow] 1. If Top== N 2. Write(“stack is full”) 3. Resturn 4. End if 5. Top =Top+1 6. Stack[Top]=value 7. Return
  • 16. Pop () [check for stack underflow] 1. If Top==0 2. Write (“Stack is empty”) 3. Return 4. End if 5. Value = Stack[Top] 6. Delete Stack[Top] 7. Top =Top-1 8. Write (“Deleted value is ”, value) 9. Return
  • 17. Peak Stack/ Stack Traversal 1. [check for underflow ] if Top==0 then write (“stack is empty”) Return end if 2. A=Top 3. Repeat while A>0 4. Write Stack[A] 5. A=A-1 [End of Loop] 6. Return
  • 18. Data structures & Algorithms Lecture#10 Stacks
  • 19. Course contents Stack Stack ADT Stack Operations List Implementation Applications Sample Examples
  • 20. Stack by using Link List Struct stk { Int data; Struct stk *link; }; Struct stk *top = NULL;
  • 21. Stack Operations(algorithms) { Repeat while true [Menu option] Write(“1: push”) Write(“2: pop”) Write(“3: peep top”) Write(“4: peep stack”) Write(“5: exit”) [selection of option] Selsct option opt Option 1: Write “Enter Value ” Read value Push(value) Option 2: Pop()
  • 22. Option 3: Peak top () Option 4: Peak stack() Option 5: Exit [end of selection option ] [end of loop of step 1]
  • 23. Push(Value) Temp = new node Temp -> data = value Temp -> link = top Top = temp Return
  • 24. Pop() 1. [check for underflow] If( top == -1) then Write(“stack underflow”) Return; [End of if structure] 2. Temp = top ; 3. Top = top -> link 4. Write(“Deleted value is ”); 5. Delete (temp) 6. Return ;
  • 25. Peak stack() 1. [check underflow] If (top = 0) then Write(“underflow’’) Return; [ End of if structure] 2. A = top Repeat while (A< top) Write(top[A]) A = A+1 [End of loop structure] 3. Return;
  • 26. Peak top() 1. [check for underflow] If(top == -1) Write(“stack is empty”) Return ; [end of if structure] 2. Write (stack[top]) 3. Return ;
  • 27. Stack implementations Array Implementation Linked List Implementation Stack is empty when ‘top’ is equal to ZERO Stack is empty if underlying list’s ‘start’ points to NULL Static array with fixed size i.e. is_full() can be implemented Dynamic data structure i.e. elements are allocated memory on runtime A variable ‘top’ is required to keep track of number of elements in stack i.e. constant time execution A linear time function size() returns the number of elements in Stack ‘top’ keeps track of position where elements are being pushed / popped from Stack ‘start’ of underlying linked list keeps track of position where elements are pushed / popped
  • 28. Stack uses Stack is used by operating systems to implement function calls Recursive functions can be converted into non-recursive functions using stacks Stacks can be used to evaluate mathematical expressions in postfix form Undo mechanism in text editors make use of stack