Program to implement Linked List in C C++

Following is the program to implement linked list in c  c++

PROGRAM:

#include<stdio.h>
#include<malloc.h>
#include<process.h>

struct node
{
 int info;
 struct node *next;
};

typedef struct node sn;

void insertatbeg(sn **,int);
void insertatend(sn **,int);
void insertatpos(sn **,int,int);
void traverse(sn *);
int delfirst(sn *);
int dellast(sn *);
int delpos(sn *,int);
int count(sn *);

int main()
{
sn *p;
int n,i,l;
char ch;
p=NULL;
do
{
 printf("n-----SELECT AN OPERATION-----");
 printf("n1->To insert an item at begining<-");
 printf("n2->To insert an item at end<-");
 printf("n3->To insert an item at position<-");
 printf("n4->To traverse the list<-");
 printf("n5->To delete first item<-");
 printf("n6->To delete last item<-");
 printf("n7->To delete item from position<-");
 printf("n8->To count no. of items in list<-");
 printf("n9->Exit<-");
 printf("n->Enter your choice:");
 scanf("%d",&n);
 switch(n)
 {
  case 1:printf("n->Enter the item to insert: ");
  scanf("%d",&i);
  insertatbeg(&p,i);
  break;

  case 2:printf("n->Enter the item to insert: ");
  scanf("%d",&i);
  insertatend(&p,i);
  break;

  case 3:printf("n->Enter the item to insert: ");
  scanf("%d",&i);
  printf("n->Enter the position: ");
  scanf("%d",&l);
  insertatpos(&p,i,l);
  break;

  case 4:traverse(p);
  break;

  case 5:i=delfirst(p);
  printf("nItem deleted is %d.",i);
  break;

  case 6:i=dellast(p);
  printf("nItem deleted is %d.",i);
  break;

  case 7:printf("n->Enter the position");
  scanf("%d",&l);
  i=delpos(p,l);
  printf("nItem deleted is %d.",i);
  break;

  case 8:i=count(p);
  printf("nNumber of nodes in list are %d.",i);
  break;

  case 9:exit(0);
  break;
  default:printf("n->You have entered wrong choice");
  }
 printf("n->Do you want to continue/exit(y/n)");
 fflush(stdin);
 scanf("%c",&ch);
 } while((ch=='y')||(ch=='Y'));
}

int count(sn *p)
{
 sn *temp;
 int c=1;
 temp=p;
 while((temp->next)!=NULL)
 {
  c++;
  temp=temp->next;
 }
 return c;
}
void insertatbeg(sn **p,int item)
{
 sn *ptr;
 ptr=(sn *)malloc(sizeof(sn));
 ptr->info=item;
 if(*p==NULL)
 ptr->next=NULL;
 else
 ptr->next=*p;
 *p=ptr;
}
void insertatend(sn **p,int item)
{
 sn *ptr,*temp;
 ptr=(sn *)malloc(sizeof(sn));
 ptr->info=item;
 ptr->next=NULL;
 if(*p==NULL)
 *p=ptr;
 else
 { temp=*p;
 while((temp->next)!=NULL)
 temp=temp->next;
 temp->next=ptr;
 }
}
void insertatpos(sn **p,int item,int loc)
{
 sn *ptr,*temp,*s;
 int i,c;
 temp=*p;
 ptr=(sn *)malloc(sizeof(sn));
 ptr->info=item;
 c=count(*p);
 if(loc>c)
  printf("nList is short,Item can't inserted");
 else
  {
  for(i=0;i<loc-1;i++)
  { s=temp;
  temp=temp->next;
  }
  ptr->next=s->next;
  s->next=ptr;
  }
}

void traverse(sn *p)
{
 sn *temp;
 if(p==NULL)
  printf("nList is empty.");
 else
 {
  temp=p;
  while((temp->next)!=NULL)
  {
   printf("nno.=%d",temp->info);
   temp=temp->next;
  }
 printf("nno.=%d",temp->info);
 }
}

int delfirst(sn *p)
{
 int item;
 sn *temp;
 if(p==NULL)
 {
  printf("nList is empty");
  return 0;
 }
 else
 {
  temp=p;
  p=p->next;
  item=temp->info;
  free(temp);
  return item;
 }
}

int dellast(sn *p)
{
 int item;
 sn *temp,*s;
 if(p==NULL)
 {
  printf("nList is empty");
  return 0;
 }
 else
 {
  temp=p;
  while((temp->next)!=NULL)
  {
   s=temp;
   temp=temp->next;
  }
 item=temp->info;
 s->next=NULL;
 free(temp);
 return item;
 }
}

int delpos(sn *p,int loc)
{
 int item,i,c;
 sn *temp,*s;
 c=count(p);
 if(p==NULL)
 {
  printf("nList is empty");
  return 0;
 }
 else
 {
  if(loc>c)
  {
   printf("nItem is not in list.");
   return 0;
  }
  else
  { temp=p;
   for(i=0;i<loc-1;i++)
   {
    s=temp;
    temp=temp->next;
   }
   item=temp->info;
  s->next=temp->next;
  free(temp);
  return item;
  }
 }
}

 

Leave a Comment