Monday, 19 October 2015

C program to implement stack

#include<stdio.h>
#include<conio.h>
#define MAX 10
int st[MAX],top=-1;
void push(int st[],int val);
int pop(int st[]);
int peep(int st[]);
void display(int st[]);

void main()
{
    int ch,m,value;
    clrscr();
    while(1)
    {
        printf("\n press 1 for push operation");
        printf("\n press 2 for pop operation");
        printf("\n press 3 for peep operation");
        printf("\n press 4 for display");
        printf("\n press 5 for exit");
        printf("\n enter your choice:");
        scanf("%d",&ch);

        switch(ch)
        {
            case 1:
                if(top==MAX-1)
                {
                    printf("\n stack overflow");
                    break;

                }
                printf("\n enter data to be inserted:");
                scanf("%d",&value);
                push(st,value);
            break;

            case 2:
                if(top==-1)
                {
                    printf("\n stack underflow");
                    break;
                }
                m=pop(st);
                printf("%d value is deleted",m);
            break;

            case 3:
                if(top==-1)
                {
                    printf("\n stack underflow");
                    break;
                }
                m=peep(st);
                printf("%d",m);
            break;

            case 4:
                if(top==-1)
                {
                    printf("\n stack underflow");
                    break;
                }
                display(st);
            break;

            case 5:
                exit(1);
            break;

            default:
                printf("\n wrong choice");
        }
        getch();
    }
}

void push(int st[],int val)
{
    top++;
    st[top]=val;
}

int pop(int st[])
{       int val;
    val=st[top];
    top--;
    return val;
}

int peep(int st[])
{
     return st[top];
}

void display()
{
    int i;
    for(i=top;i>=0;i--)
    {
        printf("%d\n",st[i]);
    }
}



                                                                               OR


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
int m,n;

struct node
{
    int data;
    struct node *next;
}*top;

void push(int val);
int pop();
void display();

void main()
{
    int ch,i;
    top=NULL;
    clrscr();
    while(1)
    {
        printf("\n press 1 for push operation");
        printf("\n press 2 for pop operation");
        printf("\n press 3 for display");
        printf("\n press 4 for exit");
        printf("\n enter your choice:");
        scanf("%d",&ch);

        switch(ch)
        {
            case 1:
                printf("\n enter total no. of nodes to create:");
                scanf("%d",&m);
                for(i=0;i<m;i++)
                {

                    printf("\n enter data to be inserted:");
                    scanf("%d",&n);
                    push(n);
                }
            break;

            case 2:
                if(top==NULL)
                {
                    printf("\n stack underflow");
                    break;
                }
                m=pop();
                printf("%d value is deleted",m);
            break;

            case 3:
                if(top==NULL)
                {
                    printf("\n stack underflow");
                    break;
                }
                display();
            break;

            case 4:
                exit(1);
            break;

            default:
                printf("\n wrong choice");
        }
        getch();
    }
}

void push(int val)
{
    struct node *tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=val;
    if(top==NULL)
    {
        top=tmp;
        top->next=NULL;
        return;
    }
    else
    {
        tmp->next=top;
        top=tmp;
    }
}

void display()
{
    struct node *q;
    q=top;
    while(q!=NULL)
    {
        printf("\n data=%d",q->data);
        q=q->next;
    }
}

int pop()
{
    struct node *tmp,*p;

    if(p!=top)
    {
        p=top;
        tmp=p;
        top=top->next;

        free(p);
    }
    return tmp->data;

}





No comments:

Post a Comment