1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Why the code is not working?

Discussion in 'Meet and Greet' started by sumedha, Mar 30, 2010.

  1. sumedha

    sumedha New Member

    Joined:
    Mar 30, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<malloc.h>
       
    struct node
    {
     int num;
     struct node *next;
    };
    typedef struct node NODE;
    NODE *start=NULL;
    NODE *last=NULL;
    void eledisplay();
    eleinsert(NODE *start);
    eledelete(NODE *start);
    void main()
    {
     int ch;
     do
     {
      printf("1.insert\n");
      printf("2.Delete\n");
      printf("3.Display\n");
      printf("4.Exit");
      printf("Enter ur choice");
      scanf("%d",&ch);
      switch(ch)
      {
      case 1:start=eleinsert(start);
       break;
      case 2:eledelete(start);
      break;
      case 3:eledisplay();
       break;
      case 4:return;
      }
     }
      while(ch!=4);
    }
    NODE *eleinsert(NODE *start)
    {
     NODE *p;
     p=(NODE *)malloc(sizeof(NODE));
      printf("Enter ele to insert\n");
     scanf("%d",&p->num);
     if(start==NULL)
     {
      p->next=p;
      start=p;
      last=p;
     }
     else
     {
      p->next=start;
      start=p;
      last->next=p;
     }
     return start;
    }
    
    void eledisplay()
    {
     NODE *p;
     p=start;
     if(p==NULL)
     {
      printf("List is empty");
     }
     else
     {
      while(p->next!=start)
      {
       printf("%d",p->num );
       p=p->next;
      }
     }
     
    }
    NODE *eledelete(NODE *start)
    {
     NODE *p;
     p=start;
     if(p==NULL)
     {
      printf("List is empty");
     }
     else
     {
      p=start;
      start=start->next;
      printf("Deleted ele is:%d",p->num);
      last->next=p;
     }
     return  start;
    }
     
    Last edited by a moderator: Mar 30, 2010
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Who says its not working. I am sure it is working but not as you expect it to?
     
  3. sumedha

    sumedha New Member

    Joined:
    Mar 30, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    ya,Its working bt
    When all nodes r deleted, it stiil displays deleted nodes.
    Why?
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Your deletion logic is not at all right. Search the forum and you will find code on how to delete a node in a linked list
     

Share This Page