SINGLE LINKED LIST PROGRAM IN C (DATA STRUCTURES)


#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*p;
int count()
{
struct node *n;
int c=0;
n=p;
while(n!=NULL)
{
n=n->next;
c++;
}
return(c);
}//function to delete a node from the front of list //
void delete_front()
{
struct node *n;
//if the list is empty
if(p==NULL)
{
printf("\n empty linked list deletion not possible");
}
else
{
n=p;
p=p->next;
printf("the deleted is %d",n->data);
free(n);
printf("\n");
}
}
void delete_end()
{
struct node *r,*t;
if(p==NULL)
{
printf("\nempty linked list deletion not possible");

}
else
{
if(count()==1)
{
r=p;
p=p->next;
printf("\nthe deleted node from the end is %d,r->data");
free(r);
}
else
{
//traverse to end of list
r=p;
while(r->next!=NULL)
{
t=r;
r=r->next;
}
t->next=r->next;
printf("\nthe delete node from the end is %d",r->data);
free(r);
}
}
}
//this function deletes a linked list //
void delnode(int loc)
{
int i;
struct node *r,*t;
if(p==NULL)
{
printf("\nempty linked list.deletion not possible");
}
else
{
if(loc>0 && loc<=count())
{
if(loc==1)
{
r=p;
p=p->next;
printf("\n node with data %d deleted.\n",r->data);
free(r);
}
else
{
r=p;
for(i=1;i<loc;i++)
{
t=r;
r=r->next;
}
t->next=r->next;
printf("\nnode with data %d deleted\n",r->data);
free(r);
}
}
else
{
printf("\n deletion not possible");
}
}
}
// this iis function adds a node at the last of l.l//
void append(int num)
{
struct node *temp,*r;
// creating a node assign value to it//
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
r=(struct node *)p;
if(p==NULL)
{
p=temp;
p->next=NULL;
}
else
{
while(r->next!=NULL)
r=r->next;
r->next=temp;
r=temp;
r->next=NULL;
}
}
void addbeg(int num)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
if(p==NULL)
{
p=temp;
p->next=NULL;
}
else
{
temp->next=p;
p=temp;
}
}
void addafter(int num,int loc)
{
int i,n;
struct node *temp,*t,*r;
r=p;
n=count();
if(loc>n+1||loc<=0)
{
printf("insretion is not possible");
return;
}
if(loc==1)
{
addbeg(num);
return;
}
else
{
for(i=1;i<loc;i++)
{
t=r;
r=r->next;
}
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
t->next=temp;
t->next=r;
}
}
void display(struct node *r)
{
r=p;
if(r==NULL)
{
printf("no element in the list");
return;
}
while(r!=NULL)
{
printf("->%d",r->data);
r=r->next;
}
printf("\n");
}
void reverse(struct node *q)
{
struct node *m,*n,*s;
m=q;
n=NULL;
while(m!=NULL)
{
s=n;
n=m;
m=m->next;
n->next=s;
}
p=n;
}
struct node *n;
void main()
{
int i;
p=NULL;
while(1)
{
                    printf("\n 1.insert a number at beginning");
                    printf("\n2.insert a number at last");
printf("\n3.insert a number at particular location in list");
          printf("\n4 print the elements");
printf("\n5.count the elements in list");
printf("\n6.reverse a linked list");
          printf("\n7.delete a node in list");
 printf("\n8.delete a front node in list");
                              printf("\n9.delete a last node");
                    printf("\n10.get out of the list(byee byee)");
                              printf("\nenter u r choice ");
                              scanf("%d",&i);
                                        switch(i)
{
case 1:
{
int num;
                                        printf("please  enter number");
                                                  scanf("%d",&num);
                                                            addbeg(num);
                                                            break;
}
case 2:
                                                                                {
                                                  int num;
                                                  printf("please enter number");
                                                            scanf("%d",&num);
                                                            append(num);
break;
}
case 3:
{
                                        int num, loc;
                                        printf("plz enter the number");
                                        scanf("%d",&loc);
                              printf("please enter number");
                              scanf("%d",&num);
                                        addafter(num,loc);
                              break;
}
case 4 :
{
printf("the elements in list are");
display(n);
break;
}
case 5 :
{
printf("total number of elements are %d",count());
break;
}
case 6:
{
reverse(p);
display(p);
break;
}
case 7 :
{
int loc;
printf("please enter the location number");
scanf("%d",&loc);
break;
}
case 8:
{
delete_front();
break;
}
case 9 :
{
delete_end();
break;
}
case 10 :
{
exit(0);
}
}
}
}

HOPE ALL WILL GET THE OPERATIONS OF SINGLE LINKED LIST 

Comments

Popular Posts