0% found this document useful (0 votes)
2 views

Single Linked List

The document is a C program that implements a singly linked list with functionalities for insertion and deletion of nodes at various positions. It includes functions to add nodes at the beginning, end, or a specific position, as well as to delete nodes from those positions. The program also provides a user interface for interacting with the linked list through a menu-driven approach.

Uploaded by

amanvishwari
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Single Linked List

The document is a C program that implements a singly linked list with functionalities for insertion and deletion of nodes at various positions. It includes functions to add nodes at the beginning, end, or a specific position, as well as to delete nodes from those positions. The program also provides a user interface for interacting with the linked list through a menu-driven approach.

Uploaded by

amanvishwari
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

//Insertion and deletion on singly linked list

#include <stdio.h>
#include<stdlib.h>
struct list{
int data;
struct list *next;
};
// Creation of node
struct list *makenode()
{
struct list*p;
p=(struct list*)malloc(sizeof(struct list));
p->next=0;
printf("Enter the list....");
scanf("\n%d",&p->data);
return p;
}
//Add node at first position
struct list *addfirst(struct list*h)
{
struct list*p=makenode();
p->next=h;
return p;
}
//Add node at last
struct list *addlast(struct list*h)
{
struct list *p,*t ;
p=makenode();
if(h==0)
return p;
t=h;
while(t->next!=0)
t=t->next;
t->next=p;
return h;
}
// Add node at specific position
struct list *addpos(struct list *h)
{
struct list *p,*t;
int pos,cnt=0 ,i;
t=h;
while(t)
{
cnt++;
t=t->next;
}
printf("\nEnter the position");
scanf("%d",&pos);
if(pos<1||pos>cnt+1)
{
printf("invalid operation.....");
return h;
}
if(pos==1){
h=addfirst(h);
return h;
}
if(pos==cnt+1)
{
h=addlast(h);
return h;
}
t=h;
for(i=2;i<pos;i++){
t=t->next;
}
p=makenode();
p->next=t->next;
t->next=p;
return h;
}
//Delete first node
struct list *delfirst(struct list*h)
{
struct list*p;
if(h==0){
printf("THERE IS NO ELEMENT IN THE LIST");
return h;
}
p=h;
h=h->next;
printf("\n Delete: %d",p->data);
free(p);
return h;
}
// Delete last node
struct list *dellast(struct list*h)
{
struct list *p,*t ;

if(h==0){
printf("THERE IS NO ELEMENT IN THE LIST");
return h;
}
if(h->next==0){
printf("\n Delete:%d",h->data);
free(h);
return 0;
}
t=h;
while(t->next->next !=0)
t=t->next;
p=t->next;
t->next=0;
printf("\n Delete:%d",h->data);
free(p);
return h;
}
// Delete node from specific position
struct list *delpos(struct list *h)
{
struct list *p,*t;
int pos,cnt=0 ,i;
t=h;
while(t)
{
cnt++;
t=t->next;
}
printf("\nEnter the position");
scanf("%d",&pos);
if(pos<1||pos>cnt)
{
printf("invalid operation.....");
return h;
}
if(pos==1){
h=delfirst(h);
return h;
}
if(pos==cnt)
{
h=dellast(h);
return h;
}
t=h;
for(i=2;i<pos;i++)
{
t=t->next;
}
p=t->next;
t->next=p->next;
printf("Deleting... %d",p->data);
return h;
}
// Display the list
void display(struct list *h)
{
if(!h)
printf("\n Empty list....");
while(h)
{
printf("\n %d", h->data);
h=h->next;
}
}
void main()
{
struct list *head=0;
int ch;
while(1)
{
//clrscr();
printf("\n1..... Add the node at First");
printf("\n2..... Add the node at Last");
printf("\n3..... Add the node at specific");
printf("\n4..... Delete the node at First");
printf("\n5..... Delete the node at last");
printf("\n6..... Delete the node at specific");
printf("\n7......Display the elements");
printf("\n8......Exit");
printf("\n Enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1: head=addfirst(head); break;
case 2: head=addlast(head); break;
case 3: head=addpos(head); break;
case 4: head=delfirst(head); break;
case 5: head=dellast(head); break;
case 6: head=delpos(head); break;
case 7: display(head); break;
default: printf("\n Thanks");
exit(0);
}
}
}

You might also like