Está en la página 1de 13

IIT Bombay

Data Structures and Algorithms


Prof. Ajit A. Diwan
Prof. Ganesh Ramakrishnan
Prof. Deepak B. Phatak
Department of Computer Science and Engineering
IIT Bombay
Session: Implementing Stacks using linked lists
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Implementing Stacks (Linked List)


IIT Bombay

We will use a node to represent an element of stack


struct Node{
char info;
struct Node *next;
};
info *next

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Implementing Stacks (Linked List)


IIT Bombay

Our stack will now be a linked list of nodes


struct Stack{
struct Node *top;
};
typedef struct Stack Stack;

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Implementing Stacks (Linked List)


IIT Bombay

Implement the following functions

Initialize
Empty
Full (Not required)
Top
Push
Pop

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Implementing Stacks (Linked List)


IIT Bombay

Define Structure of Node

Define Structure of Stack

struct Node{
char info;
struct Node *next;
};

struct Stack{
struct Node *top;
};
typedef struct Stack Stack;

info *next
Initialize Stack

void Stack_Init(Stack *S){


S->top = NULL;
}

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Implementing Stacks (Linked List)


IIT Bombay

Determines Stack is empty or not

bool Stack_Empty(Stack *S){


return (S->top == NULL);
}

Examine the top element of Stack

bool Stack_Top(Stack *S, char &element){


//Assume stack has one element F
if(Stack_Empty(S)){
return false;
}
else{
element = S->top->info;
return true;
}
}
S->top

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

NULL
6

Implementing Stacks (Linked List)


Pushes/Inserts an element on the Stack
bool Stack_Push(Stack *S, char value) {
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->info=value;
// Occurs when stack is
not empty

//Assign address
//of temp to S->top

temp

= 0xA

S->top

NULL

Addr:0xA

= 0xA

Assign address of temp to S->top


i.e. S->top points to temp

S->top

NULL

temp Addr:0xB
R 0xA
S->top

Addr:0xA

NULL

S->top->next=NULL;
}
return true;
} // end of function

Addr:0xB
0xA
R

S->top = temp;
}

else{
S->top=temp;

Addr:0xA

= 0xA

temp->next = S->top;

temp
F
*next

//Occurs when stack is empty

*next

S->top
//temp->next should
// point to S->top

temp
info *next

Assume Stack has 1 element F


And R is to be pushed
tempAddr:0xB

if (S->top != NULL){

IIT Bombay

S->top

NULL

Addr:0xA
= 0xB and Deepak B. Phatak,
Ajit A. Diwan, Ganesh Ramakrishnan,
IIT Bombay

Implementing Stacks (Linked List)


IIT Bombay

Pops an Element from the Stack


bool Stack_Pop(Stack *S, char &element) {
struct Node *temp;
if(Stack_Empty(S)) {
return false;
}
else{
temp = S->top;

S->top = S->top->next;
element = temp->info;

Assume the following


= 0xB

S->top

}
}
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Addr:0xA

NULL

NULL

temp
S->top
= 0xB

0xA
Addr:0xB

Addr:0xA

temp
S->top
= 0xA

free(temp);
return true;

Addr:0xB
0xA
R

S->top
= 0xA

0xA
Addr:0xB

NULL

Addr:0xA

NULL

Addr:0xA

Implementing Stacks (Linked List)


IIT Bombay

int main(){
Stack s1;
char element;
//Initializing Stack
Stack_Init(&s1);
//confirm that it is empty
if ( Stack_Empty(&s1) == true)
cout << Stack is empty as expected << endl;
else
cout << "Error: stack not empty" << endl;

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Implementing Stacks (Linked List)


IIT Bombay

//Push Element 'F'


element = 'F';
if (Stack_Push(&s1,element) == true)
cout << "Element " << element << " pushed in" << endl;
else
cout << "Error: Cannot Push" << endl;
// Similarly, push two more elements, say R and Y

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

10

Implementing Stacks (Linked List)


IIT Bombay

//Access Top Element


if (Stack_Top(&s1,element) == true)
cout << "Top element: " << element << endl;
else
cout << "Error: stack empty" << endl;
//Pop all elements from the stack
for (int i = 0; i <=10 ; i ++){
if (Stack_Pop(&s1,element) == true)
cout << "Element Popped: " << element << endl;
else{
cout << "Error: Cannot Pop, Stack empty" << endl;
break;
}
}
}
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

11

Implementing Stacks (Linked List)


IIT Bombay

Output
Stack is empty as expected
Element F pushed
Element R pushed
Element Y pushed
Top element: Y
Element Popped: Y
Element Popped: R
Element Popped: F
Error: Cannot Pop, Stack empty

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

12

IIT Bombay

Thank you

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

13