Like Tree1Likes
  • 1 Post By AndiPersti

Sorted Linked list Help please

This is a discussion on Sorted Linked list Help please within the C Programming forums, part of the General Programming Boards category; I been ask to create a sorted linked list module, and to test each function with 1 unit, 2 +. ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    19

    Sorted Linked list Help please

    I been ask to create a sorted linked list module, and to test each function with 1 unit, 2 +.

    so i wrote my addtofront function( we have to use the same as the teacher)
    Code:
    struct MyList{
    
        char * name;
    
         Sorted * AddmyChar;
        struct MyList * next;
    
    
    };
    
    typedef struct MyList Sorted;
    typedef Sorted * Thelist;
    
    /*the function is this*/
    
    Thelist addfront(Thelist list, Sorted * AddmyChar)
    {
     Thelist head;
     Struct Sorted * temp;
     
      head = AddmyChar;
     if(list == NULL){
     list = (struct Sorted *)malloc(sizeof(Sorted));
     if (list == NULL){
       printf("Error! Out of Memory\n");
       exit(0);
     }
     list->head= head;
     theList-> next = list;
     } else {
        temp = list;
        
        while (temp->next != list)
             temp = temp->next;
         temp-> next = (Sorted *)malloc(sizeof(Sorted)); 
        if(temp -> next == NULL)
        {
        printf("Error! Out of Memory\n");
        exit(0);
        }
        temp = temp->next;
        temp->head = head;
        temp->next = list;
        }
        return (list);
    }
    }
    but i cant seem to test it because it always display a seg fault....
    i dont know what i did wrong....how do i call this function in main to display a string i want?

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Looks like your function doesn't return anything ( returns rubbish ) if the list is empty.
    Can't tell for shure, the indentation is so bad.
    Kurt

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    19
    i have another function that checks if the list is empty or not..i just wanna know,
    if i put a string in main and call it in this function
    Code:
    int main()
    {
     char * string;
     addfront(string,"name");
    
    }
    how do i add it to this? cause addmyChar is not type char *, it is type Sorted; a type that was created.

    i know that the indent is bad..sorry

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    First of all you have to sort out the types that you use.
    The definition of struct MyList doesn't make sense. Its using a typedef that is defined afterwards.
    You shouldn't typedef pointer types. That just adds confusion.
    Also use descriptive names.
    Kurt

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    19
    thats the way the teacher use it...we have to use her functions prototypes, we cant change it...thats the only reason i'm confuse..cause i able to do it with char and int but cant do it with that

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    struct MyList{
    
        char * name;
    
         Sorted * AddmyChar;
        struct MyList * next;
    
    
    };
    
    typedef struct MyList Sorted;
    typedef Sorted * Thelist;
    Is this part really given to you by your teacher?

    Bye, Andreas
    ZuK likes this.

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    19
    Believe it or not, it actually was given by my teacher;
    its says

    typedef struct WHATEVERYOURSTRUCTNAMEIS Sorted
    typedef Sorted * Thelist;

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    What's about the struct itself ("MyList" in your code)?

    And what's about the function header/interface?
    Code:
    Thelist addfront(Thelist list, Sorted * AddmyChar)
    Bye, Andreas

  9. #9
    Registered User
    Join Date
    Mar 2013
    Posts
    19
    that one was also given...but thanks anyways...my teacher just send me an example on how to do it...

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    Quote Originally Posted by Tryhard View Post
    i have another function that checks if the list is empty or not..i just wanna know,
    if i put a string in main and call it in this function
    Code:
    int main()
    {
     char * string;
     addfront(string,"name");
    
    }
    how do i add it to this? cause addmyChar is not type char *, it is type Sorted; a type that was created.

    i know that the indent is bad..sorry
    You have to follow a function's parameter list to get a compile. Look at the signature of function addfront:
    Code:
    Thelist addfront(Thelist list, Sorted * AddmyChar)
    You are expected to pass TheList, and Sorted * type variable in that order. So the string variable is wrong because it isn't the right type for either argument.

    Your code needs to decide which parameter is the list you want to add to and which parameter represents the node that you should add. When you make your decision, leave a comment, because it is not clear which parameter is which and if I am reading this right both type names are functionally the same type which is weird. Then you have to rewrite your function to deal with the following cases:

    1. When the list you want to add to has no items.
    2. When the item you want to add should be placed in the front of the list.
    3. When the item you want to add should be placed in the middle of the list.
    4. When the item you want to add should be placed in the back of the list.

    It is possible that the 3rd and 4th case can be addressed by the same code, but that depends.

    If the node you want to add will always look like:

    name: <any name>
    next: NULL

    Then it should be possible.

    Before the function can be considered complete something needs to be done with the pointer addmyChar inside of the type:

    Code:
    struct MyList{
     
        char * name;
     
         Sorted * AddmyChar;
        struct MyList * next;
    };
    It is not clear to me what that pointer is for. And since both of the types Sorted * and Thelist have this pointer something must be done.

    Basically I'm trying to rephrase what other people have told you...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting to sorted linked list
    By sand_man in forum C Programming
    Replies: 12
    Last Post: 05-05-2012, 06:47 PM
  2. sorted linked list
    By budala in forum C Programming
    Replies: 12
    Last Post: 07-29-2009, 08:47 AM
  3. sorted linked list...why seg faults!
    By S15_88 in forum C Programming
    Replies: 4
    Last Post: 03-24-2008, 11:30 AM
  4. Sorted Linked List Problem
    By s0ul2squeeze in forum C++ Programming
    Replies: 2
    Last Post: 03-26-2002, 09:01 PM
  5. sorted linked list
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 02-03-2002, 08:11 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21