Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

Data Structures and

Algorithm Analysis I
(Unit III – Session 2)

Stacks – Linked List Implementation

Dr.G.Anupriya
Professor - CSE Department

CSE - Data Structures and Algorithm Analysis 1


Unit III – Course Outcome
• At the end of this unit, students will be able to:
Perform operations on Stack and Queue data structures for
various applications
• At the end of this session, students will be able to:
Implement operations on Linked List based Stacks

CSE - Data Structures and Algorithm Analysis 2


Stack
• Linked List Implementation
• Used for dynamic Stacks when maximum size is not known
in advance
• Similar to linked list implementation
• Top pointer is maintained instead of first
• Insert and Delete only at top end (Beginning of linked list)
• Theoretically Stack full condition will not occur

3
Stack
• Linear data structure
• Last In First Out (LIFO)
• List with the restriction that insertions and deletions can
only be performed at one end – called the top end of the
Stack
Book 5 Top of Stack
• Examples: Book 4
• Stack of coins / books Book 3
Book 2
• Undo / Redo operations
Book 1

Stack of Books

4
Stack Linked List implementation
• Push operation
Should we check for Stack
void push(int val) full condition? Justify..
{
struct Node *NN=(struct Node *)malloc(sizeof(struct Node));
NN->data = val;
NN->next = top;
top = NN;
} Time Complexity – O(1)

CSE - Data Structures and Algorithm


5
Analysis
Stack Linked List implementation
• Push operation Empty Stack - S

void push(int val) top = 

{
struct Node *NN=(struct Node *)malloc(sizeof(struct Node));
NN->data = val; Push 1
NN->next = top; NN
1 
top = NN;
}
top

CSE - Data Structures and Algorithm


6
Analysis
Stack Linked List implementation
Push 3
top
• Push operation 1 
NN
3 
void push(int val)
{ top
struct Node *NN=(struct Node *)malloc(sizeof(struct Node));
NN->data = val;
NN->next = top; top
top = NN; 3 1 

CSE - Data Structures and Algorithm


7
Analysis
Stack Linked List implementation
• Pop operation
int pop()
{
if(top==NULL)
{ printf(“Stack Empty”); return -1; }
int val = top->data;
struct Node *temp = top;
top = top->next;
free(temp);
return val; Time Complexity – O(1)
}

CSE - Data Structures and Algorithm


8
Analysis
Stack Linked List implementation
top
• Pop operation 3 1 

int pop()
temp top
{
if(top==NULL)
{ printf(“Stack Empty”); return -1; } Pop
int val = top->data;
val = 3
struct Node *temp = top;
top = top->next; return 3
free(temp);
return val;
}

CSE - Data Structures and Algorithm


9
Analysis
Stack Linked List implementation
• Pop operation top=
1 

int pop()
top temp
{
if(top==NULL)
{ printf(“Stack Empty”); return -1; } Pop
int val = top->data;
val = 1
struct Node *temp = top;
top = top->next; return 1
free(temp);
return val;
}

CSE - Data Structures and Algorithm


10
Analysis
Stack Linked List implementation
• Top operation
int top_val()
{
if(top==NULL)
{ printf(“Stack Empty”); return -1; }
return topdata;
}

Time Complexity – O(1)

CSE - Data Structures and Algorithm


11
Analysis
Stack Linked List implementation
• Top operation 1 

int top_val()
{ top
return 1
if(top==NULL)
{ printf(“Stack Empty”); return -1; }
return topdata;
} top=

return -1

CSE - Data Structures and Algorithm


12
Analysis
Summary
• Linked List implementation
• Dynamic and no upper bound on Stack size
• Push, Pop and Top
• Time Complexity – O(1)

CSE - Data Structures and Algorithm Analysis 13


References
• Mark A. Weiss, “Data Structures and Algorithm Analysis in
C”, Second Edition, Pearson Education, 2011.
• Anany Levitin, “Introduction to the Design & Analysis of
Algorithms”, Third Edition, Pearson Education, 2011.

CSE - Data Structures and Algorithm Analysis 14

You might also like