You are on page 1of 7

Experiment No (TASK) : 2

Experiment (TASK) name : Implementation of singly linked list

Aim: To implement single linked list in data structure using ‘c’

Algorithm:

Algorithm steps:

Step1: Create nodes first,last,next,prev and cur then set the value as NULL.

Step 2: Read the list operation type.

step 3: If operation type is create then process the following steps.

1. Allocate memory for node cur.

2. Read data in cur's data area.

3. Assign cur link as NULL.

4. Assign first=last=cur.

Step 4: If operation type is Insert then process the following steps.

1. Allocate memory for node cur.

2. Read data in cur's data area.

3. Read the position the Data to be insert.

4. Availability of the position is true then assing cur's link as first and first=cur.

5. If availability of position is false then do following steps.

1. Assign next as cur and count as zero.

2. Repeat the following steps until count less than postion.

1 .Assign prev as next

2. Next as prev of link.

3. Add count by one.


1|Page
4. If prev as NULL then display the message INVALID POSITION.

5. If prev not qual to NULL then do the following steps.

1. Assign cur's link as prev's link.

2. Assign prev's link as cur.


Step5: If operation type is delete then do the following steps.

1. Read the position .

2. Check list is Empty .If it is true display the message List empty.

3. If position is first.

1. Assign cur as first.

2. Assign First as first of link.

3. Reallocate the cur from memory.

4. If position is last.

1. Move the current node to prev.

2. cur's link as Null.

3. Reallocate the Last from memory.

4. Assign last as cur.

5. If position is enter Mediate.

1. Move the cur to required postion.

2. Move the Previous to cur's previous position

3. Move the Next to cur's Next position.

4. Now Assign previous of link as next.

5. Reallocate the cur from memory.

2|Page
step 6: If operation is traverse.
1. Assign current as first.

2. Repeat the following steps untill cur becomes NULL.

PROGRAM:-
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
void create();
void insert();
void delet();
void display();
struct node
{
int data;
struct node *link;
};
struct node *first=NULL,*last=NULL,*next,*prev,*cur;
void create()
{
cur=(struct node*)malloc(sizeof(struct node));
printf("\nENTER THE DATA: ");
scanf("%d",&cur->data);
cur->link=NULL;
first=cur;
last=cur;
}
void insert()
{
int pos,c=1;
cur=(struct node*)malloc(sizeof(struct node));
printf("\nENTER THE DATA: ");
scanf("%d",&cur->data);
printf("\nENTER THE POSITION: ");
scanf("%d",&pos);
if((pos==1) &&(first!=NULL))
{
cur->link = first;
first=cur;
}
else
{
next=first;
while(c<pos)

3|Page
{
prev=next;
next=prev->link;
c++;
}
if(prev==NULL)
{
printf("\nINVALID POSITION\n");
}
else
{
cur->link=prev->link;
prev->link=cur;
}
}
}
void delet()
{
int pos,c=1;
printf("\nENTER THE POSITION : ");
scanf("%d",&pos);
if(first==NULL)
{
printf("\nLIST IS EMPTY\n");
}
else if(pos==1 && first->link==NULL)
{
printf("\n DELETED ELEMENT IS %d\n",first->data);
free(first);
first=NULL;
}
else if(pos==1 && first->link!=NULL)
{
cur=first;
first=first->link;
cur->link=NULL;
printf("\n DELETED ELEMENT IS %d\n",cur->data);
free(cur);
}
else
{
next=first;
while(c<pos)
{
cur=next;
next=next->link;

4|Page
c++;
}
cur->link=next->link;
next->link=NULL;
if(next==NULL)
{
printf("\nINVALID POSITION\n");
}
else
{
printf("\n DELETED ELEMENT IS %d\n",next->data);
free(next);
}
}
}
void display()
{
cur=first;
while(cur!=NULL)
{
printf("\n %d",cur->data);
cur=cur->link;
}
}
void main()
{
int ch;
clrscr();
printf("\n\nSINGLY LINKED LIST");
do
{
printf("\n\n1.CREATE\n2.INSERT\n3.DELETE\n4.EXIT");
printf("\n\nENTER YOUR CHOICE : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
display();
break;
case 2:
insert();
display();
break;
case 3:
delet();

5|Page
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice...");
}
}while(1);
}

OUTPUT: -

SINGLY LINKED LIST

1.CREATE
2.INSERT
3.DELETE
4.EXIT

ENTER YOUR CHOICE : 1

ENTER THE DATA: 10

10

1.CREATE
2.INSERT
3.DELETE
4.EXIT

ENTER YOUR CHOICE : 2

ENTER THE DATA: 30

ENTER THE POSITION: 1

30
10

1.CREATE
2.INSERT
3.DELETE
4.EXIT
6|Page
ENTER YOUR CHOICE : 3

ENTER THE POSITION : 2

LIST IS EMPTY

RESULT:- Thus the program is compiled and executed successfully.

7|Page

You might also like