Followers

Thursday 18 June 2020

Data Structures : Lecture7

Department: MCA
Semester    : II
Subject       : Data Structures through C++
Paper          : CCMCA 203 
Faculty        : Avinash Kumar



Syllabus covered in  this blog
STACK (Linked List Implementation) 



Linked list implementation of stack

Instead of using array, we can also use linked list to implement stack. Linked list allocates the memory dynamically. However, time complexity in both the scenario is same for all the operations i.e. push, pop and peek.

In linked list implementation of stack, the nodes are maintained non-contiguously in the memory. Each node contains a pointer to its immediate successor node in the stack. Stack is said to be overflow/full if the space left in the memory heap is not enough to create a node.

 

The structure Node is used to create the linked list that is implemented as a stack.


struct Node
{
   int data;
   struct Node *next;
};




The top most node in the stack always contains null in its address field. Lets discuss the way in which, each operation is performed in linked list implementation of stack.



Push operation

Pushing an element to a stack in linked list implementation is different from that of an array implementation. In order to push an element onto the stack, the following steps are involved.
  • Create a node first and allocate memory to it.
  • If the list is empty then the item is to be pushed as the start node of the list. This includes assigning value to the data part of the node and assign null to the address part of the node.
  • If there are some nodes in the list already, then we have to add the new element in the beginning of the list (to not violate the property of the stack). For this purpose, assign the address of the starting element to the address field of the new node and make the new node, the starting node of the list.

The push() function takes argument val i.e. value to be pushed into the stack. Then a new node is created and val is inserted into the data part. This node is added to the front of the linked list and top points to it.


The algorithm for PUSH operation is as follows:


1.    Allocate memory for the new node.
2.    Assign value to the data field of the new node.
3.    Make the next field of the new node point to top.
4.    Make top point to the new node.



void push(int val)
{
   struct Node* newnode = (struct Node*) malloc(sizeof(struct Node));  
   newnode->data = val;
   newnode->next = top;
   top = newnode;
}






POP operation

Deleting a node from the linked list implementation of stack is different from that in the array implementation. In order to pop an element from the stack, we need to follow the following steps :
  • Check for the underflow condition: The underflow condition occurs when we try to pop from an already empty stack. The stack will be empty if the head pointer of the list points to null.
  • Adjust the head pointer accordingly: In stack, the elements are popped only from one end, therefore, the value stored in the head pointer must be deleted and the node must be freed. The next node of the head node now becomes the head node.

The pop() function pops the topmost value of the stack, if there is any value. If the stack is empty then underflow is printed.

The algorithm for POP operation is as follows:

1.    Make a variable/pointer that point to the topmost node.
2.    Retrieve the value contained in the topmost node.
3.    Make top point to the next node in sequence.
4.    Release memory allocated to the node marked by variable/pointer.


void pop()
{
   if(top==NULL)
      cout<<"Stack Underflow"<<endl;
   else {
      cout<<"The popped element is "<< top->data <<endl;
      top = top->next;
   }
}

No comments:

Post a Comment