/* Ajith - Syntax Higlighter - End ----------------------------------------------- */

7.07.2010

Implementation of Stack using Singly Linked Lists

Stacks are linear data structures which means the data is stored in what looks like a line (although vertically). In simple words we can say
A stack is a last in, first out (LIFO) abstract data type and data structure.
Basic usage of stack at the Architecture level is as a means of allocating and accessing memory.


We can only perform two fundamental operations on a stack: push and pop.

The push operation adds to the top of the list, hiding any items already on the stack, or initializing the stack if it is empty. The pop operation removes an item from the top of the list, and returns this value to the caller. A pop either reveals previously concealed items, or results in an empty list.

A stack is a restricted data structure, because only a small number of operations are performed on it.

Let us 'C' code for implementing a stack using Singly Linked Lists.
#include <stdio.h>
#include <stdlib.h>

//Structure containing a Data part & a
//Link part to the next node in the List
struct Node
{
int Data;
struct Node *Next;
}*Head;

//Poping from a Stack
void popStack()
{
struct Node *tmp_ptr=NULL, *cur_ptr=Head;

if(cur_ptr)
{
   Head = Head->Next;
   free(cur_ptr);
}     
else  
   printf("\nStack is Empty");
}   

//Pushing into Stack
void pushIntoStack(int num)
{
struct Node *tmp_ptr=NULL;

if((tmp_ptr=(struct Node *)malloc(sizeof(struct Node))) != NULL)
   tmp_ptr->Data=num;
else
   printf("\nMemory Allocation Failed");

if(Head)
{
   tmp_ptr->Next=Head;
   Head=tmp_ptr;
}
else { //List is Empty
   Head=tmp_ptr;
   Head->Next=NULL;
}
}

//Displaying Stack
void displayStack()
{
struct Node *cur_ptr=Head;

if(cur_ptr)
{
    printf("\nElements in Stack:\n");
    while(cur_ptr)
    {
        printf("\t%d\n",cur_ptr->Data);
        cur_ptr=cur_ptr->Next;
    }
    printf("\n");
}
else
   printf("\nStack is Empty");
}

int main(int argc, char *argv[])
{
int i=0;

//Set HEAD as NULL
Head=NULL;

while(1)
{
  printf("\n####################################################\n");
  printf("MAIN MENU\n");
  printf("####################################################\n");
  printf(" \n1. Push into stack");
  printf(" \n2. Pop from Stack");
  printf(" \n3. Display Stack");
  printf(" \n4. Exit\n");
  printf(" \nChoose Option: ");
  scanf("%d",&i);

  switch(i)
  {
    case 1:
    {
        int num;
        printf("\nEnter a Number to push into Stack: ");
        scanf("%d",&num);
        pushIntoStack(num);
        displayStack();
        break;
    }

    case 2:
    {
        popStack();
        displayStack();
        break;
    }

    case 3:
    {
        displayStack();
        break;
    }

    case 4:
    {
        struct Node *temp;

        while(Head!=NULL)
        {
            temp = Head->Next;
            free(Head);
            Head=temp;
        }
        exit(0);
    }

    default:
    {
        printf("\nWrong Option choosen");
    }
  }/* end if switch */
}/* end of while */
}/* end of main */
References:
1. Wikipedia

1 comment :

  1. push operation is the trick i.e. to add the node before the head node. It's unlike the regular case of adding a node to linked list. In regular case, we add the new node after the last element.

    ReplyDelete

Your comments are moderated