STACK USING POINTER by smd

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
}*newn,*top=0;

void push()
{
    int x;
    printf("enter x value\n");
    scanf("%d",&x);
    newn=(struct node*)malloc(sizeof(struct node));
    newn->data=x;
    newn->next=top;
    top=newn;
}
void pop()
{
    if(top==0)
    {
        printf("stack is underflow\n");
       
    }
else
{
    struct node *temp;
    temp=top;
    printf("%d is deleted\n",temp->data);
    top=top->next;
    free(temp);
}
}
void peek()
{
    if(top==0)
    {
        printf("stack is empty\n");
       
    }
else
{
    struct node *temp;
    temp=top;
    printf("%d is top most element\n",top->data);
}
}
void display()
{
    if(top==0)
    {
        printf("stack is empty\n");
    }
else
{
    struct node *temp;
    temp=top;
    while(temp!=0)
    {
        printf("%d->",temp->data);
        temp=temp->next;
    }
}
}
void push();
void pop();
void peek();
void display();
void main()
{
    int choice=0;
    while(choice!=5)
    {
        printf("choose the below options\n");
        printf("1.push\n 2.pop\n 3.peek\n 4,display\n 5.exit\n");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
             push();
             break;
             case 2:
             pop();
             break;
             case 3:
             peek();
             break;
             case 4:
             display();
             break;
             case 5:
             printf("exiting....");
              break;
              default :
              printf("invalid entry\n");
               break;
        }
    }
}

















OUTPUT :

choose the below options 1.push 2.pop 3.peek 4,display 5.exit 1 enter x value 10 choose the below options 1.push 2.pop 3.peek 4,display 5.exit 1 enter x value 20 choose the below options 1.push 2.pop 3.peek 4,display 5.exit 1 enter x value 30 choose the below options 1.push 2.pop 3.peek 4,display 5.exit 3 30 is top most element choose the below options 1.push 2.pop 3.peek 4,display 5.exit 2 30 is deleted choose the below options 1.push 2.pop 3.peek 4,display 5.exit 4 20->10->choose the below options 1.push 2.pop 3.peek 4,display 5.exit 5 exiting....








Comments

Popular posts from this blog

SINGLE LINKED LIST by smd

CLL by smd

QUEUE USING ARRAYS by smd