0% found this document useful (0 votes)
21 views15 pages

Single Linked List

Uploaded by

Haha Haha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views15 pages

Single Linked List

Uploaded by

Haha Haha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Perform the different operations on single linked list

i) Creation ii) Insertion iii) Deletion iv) Traversal

#include<stdio.h>

#include<conio.h>

#include<process.h>

#include<stdlib.h>

struct node

{
int data;

struct node *next;

};
struct node *head=NULL;

void create();

void insert_beg();

void insert_end();

int insert_pos();

void delete_beg();

void delete_end();

int delete_pos();

void display();

int searchNode();

int main( )
{
int ch;
while(1)
{
printf("\n\n---- Single Linked List(SLL) Menu ---- ");
printf("\n1.create");
printf("\n2.Insert");
printf("\n3.Display");
printf("\n4.Delete");
printf("\n5.Search");
printf("\n6.Exit\n\n");
printf("Enter your choice(1-6):");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2: printf("\n---- Insert Menu ---- ");
printf("\n1.Insert at beginning");
printf("\n2.Insert at end");
printf("\n3.Insert at specified position");
printf("\n4.Exit");
printf("\n\nEnter your choice(1-4):");
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 3: display();
break;
case 4: printf("\n---- Delete Menu ---- ");
printf("\n1.Delete from beginning");
printf("\n2.Delete from end");
printf("\n3.Delete from specified position");
printf("\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!!");
}
break;
case 5: searchNode();
break;
case 6: exit(0);
default: printf("Wrong Choice!!");
}
}
return 0;
}

void create()
{
struct node *ptr,*temp;
int i,n,val;
printf("Enter Number of Elements :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
ptr=(struct node*)malloc(sizeof(struct node));
printf("Enter the data of node %d: ", i);
scanf("%d",&val);
ptr->data=val;
ptr->next=NULL;
if(head==NULL)
{
head=ptr;
temp=head;
}
else
{
temp->next=ptr;
temp=temp->next;
}
}
}

void insert_beg()
{
struct node *ptr;
int num;
ptr=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
ptr->data=num;
if(head==NULL)
{
ptr->next=NULL;
head=ptr;
}
else
{
ptr->next=head;
head=ptr;
}
}

void insert_end()
{
struct node *ptr;
int num;
ptr=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
ptr->data=num;
ptr->next=NULL;
if(head==NULL)
{
head=ptr;
}
else
{
struct node *temp;
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=ptr;
}
}
int insert_pos()
{
struct node *ptr;
int pos,i=1,num;
ptr=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
ptr->data=num;
printf("Enter position to insert:");
scanf("%d",&pos);
if(head==NULL)
{
ptr->next=NULL;
head=ptr;
}
else
{
struct node *temp;
temp=head;
while(i<pos-1)
{
temp=temp->next;
i++;
}
ptr->next=temp->next;
temp->next=ptr;
}
return 0;
}
void delete_beg()
{
if(head==NULL)
{
printf("List is Empty!!! Deletion is not possible\n");
}
else
{
struct node *temp;
temp=head;
if(temp->next==NULL)
{
head=NULL;
printf("Deleted element is %d",temp->data);
free(temp);
}
else
{
head=temp->next;
printf("Deleted element is %d",temp->data);
free(temp);
}
}
}
void delete_end()
{
if(head==NULL)
{
printf("List is Empty!!! Deletion is not possible\n");
}

else
{
struct node *temp,*temp1;
temp=head;
if(temp->next==NULL)
{
head=NULL;
printf("Deleted element is %d",temp->data);
free(temp);
}
else
{
while(temp->next!=NULL)
{
temp1=temp;
temp=temp->next;
}
temp1->next=NULL;
printf("Deleted element is %d",temp->data);
free(temp);
}
}
}
int delete_pos()
{
int pos,i=1;
if(head==NULL)
{
printf("List is Empty!!! Deletion is not possible\n");
}
else
{
struct node *temp,*t;
temp=head;
printf("Enter position to delete:");
scanf("%d",&pos);
if(temp->next==NULL)
{
head=NULL;
printf("Deleted element is %d",temp->data);
free(temp);
}
else
{
while(i<pos-1)
{
temp=temp->next;
i++;
}
t=temp->next;
temp->next=t->next;
t->next=NULL;
printf("Deleted element is %d",t->data);
free(t);
}
}
return 0;
void display()
{
if(head==NULL)
{
printf("List is Empty!!!\n");
}
else
{
struct node *temp;
temp=head;
printf("The linked list is:\n");
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("NULL");
}
}
int searchNode()
{
struct node *temp = head;
int key,count=0;
printf("\nEnter the element to be searched in the list : ");
scanf("%d",&key);
while(temp != NULL)
{
if(temp->data == key)
{
printf("\nElement %d found at position %d",key,count);
return 0;
}
else
{
count+=1;
temp = temp->next;
}
}

printf("\n Element %d is not found in the list\n",key);


return 0;
OUTPUT :

You might also like