Thread: error in displaying linked list after deletion from middle-please spot it and tell me

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    76

    error in displaying linked list after deletion from middle-please spot it and tell me

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    struct node{
    int data;
    struct node *next;
    };
    void main()
    {
        struct node *head, *tail, *temp,*s,*ptrl,*p;
        int n,f,i;
        char ch;
        head=tail=NULL;
        printf("want to enter data (y/n)\n");
        scanf("%c",&ch);
        if(ch=='y'||ch=='Y'){
    printf("enter your data: \n");
    scanf("%d",&n);
    fflush(stdin);
            tail=(struct node *)malloc(sizeof(struct node));
            tail->data=n;
            tail->next=NULL;
            head=tail;
        printf("\nwant to enter more data (y/n)");
        scanf("%c",&ch);
    fflush(stdin);
        }
     while(ch=='y'||ch=='Y'){
        printf("\nenter your data: ");
    scanf("\n%d",&n);
    fflush(stdin);
            temp=(struct node *)malloc(sizeof(struct node));
            tail->next=temp;
            temp->data=n;
            temp->next=NULL;
            tail=temp;
    printf("\nwant to enter more data (y/n)");
        scanf("\n%c",&ch);
        fflush(stdin);
        }
        printf("\nenter the data you want to delete");
        scanf("\n%d",&f);
        ptrl=head;
    s=head;
        while(s!=NULL){
          if(s->data!=f){
            ptrl=s;
              s=s->next;
            }
          else{
            ptrl->next=s->next;
    i=1;
          }
        }
        p=head;
        if(i==1){
            printf("\nlist after deletion--->");
        while(p!=NULL){
            printf("%d ",p->data);
            p=p->next;
        }}
        else{
            printf("\nelement not found");
        }
      getch();
    }
    the error is after :::: " printf("\nenter the data you want to delete");"
    please spot it and pull this poor man out of this misery
    Last edited by gaurav#; 02-01-2014 at 01:20 PM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    1.
    Declare main as int main(void), not void main() (which is not a standard declaration and yields no benefit). And put a return 0 statement at the end of main.

    2.
    Remove the fflush(stdin) calls as these are not needed and untrustworthy. They may "work" on some systems, but can cause problems on others.

    In order to read a single character that may have whitespace (e.g., a newline) before it, just put a space before the %c in your scanf.

    And remove any \n's you have in your scanf's, as they do absolutely nothing in front of %d and are equivalent to a single space in front of %c.

    3.
    Obviously your indentation and spacing could use some work.

    4.
    Don't bother casting the return value of malloc as this is unnecessary in C (although it is required in C++, but in C++ you'd be using the new operator instead).

    5.
    I'm too lazy to describe the errors in your code, so here's a re-write. Please study the differences.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node{
        int data;
        struct node *next;
    };
    
    int main(void)
    {
        struct node *head, *tail, *temp, *prev, *p;
        int n;
        char ch;
    
        head = tail = NULL;
    
        printf("Do you want to enter data (y/n) ");
        scanf(" %c", &ch);
    
        while (ch == 'y' || ch == 'Y') {
    
            printf("Enter your data: ");
            scanf("%d", &n);
    
            // Create and initialize new node.
            temp = malloc(sizeof(*temp));
            temp->data = n;
            temp->next = NULL;
    
            // Attach new node to list.
            if (head == NULL)
                head = tail = temp;
            else
                tail = tail->next = temp;
        
            printf("Want to enter more data (y/n) ");
            scanf(" %c", &ch);
        }
    
    
        printf("Enter the data you want to delete: ");
        scanf("%d", &n);
        p = head;
        prev = NULL;
        while (p != NULL && p->data != n) {
            prev = p;
            p = p->next;
        }
        if (p != NULL) {
            if (prev == NULL)
                head = p;
            else
                prev->next = p->next;
            free(p);
        }
    
    
        if (p != NULL) {
            printf("List after deletion: ");
            p = head;
            while (p != NULL) {
                printf("%d ", p->data);
                p = p->next;
            }
            putchar('\n');
        } else {
            printf("Element not found.\n");
        }
    
    
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    76
    thanks dude, it really helped~~

  4. #4
    Registered User
    Join Date
    Feb 2014
    Posts
    3
    here is an improved version of the above code which can be used to delete from any position i.e. head node, middle, or the last node::: your code is right but the complexity in the while loop is making it problematic


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include<conio.h>
    struct node{
    int data;
    struct node *next;
    };
    void main()
    {
        struct node *head, *tail, *temp,*s,*ptrl,*p;
        int n,f,i=0;
        char ch;
        head=tail=NULL;
        printf("want to enter data (y/n)\n");
        scanf("%c",&ch);
        if(ch=='y'||ch=='Y'){
    printf("enter your data: \n");
    scanf("%d",&n);
    fflush(stdin);
     tail=(struct node *)malloc(sizeof(struct node));
     tail->data=n;
     tail->next=NULL;
     head=tail;
        printf("\nwant to enter more data (y/n)");
        scanf("%c",&ch);
    fflush(stdin);
        }
     while(ch=='y'||ch=='Y'){
        printf("\nenter your data: ");
    scanf("\n%d",&n);
    fflush(stdin);
     temp=(struct node *)malloc(sizeof(struct node));
     tail->next=temp;
     temp->data=n;
     temp->next=NULL;
     tail=temp;
    printf("\nwant to enter more data (y/n)");
        scanf("\n%c",&ch);
        fflush(stdin);
        }
    printf("\nYour Link list is :\n");
    p=head;  while(p!=NULL){
     printf("%d ",p->data);
     p=p->next;
        }
        printf("\nenter the data you want to delete :");
        scanf("\n%d",&f);
        ptrl=head;
    s=head;
    if(s->data==f)
    {
    head=s->next;
    i=1;
    }
    else
    {
    while(s->next!=NULL)
      { ptrl=s;
        s=s->next;
      if(s->data==f)
      {
          ptrl->next=s->next;i=1;
       }
       }
       }
       p=head;
       if(i==1){
            printf("linked list after deletion is: ");
        while(p!=NULL){
     printf("%d ",p->data);
     p=p->next;
        }
        }
        else{
     printf("\ndata not found");
        }
      getch();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-15-2013, 01:16 PM
  2. Mass deletion from linked list
    By budala in forum C Programming
    Replies: 3
    Last Post: 04-27-2010, 05:43 AM
  3. Deletion in linked list
    By jeya in forum C Programming
    Replies: 1
    Last Post: 07-24-2008, 12:36 AM
  4. Linked List Deletion
    By MasterAchilles in forum C Programming
    Replies: 1
    Last Post: 04-20-2008, 05:27 AM
  5. linked list inserting in a particular spot
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 10-28-2005, 03:49 AM