0% found this document useful (0 votes)
187 views11 pages

Singly Linked List

This document describes functions to implement a singly linked list in C including functions to insert and delete nodes from the beginning, middle, and end of the list as well as a display function. The main function uses a switch case to call the appropriate function based on the user's menu choice. Key functions include create to initialize an empty list, insert functions to add nodes in different positions, delete functions to remove nodes, and display to output the list.

Uploaded by

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

Singly Linked List

This document describes functions to implement a singly linked list in C including functions to insert and delete nodes from the beginning, middle, and end of the list as well as a display function. The main function uses a switch case to call the appropriate function based on the user's menu choice. Key functions include create to initialize an empty list, insert functions to add nodes in different positions, delete functions to remove nodes, and display to output the list.

Uploaded by

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

SINGLY LINKED LIST

#include<conio.h>
#include<stdio.h>
typedef struct node_n
{
int info;
node_n *next;
}node;
node *temp,*start,*q,*r;
void create();
void ins();
void ins_mid();
void ins_end();
void disp();
void del();
void del_mid();
void del_end();
main()
{
int ch;
while(1)
{
printf("\n\nEnter choice\n\n");
printf("\n1.Insert at beg");
printf("\n2.Insert at mid");
printf("\n3.Insert at end");

printf("\n4.Delete at beg");
printf("\n5.Delete at mid");
printf("\n6.Delete at end");
printf("\n7.Display");
printf("\n\nChoice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: ins();
break;
case 2: ins_mid();
break;
case 3: ins_end();
break;
case 4: del();
break;
case 5: del_mid();
break;
case 6: del_end();
break;
case 7: disp();
break;
default: exit(0);

}
}

getch();
}
void create()
{
if(start==NULL)
{
temp=(node *)malloc(sizeof(node *));
printf("\nEnter value: ");
scanf("%d",&temp->info);
temp->next=NULL;
start=temp;
}
else
printf("\nInsert via beginning");
}
void ins()
{
if(start==NULL)
create();
else
{
temp=(node *)malloc(sizeof(node *));
printf("\nEnter value: ");
scanf("%d",&temp->info);
temp->next=start;
start=temp;

}
}
void ins_end()
{
if(start==NULL)
create();
else
{
temp=(node *)malloc(sizeof(node *));
printf("\nEnter value: ");
scanf("%d",&temp->info);
q=start;
while(q->next!=NULL)
q=q->next;
q->next=temp;
temp->next=NULL;
}
}
void ins_mid()
{ int num;
if(start==NULL)
create();
else
{
temp=(node *)malloc(sizeof(node *));
printf("\nEnter value: ");

scanf("%d",&temp->info);
printf("\nEnter value after which you wish to enter: ");
scanf("%d",&num);
q=start;
while(q->info!=num)
q=q->next;
temp->next=q->next;
q->next=temp;
}
}
void disp()
{
q=start;
while(q!=NULL)
{
printf("\n%d",q->info);
q=q->next;
}
}
void del()
{
if(start->next==NULL)
{
q=start;
free(q);
printf("\nEmpty");

}
else
{
q=start;
start=start->next;
free(q);
}
}
void del_end()
{
if(start->next==NULL)
{
q=start;
free(q);
printf("\nEmpty");
}
else
{

q=start;
while(q->next!=NULL)
{
r=q;
q=q->next;
}
r->next=NULL;
free(q);

}
void del_mid()
{
int num;
printf("\nEnter value to be deleted: ");
scanf("%d",&num);
q=start;
while(q->info!=num)
{
r=q;
q=q->next;
}
r->next=q->next;
free(q);
}

//////////////OUTPUT

You might also like