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;
}