1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Linked List Using C

Discussion in 'C' started by angad_aks, Feb 15, 2011.

  1. angad_aks

    angad_aks Banned

    Joined:
    Feb 15, 2011
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<malloc.h>
    struct node
    {
     int data;
     struct node *next;
    };
    struct node *head=NULL;
    void create_list();
    void display_list();
    void ins_beg();
    void del_beg();
    void ins_aft_data(int x);
    void ins_aft_node(int count);
    void ins_end();
    void del_after_data(int x);
    void del_after_node(int count);
    void del_end();
    void main()
    {
     int ch,x,count;
     clrscr();
     while(1)
     {
      printf("\nenter,1 for create,2 for display,3 for insert beginning,4 for ins after data,5 for ins aft node,6 for ins end,7 for del end,8 for del after data,9 for del after node,10 for del end,11 for exit");
      printf("\nenter choice");
      scanf("%d",&ch);
      switch(ch)
      {
       case 1:
    		 create_list();
    		 break;
       case 2:
    		 display_list();
    		 break;
       case 3:
    		 ins_beg();
    		 break;
       case 4:
    		 printf("\n enter data after which a node will be inserted");
    		 scanf("%d",&x);
    		 ins_after_data(x);
    		 break;
       case 5:
    		 printf("enter count \n");
    		 scanf("%d",&count);
    		 ins_after_node(count);
    		 break;
      case 6:
    		 ins_end();
    		 break;
      case 7:
    		 del_beg();
    		 break;
      case 8:
    		printf("\n enter data to be deleted");
    		scanf("%d",&x);
    		del_after_data(x);
    		break;
      case 9:
    		printf("enter count \n");
    		scanf("%d",&count);
    		del_after_node(count);
    		break;
      case 10:
    		del_end();
    		break;
      case 11:
    		return;
      }
     }
    }
    void create_list()
    {
     struct node *p,*t;
     if(head!=NULL)
     {
      printf("\n list already exist");
      return;
     }
     p=(struct node *)malloc(sizeof(struct node));
     printf("\n enter data:");
     scanf("%d",&(p->data));
     p->next=NULL;
     if(head==NULL)
       head=p;
     }
    
    
    void ins_beg()
    {
     struct node *p;
     if(head==NULL)
     {
      printf("\n no list exist");
      return;
     }
     p=(struct node *)malloc(sizeof(struct node));
     printf("\n enter data");
     scanf("%d",&(p->data));
    
     p->next=head;
     head=p;
    
    }
    void display_list()
    {
     struct node *t;
     if(head==NULL)
     {
      printf("\n no list exist");
     return;
     }
    
       t=head;
       while(t!=NULL)
       {
    	printf("%d",t->data);
    	t=t->next;
       }
     }
    void del_beg()
    {
     struct node *t;
     if(head==NULL)
      printf("\n no list exist");
     else
     {
      t=head;
      head=t->next;
      free(t);
     }
     return;
    }
    void ins_aft_data(int x)
    {
     struct node *t;
     if(head==NULL)
     {
      printf("\n no list exist");
      return;
     }
     t=head;
     while(t!=NULL)
     {
      if((t->data)==x)
      {
       struct node *p;
       p=(struct node *)malloc(sizeof(struct node));
       printf("\n enter data");
       scanf("%d",&(p->data));
       p->next=t->next;
       t->next=p;
       return;
      }
      else
      {
       t=t->next;
      }
      if(t==NULL)
      {
       printf("there is no such data,so insertion failed");
      }
     }
    }
    void ins_aft_node(int count)
    {
     struct node *t;
     if(head==NULL)
     {
      printf("\n no list exist");
      return;
     }
     t=head;
     while(count>1 && t!=NULL)
     {
      t=t->next;
      count=count-1;
     }
     if(t!=NULL)
     {
      struct node *p;
      p=(struct node *)malloc(sizeof(struct node));
      printf("\n enter data");
      scanf("%d",&(p->data));
      p->next=t->next;
      t->next=p;
     }
     else
     {
      printf("\n insertion failed");
     }
    }
    void ins_end()
    {
     struct node *p,*t;
     if(head==NULL)
     {
      printf("\n no list exist");
     }
     else
     {
      t=head;
      while((t->next)!=NULL)
      {
       t=t->next;
      }
      p=(struct node *)malloc(sizeof(struct node));
      printf("\n enter data");
      scanf("%d",&(p->data));
      t->next=p;
      p->next=NULL;
     }
    }
    void del_after_data(int x)
    {
     struct node *p,*t;
     if(head==NULL)
     {
      printf("\n no list exist");
     }
     else if((head->data)==x && (head->next)==NULL)
     {
      free(head);
      head=NULL;
     }
     else if((head->data)==x && (head->next)!=NULL)
     {
      p=head;
      head=p->next;
      free(p);
     }
     else
     {
      t=head;
      while((t->next->data)!=x && (t->next)!=NULL)
      {
       t=t->next;
      }
      if((t->next)==NULL)
      {
       printf("\n deletion failed");
      }
      else
      {
       p=t->next;
       t->next=p->next;
       free(p);
      }
     }
    }
    void del_end()
    {
     struct node *t,*p;
     if(head==NULL)
     {
      printf("\n no list exist");
     }
     else if((head->next)==NULL)
     {
      t=head;
      free(t);
      head=NULL;
     }
     else
     {
      t=head;
      while((t->next->next)!=NULL)
      {
       t=t->next;
      }
      p=t->next;
      t->next=NULL;
      free(p);
     }
    }
    void del_after_node(int count)
    {
     struct node *t,*p;
     if(head==NULL)
     {
      printf("\n no list exist");
     }
     else if(count>1 && (head->next)==NULL)
     {
      t=head;
      while((t->next)!=NULL)
      {
       t=t->next;
      }
      count=count-1;
     }
     else if((head->next)==NULL)
     {
      free(head);
      head=NULL;
     }
     else if
     {
      free(head);
      head=NULL;
     }
     else
     {
      p=head;
      head=p->next;
      free(p);
      if((t->next)==NULL)
       printf("\n deletion failed");
      else
      {
       p=t->next;
       t->next=p->next;
       free(p);
      }
     }
    }
    
    
    
    
    
    
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    clrscr(), getch(), single letter variable names, no comments or explanation, void main(). Please stop posting code until you can do a better job of it.
     
  3. angad_aks

    angad_aks Banned

    Joined:
    Feb 15, 2011
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    sir i am very new to this forum. but ya tryd . n it ws not shw off. n i gues dis is pretty discouraging 4m such a highly reputed ppl.
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Well, I don't understand most of that as you seem to have half the letters missing on your keyboard. When new to a forum, it's always worth hanging around for a while to see what's going on. Don't just wade in with posts that you don't know will be appreciated.

    But don't be discouraged; keep on programming, but be aware of the fact that until you've been programming every day for 8 hours a day for at least 2 years, you're not going to be anything like an expert on it. Don't post demo code until you are an expert and have something worth posting.
     
  5. lionaneesh

    lionaneesh New Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    India
    Please dont use SMS language here...
    Its difficult for us to read and it seems too unprofessional..
    Try and understand...

    Yes , He's right....First of all angad be prepared to get the warning and negative feedbacks as at some point i used to get that too and still i get it many a times...So don't ever be discourages as you'll ultimately end up not programming!!!!
     

Share This Page