6
The stack is a very common data structure used in programs. Representation of stack may be done in various ways in computer memory for ex. by means of a link list or a linear array. Implementation of array-based stack is very simple. It uses top variable to point to the topmost stack's element in the array. Initially the value of top is set to -1. Every time we execute push() operation the value of top is incremented by one and writes the pushed element to storage[top]. Incase of pop() operation if the value of top not equals to -1 then will return element storage[top] and the value of top will be decremented by one. The only limitation of array based implementation of stack is that stack capacity is limited to a certain value and overfilling the stack will cause an error. Though, using the ideas from dynamic arrays implementation, this limitation can be easily avoided. Algorithm of Push Let stack[Max] is an array for implementing the stack. TOP represents top of the stack. 1. [check for stack overflow?] If TOP = Max , then print : overflow and exit 2. Set TOP = TOP + 1 3. Set stack[TOP] := ITEM [ insert item in new TOP position] 4. Exit Stack Implementation Stack implemented as an array Algorithm of Pop 1. [check for stack underflow?] If top < 0 , then Print : underflow and exit Else [remove the top element] Set item := stack[TOP] 2. Decrement the stack top Set TOP := TOP - 1 3. Return the deleted item from the stack 4. Exit

Implementation of Stack

Embed Size (px)

DESCRIPTION

Implementation of Stack

Citation preview

  • The stack is a very common data structure used in programs. Representation of stack may be done invarious ways in computer memory for ex. by means of a link list or a linear array.

    Implementation of array-based stack is very simple. It uses top variable to point to the topmoststack's element in the array. Initially the value of top is set to -1. Every time we execute push()operation the value of top is incremented by one and writes the pushed element to storage[top].Incase of pop() operation if the value of top not equals to -1 then will return element storage[top] andthe value of top will be decremented by one.

    The only limitation of array based implementation of stack is that stack capacity is limited to a certainvalue and overfilling the stack will cause an error. Though, using the ideas from dynamic arraysimplementation, this limitation can be easily avoided.

    Algorithm of Push

    Let stack[Max] is an array for implementing the stack.

    TOP represents top of the stack.

    1. [check for stack overflow?]

    If TOP = Max , then print : overflow and exit

    2. Set TOP = TOP + 1

    3. Set stack[TOP] := ITEM [ insert item in new TOP position]

    4. Exit

    Stack Implementation

    Stack implemented as an array

    Algorithm of Pop1. [check for stack underflow?] If top < 0 , then Print : underflow and exit Else [remove the top element] Set item := stack[TOP]2. Decrement the stack top Set TOP := TOP - 13. Return the deleted item from the stack4. Exit

  • Following is the C program code for the stack operations :

    In this program code following notation is used :

    Stack : name of the stack array

    Maxsize : maximum size of the stack

    Stacktop : top position of the stack

    Val : element to be pushed

    #include#define maxsize 100int stack[maxsize];int stacktop=-1;void push(int);int pop(void);void display(void);int main(){ int choice=0,val=0; do { printf("\n\t1.Push 2.Pop 3.Display \n\tSelect Your Choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("\tElement to be Pushed : "); scanf("%d",&val); push(val); break; case 2: val=pop(); if(val!=-1) printf("\tPopped Element : %d\n",val);

    break;

    case 3: display(); break;

  • default: printf("\tWrong Choice"); break; } }while(choice-1) { printf("\tElements are:"); while(i
  • We can implement Stack using linked list as well. The dynamic allocation of memory makes it betterchoice for implementing Stacks. In this scenario, each stack element is composed of a data variableand a reference (in other words, a link) to the next node in the sequence. The push() operation simplycreates a new node by allocating memory for it (Using malloc), and then linking it to a linked list asthe new head. A pop() operation detach the node from the linked list, and assigns the stack pointer tothe head of the previous second node. It also checks whether the list is empty before popping from it.

    Following is the C program code for the stack operations :#include#include#define maxsize 10void push();void pop();void display();struct node{ int info; struct node *link;}*start=NULL, *new,*temp,*p;typedef struct node N;main(){ int choice,a; do { printf("\n\t1.Push 2.Pop 3.Display \n\tSelect Your Choice : "); scanf("%d",&choice); switch(choice) { case 1: push(); break;

    case 2: pop(); break;

    case 3: display(); break;

    default: printf("\nInvalid choice"); break;

    Stack implemented as a Linked List

  • } }while(chinfo); new->link=NULL; if(start==NULL) start=new; else { p=start; while(p->link!=NULL) p=p->link; p->link=new; }}

    void pop(){ if(start==NULL) printf("\nStack is empty"); else if(start->link==NULL) { printf("\nThe deleted element is : %d",start->info); free(start); start=NULL; } else { p=start; while(p->link!=NULL) { temp=p; p=p->link; } printf("\nDeleted element is : %d\n", p->info); temp->link=NULL; free(p); }}void display(){

  • if(start==NULL) printf("\nStack is empty"); else { printf("\nThe elements are : "); p=start; while(p!=NULL) { printf("%d",p->info); p=p->link; } printf("\n"); }}