Stack and Queue using Linked List

#include <stdio.h>
#include <malloc.h>

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

node* ins_end(node* head)
{
    node *p,*q;
    q=(node*)malloc(sizeof(node));
    printf("Enter data ");
    scanf("%d",&q->data);
    q->next=NULL;
    if(head==NULL)
    {
       return q;
    }
    else
    {
        p=head;
        while(p->next!=NULL)
        {
            p=p->next;
        }
        p->next=q;
    }
    return head;
}

node* del_beg(node *head)
{
    node *p;
    if(head==NULL)
    {
        printf("Underflow !!\n");
    }
    else
    {
        p=head;
        head=head->next;
        free(p);
    }
    return head;
}

node* del_end(node *head)
{
    node *p,*q;
    if(head==NULL)
    {
        printf("Underflow !!\n");
    }
    else if(head->next==NULL)
    {
        free(head);
        head=NULL;
    }
    else
    {
        p=head;
        while((p->next)->next!=NULL)
        {
            p=p->next;
        }
        q=p->next;
        p->next=NULL;
        free(q);
    }
    return head;
}

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

int main()
{
    int x,y;
    node *head;
    head=NULL;
    do
    {
        printf("\nEnter\n1.Stack using LL\n2.Queue using LL\n3.Exit\n");
        scanf("%d",&y);
        switch(y)
        {
            case 1:
            do
            {
                printf("\nEnter :\n1.Push\n2.Pop\n3.Display\n4.Exit\n");
                scanf("%d",&x);
                switch(x)
                {
                    case 1: head=ins_end(head);       break;
                    case 2: head=del_end(head);      break;
                    case 3: display(head);                   break;
                }
            }while(x!=4);
            break;


          case 2:
            do
            {
                printf("\nEnter :\n1.Insert\n2.Delete\n3.Display\n4.Exit\n");
                scanf("%d",&x);
                switch(x)
                {
                    case 1: head=ins_end(head);       break;
                    case 2: head=del_beg(head);      break;
                    case 3: display(head);                   break;
                }
            }while(x!=4);
            break;
        }
    }while(y!=3);
    return 0;
}

/*Output:
Enter
1.Stack using LL
2.Queue using LL
3.Exit
1
Enter :
1.Push
2.Pop
3.Display
4.Exit
1
Enter data 21

Enter :
1.Push
2.Pop
3.Display
4.Exit
1
Enter data 87

Enter :
1.Push
2.Pop
3.Display
4.Exit
3
21 87

Enter :
1.Push
2.Pop
3.Display
4.Exit
2

Enter :
1.Push
2.Pop
3.Display
4.Exit
3
21

Enter :
1.Push
2.Pop
3.Display
4.Exit
2

Enter :
1.Push
2.Pop
3.Display
4.Exit
3

Enter :
1.Push
2.Pop
3.Display
4.Exit
2
Underflow !!

Enter :
1.Push
2.Pop
3.Display
4.Exit
4

Enter
1.Stack using LL
2.Queue using LL
3.Exit
2
Enter :
1.Push
2.Pop
3.Display
4.Exit
1
Enter data 35

Enter :
1.Push
2.Pop
3.Display
4.Exit
1
Enter data 68

Enter :
1.Push
2.Pop
3.Display
4.Exit
3
35 68

Enter :
1.Push
2.Pop
3.Display
4.Exit
2

Enter :
1.Push
2.Pop
3.Display
4.Exit
3
68

Enter :
1.Push
2.Pop
3.Display
4.Exit
2

Enter :
1.Push
2.Pop
3.Display
4.Exit
3

Enter :
1.Push
2.Pop
3.Display
4.Exit
2
Underflow !!

Enter :
1.Push
2.Pop
3.Display
4.Exit
4

Enter
1.Stack using LL
2.Queue using LL
3.Exit
3
*/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s