Linked List

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

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

node* ins_beg(node* head)
{
    node *p;
    p=(node*)malloc(sizeof(node));
    printf("Enter data ");
    scanf("%d",&p->data);
    p->next=head;
    return p;
}

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* ins_mid(node *head)
{
    node *p,*q;
    int y;
    p=(node*)malloc(sizeof(node));
    printf("Enter data ");
    scanf("%d",&p->data);
    p->next=NULL;
    printf("After which number ");
    scanf("%d",&y);
    for(q=head;q!=NULL;q=q->next)
        if(q->data==y)
            break;

    if(q!=NULL)
    {

        p->next=q->next;
        q->next=p;
    }
   else
       printf("Data not found !!\n");
    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;
}

node* del_mid(node *head)
{
    node *p,*q;
    int x;
    if(head==NULL)
    {
        printf("Underflow !!\n");
        return head;
    }
    printf("Enter data ");
    scanf("%d",&x);
    if(head->data==x)
    {
        p=head;
        head=head->next;
        free(p);
        return head;
    }
    for(q=head;q->next!=NULL;q=q->next)
    {
        if((q->next)->data==x)
            break;
    }
    if(q->next==NULL)
    {
        printf("Data not found !!\n");
        return head;
    }
    p=q->next;
    q->next=(q->next)->next;
    free(p);
    return head;
}

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

int main()
{
    int x;
    node *head;
    head=NULL;
    do
    {
        printf("\nEnter :\n1.Insert Beginning\n2.Insert End\n3.Insertion in Middle\n4.Delete Beginning\n5.Delete End\n6.Delete in Middle\n7.Display\n8.Exit\n");
        scanf("%d",&x);
        switch(x)
        {
            case 1: head=ins_beg(head);       break;
            case 2: head=ins_end(head);       break;
            case 3: head=ins_mid(head);       break;
            case 4: head=del_beg(head);       break;
            case 5: head=del_end(head);       break;
            case 6: head=del_mid(head);       break;
            case 7: display(head);            break;
        }
    }while(x!=8);
    return 0;
}

/*Output:

Enter :
1.Insert Beginning
2.Insert End
3.Insertion in Middle
4.Delete Beginning
5.Delete End
6.Delete in Middle
7.Display
8.Exit
1
Enter data 35

Enter :
1.Insert Beginning
2.Insert End
3.Insertion in Middle
4.Delete Beginning
5.Delete End
6.Delete in Middle
7.Display
8.Exit
2
Enter data 69

Enter :
1.Insert Beginning
2.Insert End
3.Insertion in Middle
4.Delete Beginning
5.Delete End
6.Delete in Middle
7.Display
8.Exit
3
Enter data 54
After which number 69

Enter :
1.Insert Beginning
2.Insert End
3.Insertion in Middle
4.Delete Beginning
5.Delete End
6.Delete in Middle
7.Display
8.Exit
7
35 69 54

Enter :
1.Insert Beginning
2.Insert End
3.Insertion in Middle
4.Delete Beginning
5.Delete End
6.Delete in Middle
7.Display
8.Exit
4

Enter :
1.Insert Beginning
2.Insert End
3.Insertion in Middle
4.Delete Beginning
5.Delete End
6.Delete in Middle
7.Display
8.Exit
5

Enter :
1.Insert Beginning
2.Insert End
3.Insertion in Middle
4.Delete Beginning
5.Delete End
6.Delete in Middle
7.Display
8.Exit
7
69

Enter :
1.Insert Beginning
2.Insert End
3.Insertion in Middle
4.Delete Beginning
5.Delete End
6.Delete in Middle
7.Display
8.Exit
6
Enter data 69

Enter :
1.Insert Beginning
2.Insert End
3.Insertion in Middle
4.Delete Beginning
5.Delete End
6.Delete in Middle
7.Display
8.Exit
4
Underflow !!

Enter :
1.Insert Beginning
2.Insert End
3.Insertion in Middle
4.Delete Beginning
5.Delete End
6.Delete in Middle
7.Display
8.Exit
5
Underflow !!

Enter :
1.Insert Beginning
2.Insert End
3.Insertion in Middle
4.Delete Beginning
5.Delete End
6.Delete in Middle
7.Display
8.Exit
6
Underflow !!

Enter :
1.Insert Beginning
2.Insert End
3.Insertion in Middle
4.Delete Beginning
5.Delete End
6.Delete in Middle
7.Display
8.Exit
8
*/

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