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

Ingle Linked List: Program-1

This C program implements a single linked list with functions to insert, delete, and display nodes. The insert function allows adding a node to the beginning, middle, or end of the list. The delete function similarly allows removing a node from the beginning, middle, or end. The display function prints out the elements of the linked list. The main function provides a menu to call these functions in a loop until the user exits.

Uploaded by

shalem3
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Ingle Linked List: Program-1

This C program implements a single linked list with functions to insert, delete, and display nodes. The insert function allows adding a node to the beginning, middle, or end of the list. The delete function similarly allows removing a node from the beginning, middle, or end. The display function prints out the elements of the linked list. The main function provides a menu to call these functions in a loop until the user exits.

Uploaded by

shalem3
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Program-1: SINGLE LINKED LIST

#include<stdio.h>
#include<alloc.h>
#include<conio.h>
struct node
{
int item;
struct node *next;
}*start,*neww,*new1,*l;
void display()
{
l=start;
printf("\nthe elements are:");
while(l->next!=NULL)
{
printf("%d\t",l->item);
l=l->next;
}
printf("%d\t",l->item);
}
struct node *create(int e)
{
neww=(struct node*)malloc(sizeof(struct node));
neww->item=e;
neww->next=NULL;
return neww;
}
void insert()
{
int e,n,p;
printf("enter the element to insert:\t");
scanf("%d",&e);
if(start==NULL)
{
start=(struct node*)malloc(sizeof(struct node));
start->item=e;
start->next=NULL;
}
else
{
new1=(struct node*)malloc(sizeof(struct node));
new1=create(e);
printf("\nelement to be inserted at start or middle or at end\n");
printf("\n1.start\n2.middle\n3.end");
scanf("%d",&n);
if(n==1)
{
new1->next=start;
start=new1;
}
else if(n==2)
{
printf("enter the element to search:");
scanf("%d",&p);
l=start;
while(l->item!=p && l->next!=NULL)
{
l=l->next;
}
if(l->next==NULL)
printf("no such element");
else
{
new1->next=l->next;
l->next=new1;
}
}
else if(n==3)
{
l=start;
while(l->next!=NULL)
{
l=l->next;
}
l->next=new1;
new1->next=NULL;
}
}
}
void del()
{
int n,e,p;
if(start==NULL)
{
printf("no nodes to delete");
}
else
{
printf("enter values of the node to delete\n");
printf("\n1.start\n2.middle\n3.end\n");
scanf("%d",&n);
if(n==1)
{
e=start->item;
printf("the deleted element is\t%d",e);
start=start->next;
}
else if(n==2)
{
int p,e;
printf("enter data item to search:");
scanf("%d",&p);
l=start;
while(l->next->item!=p && l->next!=NULL)
{
l=l->next;
}
if(l->next==NULL)
printf("no such element");
else
{
e=l->next->item;
printf("the deleted element is:\t%d",e);
l->next=l->next->next;
}
}
else if(n==3)
{
int e;
l=start;
while(l->next->next!=NULL)
{
l=l->next;
}
e=l->next->item;
printf("the deleted element is\t%d",e);
l->next=NULL;
}
}
}
void main()
{
int ch;
clrscr();
start=NULL;
while(1)
{
printf("\nenter your choice:");
printf("\n1.insert\n2.delete\n3.display\n4.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:insert();
break;
case 2:del();
break;
case 3:display();
break;
case 4:exit(0);
}
}
}

You might also like