Minimum element value of a linked list

This is a discussion on Minimum element value of a linked list within the C Programming forums, part of the General Programming Boards category; I am working through C in linked lists. I have been stumped on these two things. I have a structure ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    17

    Question Minimum element value of a linked list

    I am working through C in linked lists. I have been stumped on these two things. I have a structure that resembles :



    Code:
    typedef struct name
                {
                    char *first;
                    char *last;
                    struct name *next;
                 } person;
    
    with the structure variable : people;
    
    person people *head,*tail * current;

    now I need a small function that will find the smallest value of the list with the data:

    current->first=John;
    current->last=Marshall
    current->first=Sam;
    current->last=sneed;
    current->first=Ann;
    current->last=abercrombie;
    and return a chart in the function minimum.

    Code:
    char minimum ( struct name min);
    now how do I use a for construct to start at the second vale and iterate by 1 throughout the list?

    I have a couple of sorts I am working on!

    John

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Psuedo code.
    The code will be just like finding smallest/largest element in array..
    Code:
    smallest = list;  
    while( list != NULL) {
      if( list->data < smallest->data ) {
           smallest = list;
      }
      list = list->next
    }

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    17
    Bayint Naung:

    What would I return?

    John

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Whatever you want to return. Return the value the node holds if all you want is the value. Return a pointer to the node if you want that.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    17
    If i wanted a character string to come back to set equal to min then?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What is 'min' as compared to these names? Are you trying to return one name, or two? Or the first and last combine in a string? The term 'min' doesn't really fit when you're comparing strings.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    17
    What is 'min' as compared to these names? Are you trying to return one name, or two?
    I am trying to return 2 strings (first and last of the smallest). With a return statement at the end


    Or the first and last combine in a string?
    min->first;
    min->last;

    which yields the the minimum value when you read through the list.

    The term 'min' doesn't really fit when you're comparing strings.

    strcmp( min->first, curr->first)?

    John

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You already have the name stored in a node. Why don't you just return a pointer to it? Otherwise, no, you can't return two individual things. You can copy them to a buffer or some dynamically allocated space, and return the name as a whole, or you can return either name, but without just returning the structure you already have them stored in (or a pointer to it), you cannot return more than one item from a function.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    17

    Question code time.

    could show me the full code?

    using this:

    Code:
    people * minimum (struct name *people)
    {
       people person *min, *current;
      min=people;
    while( list != NULL) {
      if( people->fname < min->fname) {
          min =current;
      }
      list = list->next
    }
    
    return current;
    Is the above correct? If not what changes have to be made?

  10. #10
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    458
    Use strcmp instead of logical <. Your function should return person* instead of people*. You have never typedef the type people. People is your veriable name, not a type.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    type *minimal( type *list )
    {
        type *min
    
        loop through list
            compare to find minimum
                min = new minimum just found in comparison
    
        return min
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    17
    Code:
     person * minimum (struct people *list)
    {
        person *min;
        while (list !=NULL)
        {
          if (strcmp(min->fname, list->fname)<0)
            {
                return min;
             }
          }
          what  do I return here?
    
    }
    Is this right?

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you are trying to get through the whole list, then no, it's not right. Write a function that takes an array, and returns the smallest value. Once you know how to do that, convert it to take a linked list.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Oct 2010
    Posts
    17
    Here's what I have for an array:
    Code:
    int main ()
    {
     
    int icounter=0;
    int array[10]={12,34,2,3,90,34,56,100,6}
    int min=a[0];
    
    for(i=0;i<=10;i++)
    {
      if (array[i]<min)
       {
         min=a[i];
        
      }
     return min;
    }
    ok, now this is what I have for linked lists

    Code:
    int main()
    {
    person *min, *curr;
    
    //question : how do I convert a[0]?
    while (curr !=NULL)
    {
       if(strcmp(min->fname,curr->fname)<0)
         {
            min->fname=curr->fname;
          }
     }
    return min->fname;
    }
    Right?

    John

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do you know how to walk through a linked list?

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 08:53 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 09:55 PM
  3. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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