Thread: linked list problem

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    151

    linked list problem

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    typedef struct linkledlist {
     char data;
    struct linkedlist *nextptr;
    } DEFINE;
    
    
    
    void insert(DEFINE  **,char);
    int main(int argc, char *argv[])
    {
    DEFINE  *startptr;
    startptr=NULL;
    startptr->nextptr=NULL;
    printf("Choose what to do  (1) to add , (2) to delete , q to quit....");
    char selection;
    char toadd;
    scanf("%c",&selection);
    switch(selection) {
    case 1:
      {
     printf("Enter character to add :");
    scanf("%c",&toadd);
    printf("x");
    insert(&startptr,toadd);
    } }
    
    printf("%c",startptr->data);
    return 0;
    printf("x");
     
     
    }
    
    
    void insert(DEFINE  **sptr,char value) {
    DEFINE *newptr=malloc(sizeof(DEFINE));
    DEFINE *previous;
    DEFINE *current;
    if(newptr!=NULL) {newptr->data=value;
    newptr->nextptr=NULL; }
    previous=NULL;
    current=*sptr;
    
    while(current!=NULL && value>current->data) { 
    previous=current;
    current=current->nextptr;} ////////////////
    
    if(previous==NULL) {
    newptr->nextptr=*sptr;  ////////////////////////////
    *sptr=newptr; }
    else {
    newptr->nextptr=current;       /////////////////////////
    previous->nextptr=newptr; }//////////////
    }
    It will be a program which is capable of adding chars and deleting chars from a linkedlist. But I get this error from compile :

    55 C:\Dev-Cpp\projects\main.c [Warning] assignment from incompatible pointer type

    It gives this error , for all the lines I have mentioned by slashes. 'd be glad if you helped.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You've been posting here long enough to have some understanding that if you don't indent your code, you probably don't get much help.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Yes you are true.I have thought of it. But then I thought that the problem was not about the whole code. So I did not. But if you say so , I will do it then I will expect some answers. Thank you.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    By the way, by intending you mean , explainin aims of lines?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No I mean Indenting:
    Un-indented code:
    Code:
    int main() {
    for(i=0; i < 10; i++) {
    printf("&#37;d", i);
    for(j=0; j < 10; j++) {
    printf("%d", j);
    }
    }
    }
    Same code indented:
    Code:
    int main() {
      for(i=0; i < 10; i++) {
        printf("%d", i);
        for(j=0; j < 10; j++) {
          printf("%d", j);
        }
      }
    }
    It shows the relationship between the control structures and the code within them.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Some comments on the code:
    1.
    Code:
    DEFINE  *startptr;
    startptr=NULL;
    startptr->nextptr=NULL;
    this will crash the application - writing to NULL is a definite bad thing.

    2. If you are using C, rather than C++, declare all variables first, then put code after the declarations.
    Code:
    printf("Choose what to do  (1) to add , (2) to delete , q to quit....");
    char selection;
    char toadd;
    3. Don't put code that you expect to execute AFTER a return.
    Code:
     return 0;
    printf("x");
    4. Your actual compile problem is caused by a typo:
    Code:
    struct linkledlist { 
    ...
    struct linkedlist *nextptr;
    }
    - there is an extra letter l in the first struct name, that isn't in the second struct name. The compiler will think that the second struct is a different type, and of course give the warning you are describing.

    Edit: And don't use a name like "DEFINE" as a typename...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    I have already changed that printf after return. That is a fatal mistake. But that was about a moment anyway. And about the main problem . that is disgusting that I have made a mistake which is completely about syntax error. How could not I see that extra `l` there. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM