Thursday 22 September 2016

Singly Linked List: Insert at Front

Singly Linked List Insert at Front


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

struct node
{
            int data;
            struct node *link;
};
typedef struct node * NODE;

NODE insert_at_front(NODE first, int item);
void display(NODE first);

NODE getnode()
{
            NODE x;
            x = (NODE)malloc(sizeof(struct node));
            x->link = NULL;
            return x;
}

NODE insert_at_front(NODE first, int item)
{
            NODE temp;
            temp = getnode();
            temp->data = item;

            /*Case 1: List is empty.*/
            if(first==NULL)
                        return temp;

            /*Case 2: List has nodes. Insert temp node in the front*/
            temp->link = first;
            return temp;
           

                                    /*
                                                            ( or )
                                                    if(first)
                                                                    temp->link = first;
                                                    return temp;
                                     */
}

void display(NODE first)
{
            NODE cur;
            cur = first;
            printf("\nContents are:");
            if(cur == NULL)
                        printf("\nList is empty. Nothing to display.");
            else
            {
                        while(cur!=NULL)
                        {
                                    printf("%d  ", cur->data);
                                    cur = cur->link;
                        }
            }
}

void main()
{
            int ch, item;
            NODE first = NULL;
            while(1)
            {
                        printf("\n\n~~MENU~~");
                        printf("\n1.Insert at front");
                        printf("\n2.Display");
                        printf("\n3.exit");
                        printf("\nEnter your choice: ");
                        scanf("%d",&ch);
                        switch(ch)
                        {
                                    case 1:             printf("\nEnter the value: ");
                                                            scanf("%d", &item);
                                                            first = insert_at_front(first, item);
                                                            break;
                                    case 2:            display(first);
                                                            break;
                                    case 3:  exit(0);
                        }
            }
}

Output:
~~MENU~~
1.Insert at front
2.Display
3.exit
Enter your choice: 1
Enter the value: 11

~~MENU~~
1.Insert at front
2.Display
3.exit
Enter your choice: 1
Enter the value: 12

~~MENU~~
1.Insert at front
2.Display
3.exit
Enter your choice: 2
Contents are: 12      11
~~MENU~~
1.Insert at front
2.Display
3.exit

No comments:

Post a Comment