affiliate marketing

Monday, 12 December 2011

LINKED LIST IMPLEMENTATION OF SINGLY LINKED LIST


LINKED LIST IMPLEMENTATION OF SINGLY LINKED LIST
Aim:
   To write a program to implement singly linked list using linked list.
Algorithm:
   Step 1: initialize the list as null
   Step 2: Display linked list operations insert, delete and display the result.
   Step 3: If choice is 1 the read element to be inserted and call the insert function
   Step 4: If choice is 2 then read element to be deleted and call the delete function
   Step 5: If choice is 3 then call display function
   Step 6: If choice is default the exit the program.
Prog Code:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void insert(int x);
void deletion(int x);
void display();
struct node
{
            int element;
            struct node *next;
}*list=NULL,*p;
struct node *find(int s)
{
            p=list->next;
            while(p!=NULL && p->element!=s)
                        p=p->next;
            return p;
}
struct node *findprevious(int s)
{
            p=list;
            while(p->next!=NULL && p->next->element!=s)
                        p=p->next;
            return p;
}
void main()
{
            int data,ch;
            clrscr();
            printf("\n\n1.INSERT\n\n2.DELETE\n\n3.DISPLAY");
            do
            {
                        printf("\n\nEnter your Choice :: ");
                        scanf("%d",&ch);
                        switch(ch)
                        {
                                    case 1:
                                                printf("\n\nEnter the element to be inserted::");
                                                scanf("%d",&data);
                                                insert(data);
                                                break;
                                    case 2:
                                                printf("\n\nEnter the element to be deleted::");
                                                scanf("%d",&data);
                                                deletion(data);
                                                break;
                                    case 3:
                                                display();
                                                break;
                                    default:
                                                printf("\n\nInvalid Choice......");
                                                getch();
                                                exit(0);
                        }
            }while(ch<4);
}
void insert(int x)
{
            struct node *newnode;
            int pos;
            newnode=malloc(sizeof(struct node));
            newnode->element=x;
            if(list->next==NULL)
            {
                        list->next=newnode;
                        newnode->next=NULL;
            }
            else
            {
                        printf("\n\nEnter the value of the element to be inserted ::");
                        scanf("%d",&pos);
                        p=find(pos);
                        newnode->next=p->next;
                        p->next=newnode;
            }
}
void deletion(int x)
{
            struct node *temp;
            temp=malloc(sizeof(struct node));
            p=findprevious(x);
            if(p->next!=NULL)
            {
                        temp=p->next;
                        p->next=temp->next;
                        printf("\n\nThe deleted element is %d",temp->element);
                        free(temp);
            }
            else
                        printf("\n\nElement is not found in the list!!!");
}
void display()
{
            if(list->next==NULL)
                        printf("\n\nList is empty!!!");
            else
            {
                        p=list->next;
                        printf("\n\nThe contents of the list are\n::");
                        while(p!=NULL)
                        {
                                    printf("%d ->",p->element);
                                    p=p->next;
                        }
            }
}

Output:

1.INSERT
2.DELETE
3.DISPLAY

Enter your Choice ::1

Enter the element to be inserted::2

Enter your Choice ::1

Enter the element to be inserted::5

Enter the value of the element to be inserted ::2

Enter your Choice :: 3

The contents of the list are::2 ->5 ->NULL

Enter your Choice :: 1

Enter the element to be inserted::7

Enter the value of the element to be inserted ::2

Enter your Choice :: 3

The contents of the list are ::2 ->7 ->5 ->NULL

Enter your Choice :: 2

Enter the element to be deleted::5

The deleted element is 5

Enter your Choice :: 3

The contents of the list are ::2 ->7 ->NULL

No comments:

Post a Comment