Singly Linked List Program in C
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
typedef struct
{
node *start;
}head;
void insertbeg(head *t, int ele)
{
node *p;
p=(node*)malloc(sizeof(node));
p->data=ele;
p->next=t->start;
t->start=p;
}
void insertend(head *t, int ele)
{
node *p,*q;
p=(node*)malloc(sizeof(node));
p->data=ele;
p->next=NULL;
if(t->start==NULL)
t->start=p;
else
{
q=t->start;
while(q->next!=NULL)
q=q->next;
q->next=p;
}
}
void display(head *t)
{
node *q;
if(t->start==NULL)
{
printf("LL Empty\n");
return;
}
q=t->start;
while(q!=NULL)
{
printf("%d ",q->data);
q=q->next;
}
printf("\n");
}
int deletebeg(head*t)
{
node *p;
if(t->start==NULL)
{
printf("LL Empty\n");
return -1;
}
p=t->start;
t->start=t->start->next;
return(p->data);
}
int deleteend(head*t)
{
node *p,*q;
if(t->start==NULL)
{
printf("LL Empty\n");
return -1;
}
if(t->start->next==NULL)
{
p=t->start;
t->start=NULL;
}
else
{
p=t->start;
while(p->next!=NULL)
{
q=p;
p=p->next;
}
q->next=NULL;
}
return(p->data);
}
void deleteele(head*t,int ele)
{
node * q;
if(t->start==NULL)
{
printf("LL Empty\n");
return;
}
if(ele==t->start->data)
{
t->start=t->start->next;
return;
}
q=t->start;
while(q->next!=NULL)
{
if (q->next->data==ele)
break;
else q=q->next;
}
if(q->next==NULL)
printf("%d is not found\n",ele);
else
{
node *p;
p=q->next;
q->next=p->next;
}
}
int count(head *t)
{
int c=0;
node *q=t->start;
while(q!=NULL)
{
c++;
q=q->next;
}
return c;
}
void sort(head *t)
{
node *i,*j;
for(i=t->start;i->next!=NULL;i=i->next)
{
for(j=t->start;j->next!=NULL;j=j->next)
{
if(j->data>j->next->data)
{
int t=j->data;
j->data=j->next->data;
j->next->data=t;
}
}
}
}
void main()
{
int ch,ele;
head x;
x.start=NULL;
while(1)
{
printf("Enter choice 1:Insertbeg 2:Insertend 3:Display 4:Deletebeg 5:Deleteend 6:Deleteele 7.Count 8.Sort 9:Exit\n");
scanf("%d",&ch);
if(ch==9)
break;
switch(ch)
{
case 1: printf("Enter element to insert\n");
scanf("%d",&ele);
insertbeg(&x,ele);
display(&x);
break;
case 2: printf("Enter element to insert\n");
scanf("%d",&ele);
insertend(&x,ele);
display(&x);
break;
case 3: display(&x);
break;
case 4: if(x.start==NULL)
printf("LL Empty\n");
else
{
ele=deletebeg(&x);
printf("Deleted Element = %d\n",ele);
display(&x);
}
break;
case 5: if(x.start==NULL)
printf("LL Empty\n");
else
{
ele=deleteend(&x);
printf("Deleted Element = %d\n",ele);
display(&x);
}
break;
case 6: printf("Enter element to delete\n");
scanf("%d",&ele);
deleteele(&x,ele);
display(&x);
break;
case 7: printf("Number of nodes=%d\n",count(&x));
break;
case 8: sort(&x);
display(&x);
break;
default:printf("Invalid Choice\n");
}
}
}
OUTPUT:
Enter choice 1:Insertbeg 2:Insertend 3:Display 4:Deletebeg 5:Deleteend 6:Deleteele 7.Count 8.Sort 9:Exit
1
Enter element to insert
12
12
Enter choice 1:Insertbeg 2:Insertend 3:Display 4:Deletebeg 5:Deleteend 6:Deleteele 7.Count 8.Sort 9:Exit
1
Enter element to insert
45
45 12
Enter choice 1:Insertbeg 2:Insertend 3:Display 4:Deletebeg 5:Deleteend 6:Deleteele 7.Count 8.Sort 9:Exit
2
Enter element to insert
76
45 12 76
Enter choice 1:Insertbeg 2:Insertend 3:Display 4:Deletebeg 5:Deleteend 6:Deleteele 7.Count 8.Sort 9:Exit
2
Enter element to insert
98
45 12 76 98
Enter choice 1:Insertbeg 2:Insertend 3:Display 4:Deletebeg 5:Deleteend 6:Deleteele 7.Count 8.Sort 9:Exit
2
Enter element to insert
54
45 12 76 98 54
Enter choice 1:Insertbeg 2:Insertend 3:Display 4:Deletebeg 5:Deleteend 6:Deleteele 7.Count 8.Sort 9:Exit
1
Enter element to insert
5
5 45 12 76 98 54
Enter choice 1:Insertbeg 2:Insertend 3:Display 4:Deletebeg 5:Deleteend 6:Deleteele 7.Count 8.Sort 9:Exit
4
Deleted Element = 5
45 12 76 98 54
Enter choice 1:Insertbeg 2:Insertend 3:Display 4:Deletebeg 5:Deleteend 6:Deleteele 7.Count 8.Sort 9:Exit
5
Deleted Element = 54
45 12 76 98
Enter choice 1:Insertbeg 2:Insertend 3:Display 4:Deletebeg 5:Deleteend 6:Deleteele 7.Count 8.Sort 9:Exit
6
Enter element to delete
12
45 76 98
Enter choice 1:Insertbeg 2:Insertend 3:Display 4:Deletebeg 5:Deleteend 6:Deleteele 7.Count 8.Sort 9:Exit
7
Number of nodes=3
Enter choice 1:Insertbeg 2:Insertend 3:Display 4:Deletebeg 5:Deleteend 6:Deleteele 7.Count 8.Sort 9:Exit
8
45 76 98
Enter choice 1:Insertbeg 2:Insertend 3:Display 4:Deletebeg 5:Deleteend 6:Deleteele 7.Count 8.Sort 9:Exit
9