C Program to Implement a Linked List

#include<stdio.h>
#include<stdlib.h>

struct node    //Structure node of the linked list...
{
 int data;
 struct node* next;
};

int flag;     // Temporary variable to handle the error condition in deleteatfront,deleteatend functions...

struct node* insertatfront(struct node* thead,int x);
struct node* insertatend(struct node* thead,int x);
struct node* insertatposition(struct node* thead,int pos,int x);
struct node* deleteatposition(struct node* thead,int pos,int *pans);
struct node* deleteatfront(struct node* thead,int *pans);
struct node* deleteatend(struct node* thead,int *pans);
void printlist(struct node* thead);

void main()
{
 struct node* head=NULL;
 int breakcondition=1;  // Temporary variable to exit the menu driven program...
 printf("Empty Linked list is created..");
 int ch,x,p;
 printf("\nChoice : \n1) Insert at Front.\n2) Insert at End.\n3) Delete at Front\n4) Delete at End.\n5) Print the list.\n6) Insert at a position.\n7) Delete at a position.\n8) Length of Linked list.\n9) Stop..");
 while(1)
{
 printf("\nEnter your choice..");
 scanf("%d",&ch);
 switch(ch)
 {
  case 1:{
          printf("\nEnter any element you want to insert...");
          scanf("%d",&x);
          head=insertatfront(head,x);
          printlist(head);
          break;
         }
  case 2:{
          printf("\nEnter any element you want to insert...");
          scanf("%d",&x);
          head=insertatend(head,x);
          printlist(head);
          break;
}
  case 3:{
          int ans;
          flag=0;
          head=deleteatfront(head,&ans);
          if(flag==0)
          printf("\nThe element which is deleted is ..%d",ans);
          printlist(head);
          break;
         }
  case 4:{
          int ans;
          flag=0;
          head=deleteatend(head,&ans);
          if(flag==0)
          printf("\nThe element which is deleted is ..%d",ans);
          printlist(head);
          break;
         }
  case 5:{
          printlist(head);
          break;
         }
  case 6:{
          printf("\nEnter any element you want to insert and its position..");
          scanf("%d%d",&x,&p);
          head=insertatposition(head,p,x);
          printlist(head);
          break;
          }
  case 7:{
          int ans;
          flag=0;
          printf("\nEnter the position..");
          scanf("%d",&p);
          head=deleteatposition(head,p,&ans);
          if(flag==0)
          printf("\nThe element which is deleted is ..%d",ans);
          printlist(head);
          break;
          }
  case 8:{
          int len=length(head);
          if(len==0){
            printf("\nThe linked list is empty...");
          }
          else {
            printf("\nThe length of the linked list is..%d",len);
          }
          break;
          }
  case 9:{
          breakcondition=0;
          break;
         }
  default : printf("\nPlease Enter a valid choice..");

 }
 if(breakcondition==0)
   break;
}
}


struct node* insertatfront(struct node* thead,int x)
{
 struct node* newnode = (struct node*)malloc(sizeof(struct node));
 newnode->data=x;
 newnode->next=thead;
 return newnode;
}
struct node* insertatend(struct node* thead,int x)
{
 struct node* newnode = (struct node*)malloc(sizeof(struct node));
 newnode->data=x;
 if(thead==NULL) // Corner Case to handle if the list is empty...
 {
  newnode->next=NULL;
  return newnode;
 }
 struct node* trav=thead;
 while(trav->next!=NULL) // Traversing to point to the last node...
   trav=trav->next;
 trav->next=newnode;
 newnode->next=NULL;
 return thead;
}

struct node* deleteatfront(struct node* thead,int *pans)
{
 if(thead==NULL)  // Corner Case to handle if the list is empty...
 {
  flag=1;
  return thead;
 }
 struct node* trav=thead;
 *pans=trav->data;
 trav=trav->next;
 free(thead);
 return trav;
}

struct node* deleteatend(struct node* thead,int *pans)
{
 if(thead==NULL)  // Corner Case to handle if the list is empty...
 {
  flag=1;
  return thead;
 }
 if(thead->next==NULL) // Corner Case to handle if the list contains only one element...
 {
  struct node* temp=thead;
  thead=NULL;
  free(temp);
  return thead;
 }
 struct node* trav=thead;
 while(trav->next->next!=NULL)  // Traversing to point to the last before node...
  trav=trav->next;
 struct node* temp=trav->next;
 *pans=temp->data;
 free(temp);
 trav->next=NULL;
 return thead;
}


void printlist(struct node* thead)
{
 if(thead==NULL)  // Corner Case to handle if the list is empty...
  {
   printf("\nThe list is Empty..");
   return;
  }
 printf("\nThe list is..");
 while(thead!=NULL)  // Traversing the entire list...
  {
   printf("%d ",thead->data);
   thead=thead->next;
  }
}

struct node* insertatposition(struct node* thead,int pos,int x)
{
    int len=length(thead);
    if(pos<=0||pos>len+1){
        printf("\nThe position is invalid..");
        return thead;
    }
    struct node* newnode = (struct node*) malloc(sizeof(struct node));
    newnode->data=x;
    struct node* trav;
    int i;
    trav=thead;
    if(pos==1){              // Corner Case to handle if the position is 1...
        newnode->next=thead;
        thead=newnode;
        return thead;
    }
    for(i=1;i<=pos-2;i++){   // Traversing the trav pointer pos-2 times from the head...
        trav=trav->next;
    }
    newnode->next=trav->next;
    trav->next=newnode;
    return thead;
}

struct node* deleteatposition(struct node* thead,int pos,int *pans)
{
    int len=length(thead);
    if(pos<=0||pos>len){          // Corner Case to handle if the position is invalid...
        printf("\nThe position is invalid..");
        flag=1;
        return thead;
    }
    struct node* front,* current;
    front=current=thead;
    int i;
    if(thead==NULL){             // Corner Case to handle if the list is empty...
        printf("\nThe List is already Empty..");
        flag=1;
        return thead;
    }
    if(pos==1){                  // Corner Case to handle if the position is 1...
        *pans=current->data;
        current=current->next;
        free(front);
        return current;
    }
    for(i=1;i<=pos-1;i++){      // Traversing the current pointer pos-1 times from the head...
        front=current;
        current=current->next;
    }
    *pans=current->data;
    front->next=current->next;
    free(current);
    return thead;
}

int length(struct node* thead)
{
    int c=0;
    while(thead!=NULL){           //Traversing the entire list and incrementing c...
        c++;
        thead=thead->next;
    }
    return c;
}

No comments:

Post a comment