You are on page 1of 7

Ex.

No: 7 Implementation of Singly Linked List

Aim:
To create a singly Linked list and to perform insertion and deletion operations.

Algorithm:

6. Start the program.


7. Create a node with two fields, data and link field.
a. Allocate space for the node dynamically.
b. Create link between the created nodes and let the last node be with NULL link
c. Insert the input data in the data field and press –1 to stop the same.
8. Get the choice of operation - either insertion or deletion.
9. For insertion get the position in which insertion is to be done and the element to be
inserted. Check for the start, middle or end position of insertion. Insert the node and
change its link accordingly.
10. For deletion get the position in which deletion is to be done. Delete the node and then
link it to the next node. Before deletion check whether there is data in the list to be
deleted.

11. Using display option, list the elements of the list.


12. Stop the program.

Program:

#include<stdio.h>
#include<conio.h>

typedefstruct list
{
intnum;
struct list *next;
}LIST;

LIST *head,*node,*temp;

void create();
void insert();
void del();
void display();
voidmain()
{
intch;
clrscr();
head=NULL;
while(1)
{
printf("enter the choice \n");
printf(" 1:create list \n 2:insert an element to list \n 3:delete an element \n 4.display \n 5.exit \n");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
insert();
break;
case 3:
del();
break;
case 4:
display();
break;
case 5:
exit(0);
break;
default:
printf("wrong choice");
break;
}}}

void create()
{
int k=1;
printf("\n enter the element( else enter -1 to stop):");
scanf("%d",&k);
while(k!=-1)
{
LIST *s1,*p;
s1=(struct list *)malloc(sizeof(LIST));
s1->num=k;
s1->next=NULL;
if(head==NULL)
head=s1;
else p-
>next=s1;
p=s1;
printf("\n enter the element(else enter -1 to
stop):"); scanf("%d",&k);
}}

void display()
{
temp=head;
printf("\n List elements are \n");
while(temp!=NULL)
{
printf("\n %d--->",temp-
>num); temp=temp->next;
}
getch();
}

void del()
{
int i=0,pos;
temp=head;
printf("Enter the position to delete \n");
scanf("%d",&pos); while(temp!
=NULL)
{
if(pos==1)
{
head=temp->next;
printf("node
deleted"); break;
}
else
{
i++;
if(i+1==pos)
{
temp->next=temp->next-
>next; printf("node deleted");
}
}
temp=temp->next;
}
getch();
}

void insert()
{
LIST *p;
int y;
temp=head;
printf("Enter the element after which new element to be inserted
\n"); scanf("%d",&y);
printf("Enter the node value to insert \n");
p=(struct list *)malloc(sizeof(LIST));
scanf("%d",&p->num); while(temp!
=NULL)
{
if(temp->num==y)
{
p->next=temp->next;
temp->next=p;
printf("value inserted \n");
}
temp=temp->next;
}}

Sample Input and Output


1:create list
2:insert an element to
list 3:delete an element
4.display
5.exit

Enter
choice 1
Enter the element -1 to stop
10
Enter the element -1 to stop
20
Enter the element -1 to stop
30
Enter the element -1 to stop
-1
Enter
choice 3
Enter the position to be
deleted 1
Enter
choice 4
List elements are
10
30
Enter
choice 5

RESULT:
Thus the program to perform operations of singly linked list was successfully executed.

Ex. No: 8 Implementation ofDoubly Linked List

Aim:
To create a Doubly Linked list and to perform insertion and deletion operations.

Algorithm:

1. Start the program.


2. Create a node with three fields, data and flink and blink field.
a. Allocate space for the node dynamically.
b. Create link between the created nodes and let the last node be with NULL flink
and first node be with NULL blink.
c. Insert the input data in the data field and press –1 to stop the same.
3. Get the choice of operation - either insertion or deletion.
4. For insertion get the position in which insertion is to be done and the element to be
inserted. Check for the start, middle or end position of insertion. Insert the node and
change its link accordingly.
5. For deletion get the position in which deletion is to be done. Delete the node and then
link it to the next node. Before deletion check whether there is data in the list to be
deleted.
6. Using display option, list the elements of the list.
7. Stop the program.

Program:
#include<stdio.h>
#include<conio.h>

typedefstruct list
{
intnum;
struct list *prev;
struct list
*next; }LIST;

LIST *head,*node,*temp;

void create();
void insert();
void del();
void display();

voidmain()
{
intch;
clrscr();
head=NULL;
while(1)
{
printf("enter the choice \n");
printf(" 1:create list \n 2:insert an element to list \n 3:delete an element \n 4.display \n 5.exit \n");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
insert();
break;
case 3:
del();
break;
case 4:
display();
break;
case 5:
exit(0);
break;
default:
printf("wrong choice");
break;
}
}
}
void create()
{
int k=1;
printf("\n enter the element(Else enter -1 to stop:");
scanf("%d",&k);
while(k!=-1)
{
LIST *s1,*p;
s1=(struct list *)malloc(sizeof(LIST));
s1->num=k;
s1->next=NULL;
s1->prev=NULL;
if(head==NULL)
head=s1;
else
{
p->next=s1;
s1->prev=p;
}
p=s1;
printf("\n enter the element (Else enter -1 to stop:");
scanf("%d",&k);
}
}
void display()
{
temp=head;
printf("\n List elements are
\n\n"); while(temp!=NULL)
{
printf("%d--->",temp->num);
temp=temp->next;
}
getch();
}

void del()
{
intpos,i=0;
LIST *temp1;
temp=head;
printf("Enter the position to delete \n");
scanf("%d",&pos); while(temp!
=NULL)
{
i++;
if(pos==1)
{
head=temp->next;
head->prev=NULL;
printf("Node deleted
\n"); break;
}
else if(i==pos)
{
temp->prev->next=temp->next;
temp->next->prev=temp-
>prev; printf("node deleted");
}
temp=temp->next;
}
getch();
}

void insert()
{
LIST *p;
int y;
temp=head;
printf("Enter the element after which new element to be inserted
\n"); scanf("%d",&y);
printf("Enter the node value to insert \n");
p=(struct list *)malloc(sizeof(LIST));
scanf("%d",&p->num); while(temp!
=NULL)
{
if(temp->num==y)
{
p->next=temp->next;
temp->next->prev=p;
temp->next=p; p-
>prev=temp; printf("value
inserted \n");
}
temp=temp->next;
}
}

Sample Input and Output


1:create list
2:insert an element to
list 3:delete an element
4.display
5.exit

Enter
choice 1
Enter the element(Else enter -1 to stop)
10
Enter the element(Else enter -1 to stop)
20
Enter the element (Else enter -1 to stop)
30
Enter the element (Else enter -1 to stop)
-1
Enter
choice 3
Enter the element to be
deleted 10
Enter
choice 4
List elements are
20 30

Enter
choice 5

RESULT:
Thus the program to perform operations of doubly linked list was successfully executed.

You might also like