Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Ex.

No:3 SINGLY LINKED LIST

Aim:
To write a program to perform various operations of Singly linked list.

Algorithm:
1. Initialize the node which contains two parts namely data part and the pointer to the next
node.
2. If Choice=1, Display insert menu.
3. Within that Insert menu,
(i) If Choice=1, Execute insertion at beginning.
(ii) If Choice=2, Execute insertion at the end.
(iii) If Choice=3, Execute insertion at the random location.
(iv) Otherwise print “Wrong Choice”.
4. If Choice=2, Execute display function.
5. If Choice=3, Display Delete menu.
6. Within that Delete menu,
(i) If Choice=1, Execute deletion at beginning.
(ii) If Choice=2, Execute deletion at the end.
(iii) If Choice=3, Execute deletion at the random location.
(iv) Otherwise print “Wrong Choice”.
7. Otherwise print “Wrong Choice”.

R.Kavibharath
1901102
Program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head=NULL,*q,*t;
int main()
{
int ch;
void insert_beg();
void insert_end();
int insert_pos();
void display();
void delete_beg();
void delete_end();
int delete_pos();
while(1)
{
printf("Singly Linked List(SLL) Menu");
printf("\n1.Insert\n2.Display\n3.Delete\n4.Exit\n\n");
printf("Enter your choice(1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nInsert Menu");
printf("\n1.Insert at beginning\n2.Insert at end\n3.Insert at specified position\
n4.Exit");
printf("\n\nEnter your choice(1-4):");
R.Kavibharath
1901102
scanf("%d",&ch);
switch(ch)
{
case 1: insert_beg();
break;
case 2: insert_end();
break;
case 3: insert_pos();
break;
case 4: exit(0);
default: printf("Wrong Choice!!");
}
break;
case 2: display();
break;
case 3: printf("\nDelete Menu");
printf("\n1.Delete from beginning\n2.Delete from end\n3.Delete from specified
position\n4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1: delete_beg();
break;
case 2: delete_end();
break;
case 3: delete_pos();
break;
case 4: exit(0);
default: printf("Wrong Choice!!");
}

R.Kavibharath
1901102
break;
case 4: exit(0);
default: printf("Wrong Choice!!");
}
}
}
void insert_beg()
{
int num;
t=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
t->data=num;
if(head==NULL)
{
t->next=NULL;
head=t;
}
else
{
t->next=head;
head=t;
}
}
void insert_end()
{
int num;
t=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);

R.Kavibharath
1901102
t->data=num;
t->next=NULL;
if(head==NULL)
{
head=t;
}
else
{
q=head;
while(q->next!=NULL)
q=q->next;
q->next=t;
}
}
int insert_pos()
{
int pos,i,num;
if(head==NULL)
{
printf("List is empty!!");
return 0;
}
t=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
printf("Enter position to insert:");
scanf("%d",&pos);
t->data=num;
q=head;
for(i=1;i<pos-1;i++)

R.Kavibharath
1901102
{
if(q->next==NULL)
{
printf("There are less elements!!");
return 0;
}
q=q->next;
}
t->next=q->next;
q->next=t;
return 0;
}
void display()
{
if(head==NULL)
{
printf("List is empty!!");
}
else
{
q=head;
printf("The linked list is:\n");
while(q!=NULL)
{
printf("%d->",q->data);
q=q->next;
}
}
}
void delete_beg()

R.Kavibharath
1901102
{
if(head==NULL)
{
printf("The list is empty!!");
}
else
{
q=head;
head=head->next;
printf("Deleted element is %d",q->data);
free(q);
}
}
void delete_end()
{
if(head==NULL)
{
printf("The list is empty!!");
}
else
{
q=head;
while(q->next->next!=NULL)
q=q->next;
t=q->next;
q->next=NULL;
printf("Deleted element is %d",t->data);
free(t);
}
}

R.Kavibharath
1901102
int delete_pos()
{
int pos,i;
if(head==NULL)
{
printf("List is empty!!");
return 0;
}
printf("Enter position to delete:");
scanf("%d",&pos);
q=head;
for(i=1;i<pos-1;i++)
{
if(q->next==NULL)
{
printf("There are less elements!!");
return 0;
}
q=q->next;
}
t=q->next;
q->next=t->next;
printf("Deleted element is %d",t->data);
free(t);
return 0;
}

R.Kavibharath
1901102
Output:

Result:
Thus, the Singly Linked List was executed successfully.

R.Kavibharath
1901102

You might also like