Wednesday, September 21, 2016

Push And Pop A Stack Using Linklist

Tags

Program To Push And Pop a Stack:

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node* next;
   
}*head;
void push()
{int item;
   
            scanf("%d",&item);
           
    struct node *temp=(struct node*)malloc(sizeof(struct node));
    temp->data=item;
    temp->next=head;
    head=temp;
    return;
}
void pop()
{if(head==NULL)
{
    printf("\nUNDERFLOW\n");
}
else{

    head=head->next;
}
}
void display()
{struct node *temp=head;
while(temp!=NULL)
{
    printf("\n%d\n",temp->data);
    temp=temp->next;
}
}
main()
{
    int x;
    printf("\nEnter Size of Stack\n");
    scanf("%d",&x);
    printf("\nEnter Data TO BE Inserted\n");
    for(int i=0;i<x;i++)
    {
        push();
    }
    display();
    start:
    int choice,item,it;
    printf("\nOPERTAION ON STACKS\nPress 1 For Push\nPress 2 For Pop\n");
    scanf("%d",&choice);
    switch(choice)
    {
        case 1:printf("\nEnter Data TO BE Inserted\n");
            push();
            break;
    case 2:
        pop();
        break;
    default:
    printf("\nEnter A Valid Option\n");
    break;   
    }
    printf("\nNEW STACK IS\n");
    display();
    printf("\nEND OF OPERATION\nPRESS 1 FOR OPERATING AGAIN ELSE ANY OTHER VALUE\n ");
    scanf("%d",&it);
    if(it==1)
    goto start;
    else
    {return 0;
    }
}