#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Delete function not working


    Hi all,
    I'm a newbie in C. I was writing a sample code to delete nodes from a linked list. But the compiler Dev C++ gives an error: expected , or ; before delete in its prototype declaration. I dont know whats wrong. Please Help.

    Also clrscr is not working even when i'cve included conio.h. Cant understand why. Explain that as well.
    Here's the code....


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

    struct node
    {
    int data;
    struct node *link;
    };


    void append(struct node **, int);
    void addatbeg(struct node **, int);
    void display(struct node *);
    int count(struct node *);
    void delete(struct node **, int);

    int main()
    {

    struct node *p;
    p=NULL;
    /* clrscr(); */

    append(&p, 11);
    append(&p, 71);
    append(&p, 23);
    append(&p, 754);
    append(&p, 7);

    count(p);
    display(p);

    addatbeg(&p,10);
    addatbeg(&p,20);
    addatbeg(&p,30);
    addatbeg(&p,40);
    addatbeg(&p,50);
    addatbeg(&p,60);

    count(p);
    display(p);

    delete(&p, 30);
    delete(&p, 7);
    delete(&p, 91);

    getch();
    return 0;
    }


    void addatbeg(struct node **q, int num)
    {
    struct node *r;
    r= (struct node*)malloc(sizeof(struct node));
    r->data = num;
    r->link = *q;

    *q=r;
    }

    void append(struct node **q, int num)
    {
    struct node *temp, *r;
    temp = *q;

    r=(struct node*)malloc(sizeof(struct node));
    r->data = num;

    if (temp==NULL)
    {
    r->link = NULL;
    *q = r;
    }
    else
    {
    while(temp->link!=NULL)
    temp= temp->link;

    r->link = NULL;
    temp->link = r;
    }
    }

    void display(struct node *q)
    {
    printf("\nLinked list is:");
    while(q!=NULL)
    {
    printf("%d\t", q->data);
    q=q->link;
    }
    }

    int count(struct node *q) /*it shud be void becaause i'm not returning anything. Printing count here only*/
    {
    int count = 0;
    while(q!=NULL)
    {
    count++;
    q=q->link;
    }
    printf("\n\nTotal number of elements in the linked list are : %d", count);
    }


    void delete(struct node **q, int num)
    {
    struct node *old, *temp;

    temp = *q;
    old = *q;
    if (temp == NULL)
    {
    printf("\nNo elements exist in the linked list");
    return ;
    }
    while(temp != NULL)
    {
    if (temp->data == num)
    {
    if (temp->link == NULL)
    old = NULL;
    else
    old->link = temp->link;
    printf("Element %d deleted." , num);
    free(temp) ;
    return ;

    else
    {
    old = temp;
    temp = temp->link;
    }
    }
    printf("\n\n%d does not exist in the linked list", num);
    }
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481
    You've posted an incomplete program. After discarding conio.h and the getch, removing the incomplete delete function, and changing count to void, gcc makes an object file without complaint.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Delete is incomplete..?


    i know count should be void..and just removing delete makes the program work..

    But i dont understand how delete is incomplete..?

    And even if it is incomplete, the error says missing , or ; before delete in its declaration above int main. I dont understand how that error is coming..?

    Also help me complete the delete function.
    Thanks in advance..:)
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0
    This is a complete delete function that i have referred from a book. Although i dont think my previous delete function was incomplete, but anyways it shows the same error.
    I'll copy the error message from the compiler. It says

    16 C:\Users\akejriwal\Desktop\linked list.cpp expected `,' or `;' before "delete"

    Line 16is delete functions's declaration.

    void delete(struct node **q, int num)
    {
    struct node *old, *temp;

    temp = *q;
    /*old = *q;
    if (temp == NULL)
    {
    printf("\nNo elements exist in the linked list");
    return ;
    }*/
    while(temp != NULL)
    {
    if (temp->data == num)
    {
    if (temp == *q)
    *q=temp->link;
    else
    old->link = temp->link;
    printf("Element %d deleted." , num);
    free ( temp ) ;
    return;

    else
    {
    old = temp;
    temp = temp->link;
    }
    }
    printf("\n\n%d does not exist in the linked list", num);
    }




    ALSO HELP ME WITH THE CLRSCR FUNCTION.
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481
    Dude or dudette, in your first post the left curly braces outnumber the right curly braces. I stick that section of your code into the file /tmp/a
    Code:
       A =: 1!:1 <'/tmp/a'
    
       +/ '{' = A
    5
       +/ '}' = A
    4


    [edit]and the second posting of the same thing surprisingly suffers likewise[/edit]
    Last edited by b49P23TIvg; August 31st, 2012 at 08:47 AM. Reason: spacing inserted for clarity.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0
    Thanks for pointing that out.
    I've corrected it. But still the error remains the same :|
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481
    Code:
    #include<stdio.h>
    #include<malloc.h>
    
    struct node {
      int data;
      struct node *link;
    };
    
    void append(struct node **, int);
    void addatbeg(struct node **, int);
    void display(struct node *);
    int count(struct node *);
    void delete(struct node **, int);
    So you say there's an error in this much code? Without conio.h it compiles on my system. to a .o
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    Originally Posted by Apoorv1234
    But the compiler Dev C++ gives an error: expected , or ; before delete in its prototype declaration. I dont know whats wrong. Please Help.
    Such error messages mean that the parser did not expect to find the delete prototype there, or rather in this case it did not expect that semicolon at the end of the preceding prototype (which it apparently did not recognize as such) or a comma contained within. This could be caused by any of several different things (eg, mismatched parentheses or quotation marks, mismatched braces, mismatched conditional compilation control statements), all of which boil down to a poorly formed statement (ie, wrong syntax).

    However, when I compile your code as-is with my MinGW gcc compiler (which I think you said elsewhere is what you're using, though you may have called it "g++"), I don't get an errors around the prototypes, but rather only later:
    C:TEST>gcc -Wall unexpected.c
    unexpected.c: In function `count':
    unexpected.c:105: warning: control reaches end of non-void function
    unexpected.c: In function `delete':
    unexpected.c:131: parse error before `else'

    C:TEST>
    It's complaining that count is not returning any value even though it's non-void, and about the missing close brace in delete.

    Now, mind you, the way that code is written is pure C, not C++. When I copy the code to a .cpp file and try to compile it with g++, then I also get that error:
    unexpected.cpp:16: parse error before `delete'
    And then a whole slew of errors and warnings after that.

    A man goes the see his doctor and tells him, "Doc! When I hold my arm up like this it hurts!" Doc says, "Well then, don't hold your arm up like that."

    When you try to compile a purely C program with the C++ compiler, it spews out a ton of weird errors, whereas when you compile it with the C compiler it only complains about two errors that are real, understandable, and fixable. Well then, compile it as a C program and not as C++. Doesn't solve the mystery, but it does at least solve the problem.


    Originally Posted by Apoorv1234
    Also clrscr is not working even when i'cve included conio.h. Cant understand why. Explain that as well.
    Again assuming that you're using MinGW gcc. All that any header file does is to tell the compiler that there are functions elsewhere with these prototypes (also header files will contain macros, type/class declarations and definitions, and extern variables), but they don't provide the object code for those functions. That object code exists within library and object files.

    Of course, if the functions are defined in another of the project's source files, then that's taken care of. But if the object code exist in a library or object file, then you have to tell the linker to link those files in.

    MinGW gcc comes with both a conio.h header file and a conio.o object file. If you want to use conio functions, then you need to include conio.o in the project; eg:
    gcc -Wall unexpected.c conio.o -o unexpected


    Also, please post your code using code tags. Failure to do so results in unreadable code listings that most of us just will not waste our time with. Plus, when the code is formatted, then you can immediately see where the missing braces are. As your post count grows and you continue to ignore this most basic of common courtesies here, you will be reminded of it with increasing frequency and emphasis.

    Type [code][/code]. Those are the open and close code tags. Now take your formatted code and copy and paste it to between those two tags. Simple as that. For example, your own code would appear thus:
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<malloc.h>
    
    struct node 
    {
           int data;
           struct node *link;
    };
    
    
    void append(struct node **q, int num);
    void addatbeg(struct node **q, int num);
    void display(struct node *q);
    int count(struct node *q);
    void delete(struct node **q, int num);
    
    int main()
    {
        
        struct node *p;
        p=NULL;
    /*    clrscr(); */
        
        append(&p, 11);
        append(&p, 71);
        append(&p, 23);
        append(&p, 754);
        append(&p, 7);
        
        count(p);
        display(p);
        
        addatbeg(&p,10);
        addatbeg(&p,20);
        addatbeg(&p,30);  
        addatbeg(&p,40);
        addatbeg(&p,50);
        addatbeg(&p,60);
                          
        count(p);
        display(p);
    
        delete(&p, 30);
        delete(&p, 7);
        delete(&p, 91);
    
        getch();
        return 0;
    }    
    
    
    void addatbeg(struct node **q, int num)
    {
        struct node *r;
        r= (struct node*)malloc(sizeof(struct node));
        r->data = num;
        r->link = *q;
        
        *q=r;
    }
      
    void append(struct node **q, int num)
    {
         struct node *temp, *r;
         temp = *q;
         
         r=(struct node*)malloc(sizeof(struct node));
         r->data = num;
         
         if (temp==NULL)
         {
         r->link = NULL;
         *q = r;
         }
         else
         {
             while(temp->link!=NULL)
             temp= temp->link;
             
             r->link = NULL;
             temp->link = r;
         }
    }
    
    void display(struct node *q)
    {
         printf("\nLinked list is:");
         while(q!=NULL)
         {
                       printf("%d\t", q->data);
                       q=q->link;
         }
    }
                            
    int count(struct node *q) /*it shud be void becaause i'm not returning anything. Printing count here only*/
    {
        int count = 0;
        while(q!=NULL)
        {
                      count++;
                      q=q->link;
        }
        printf("\n\nTotal number of elements in the linked list are : %d", count);
    }
    
    
    void delete(struct node **q, int num)
    {
         struct node *old, *temp;
         
         temp = *q;
         old = *q;
         if (temp == NULL)
         {
                  printf("\nNo elements exist in the linked list");
                  return ;
         }
         while(temp != NULL)
         {
             if (temp->data == num)
             {
                            if (temp->link == NULL)
                            old = NULL;
                            else
                            old->link = temp->link;
                            printf("Element %d deleted." , num);
                            free(temp) ;
                            return ;
                            
             else
             {
                 old = temp;
                 temp = temp->link;
             }
         }
         printf("\n\n%d does not exist in the linked list", num);
    }
    Now, right away you can see that missing close brace in delete(). You can also see the missing indentation in the if-else construct immediately preceding that missing brace. And even if you could not see that missing brace, we would be able to spot it immediately.

    Code tags are very important. Use them!
  16. #9
  17. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481
    oh! That's right, it was compiled with a c++ compiler.

    Doesn't "delete" have some sort of reserved meaning in c++?

    As in, it's a reserved word.
    Last edited by b49P23TIvg; August 31st, 2012 at 02:29 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  18. #10
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    Yes, it just hit me too. delete is a reserved word, so you cannot use it for any other purpose in C++.

    I just changed delete() to Delete(), made those other two corrections (the missing return in count and the missing brace in Delete), and added conio.o to the project and it builds just fine and generates this output:
    Total number of elements in the linked list are : 5
    Linked list is:11 71 23 754 7

    Total number of elements in the linked list are : 11
    Linked list is:60 50 40 30 20 10 11 71
    23 754 7 Element 30 deleted.Element 7 deleted.

    91 does not exist in the linked list
    Dave, sometimes I think our minds are too highly trained. And if you got that reference, then I know they are.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Problem Solved


    The problem has been solved.
    Turns out that when I changed delete to del or any other name, it works perfectly fine. Thanks a lot.

    Can you guys also help me with another problem..? Would really help me clearing my concepts. Thanks a lot. Here's the link to that thread.

    http://forums.devshed.com/c-programming-42/no-output-showing-in-dev-c-compiler-help-please-929885.html

IMN logo majestic logo threadwatch logo seochat tools logo