AIM:
To
write a C program to implement singly linked list.
ALGORITHM:
Step 1: Define a list as a node of a
structure with one data and one pointer
pointing to next element in the structure.
Step 2: Declare the function
prototypes creation (), insertion (), deletion () and
display () to perform the list function
Step
3: Declare the necessary pointer variables as structure node data type.
Step
4: Get a choice from the user.
Step
5: If the choice is create get first data from the user by calling the function
create () and display
contents of the list.
Step
6: If the choice is insert, get data and its position by calling function
insert ()and assign the next field of the given data node according to
the position. Display the contents of the list.
Step
7: If the choice is delete, get the position of data which is going to be
removed from the list and assign next field of previous node
to address.
Step
8: Repeat the steps 4, 5&6 until the choice is exit.
Step
9: Terminate the execution.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdio.h>
typedef struct SLL
{
int data;
struct SLL *next;
}node;
node *create();
void main()
{
int choice,val;
char ans;
node *head;
void display(node*);
node *search(node*,int);
void insert(node*);
void Delete(node**);
head=NULL;
do
{
clrscr();
printf("Program to perform various operations:");
printf("\n1.Create\n2.Display\n3.Search\n4.Insert\n5.Delete\n6.Exit");
printf("\nEnter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1:
head=create();break;
case 2:
display(head);break;
case 3:
printf("Enter the element to be searched");
scanf("%d",&val);
search(head,val);break;
case 4:
insert(head);break;
case 5:
Delete(&head);break;
case 6:
exit(0);
default :
clrscr();
printf("Invalid choice");
getch();
}
}while(choice!=6);
}
node *create()
{
node *temp,*New,*head;
int val,flag;
char ans='y';
node *get_node();
temp=NULL;
flag=1;
do
{
printf("\nEnter your element:");
scanf("%d",&val);
New=get_node();
if(New==NULL)
printf("\nMemory not allocated");
New->data=val;
if(flag)
{
head=New;
temp=head;
flag=0;
}
else
{
temp->next=New;
temp=New;
}
printf("\nDo you want to enter more
elements?(y/n)");
ans=getch();
}while(ans=='y');
printf("\nSLL is created");
getch();
clrscr();
return head;
}
node *get_node()
{
node *temp;
temp=(node*)malloc(sizeof(node));
temp->next=NULL;
return temp;
}
void display(node *head)
{
node *temp;
temp=head;
if(temp==NULL)
{
printf("\nList empty");
getch();
clrscr();
return;
}
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("NULL");
getch();
clrscr();
}
node *search(node *head,int key)
{
node *temp;
int found;
temp=head;
if(temp==NULL)
{
printf("Linked list empty\n");
getch();
clrscr();
return NULL;
}found=0;
while(temp!=NULL&&!found)
{
if(temp->data!=key)
temp=temp->next;
else
found=1;
}
if(found)
{
printf("Elements are present in list\n");
getch();
return temp;
}
else
{
printf("\nElements are not present");
getch();
return NULL;
}
}
void insert(node *head)
{
node *temp,*New;
int val;
temp=head;
if(temp==NULL)
{
printf("\nInsertion impossible\n");
getch();
return;
}
clrscr();
printf("\nEnter element after whch you want to
insert");
scanf("%d",&val);
temp=search(head,val);
if(temp!=NULL)
{
printf("\nEnter the element to be inserted");
scanf("%d",&val);
New=(node*)malloc(sizeof(node));
if(New==NULL)
printf("memory not allocaed\n");
New->data=val;
New->next=NULL;
New->next=temp->next;
temp->next=New;
printf("\nThe element is inserted\n");
getch();
clrscr();
}
}
node *get_prev(node *head,int val)
{
node *temp,*prev;
int flag;
temp=head;
if(temp==NULL)
return 0;
flag=0;
prev=NULL;
while(temp!=NULL&&!flag)
{
if(temp->data!=val)
{
prev=temp;
temp=temp->next;
}
else
flag=1;
}
if(flag)
return prev;
else
return NULL;
}
void Delete(node **head)
{
node *temp,*prev;
int key;
temp=*head;
if(temp==NULL){
printf("\nThe list is empty \n");
getch();
clrscr();
return;
}
clrscr();
printf("\nEnter the elements to be deleted");
scanf("%d",&key);
temp=search(*head,key);
if(temp!=NULL)
{
prev=get_prev(*head,key);
if(prev!=NULL)
{
prev->next=temp->next;
free(temp);
}
else
{
*head=temp->next;
free(temp);}
printf("\nThe element is deleted\n");
getch();
clrscr();
}
}
OUTPUT
Program to perform various operations:
1.Create
2.Display
3.Search
4.Insert
5.Delete
6.Exit
Enter your choice 1
Enter your element:10
Do you want to enter more elements?(y/n)
Enter your element:20
Do you want to enter more elements?(y/n)
Enter your element:30
Do you want to enter more elements?(y/n)
SLL is created
Enter your choice 2
10->20->30->NULL
Enter your choice 3
Enter the element to be searched20
Elements are present in list
Enter your choice 4
Enter element after which you want to insert20
Elements are present in list
Enter the element to be inserted25
The element is inserted
Enter your choice 5
Enter the elements to be deleted25
Elements are present in list
The element is deleted
Enter your choice 6
RESULT: Thus a C program is written to implement singly linked list
and executed
successfully
No comments:
Post a Comment