Minimum value in Linked List

RBCC's Avatar, Join Date: Dec 2010
Light Poster
How do I read through a Linked List and return the minimum value from a linked list. So it returns the element(s) that are the smallest?

Here is the struct

struct names
{
char first[30];
char last[30];
struct names *next;
};

struct names
{
char *first;
char *last;
struct names *next;
};

struct names names_var;

also how do I access the members of each structure?
0
jimblumberg's Avatar
Ambitious contributor
First you can't have structures with the same name.

To search a linked list you must iterate through the list and compare the node's value with the desired value.

Jim
0
RBCC's Avatar, Join Date: Dec 2010
Light Poster
The two structures are basically the same. But one has arrays and the other has pointers.
Now I was trying to is how do you access the members of each if you have a pointer for the structure var and var for the structure var.
ie:struct names names_var;
struct names *names_var;

could some please write out code for the minimum value for a Linked list in the form of a function, with a return value and passing a structure?
0
RBCC's Avatar, Join Date: Dec 2010
Light Poster
What I am trying to do in to learn how to find the minimum value of a linked list and return the value of the member that is the smallest.

using typedef struct names *pnames_var;

pnames_var member;

return member->fname;

PLease write out what's you are doing! code it for me please!

John
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
one way to code this is

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct names{
    char first[30];
    char last[30];
    struct names *next;
};
   
typedef struct names * pnames_var;
void insert( pnames_var *, char[] ,char[]);
void show_smallest(pnames_var);
void printList( pnames_var );
    int main(){
        pnames_var HEAD=NULL;//original list
        insert(&HEAD,"x2","y2");
        insert(&HEAD,"x1","y1");
        printList(HEAD);
        show_smallest(HEAD);
        getchar();
        return 0;
    }
    
    void insert( pnames_var *Head, char first[],char last[]){
        pnames_var current=*Head,previous=NULL,new1=NULL;
        new1 = (pnames_var) malloc( sizeof( struct names ) );
        strcpy(new1->first,first);
        strcpy(new1->last,last);
        new1->next=NULL;
        if (current==NULL){
            *Head=new1;
        }else{
            while(current!=NULL){
                previous=current;
                current=current->next;    
            }
            if (current==NULL) 
                previous->next=new1;
        }
    }
    
    void printList(pnames_var Head){
        printf("\n the linked list is:\n");
        pnames_var x=Head;
        while(x!=NULL){
            printf("%s , %s -->",x->first,x->last);
            x=x->next;
        }
        printf("NULL\n");
    }
    void show_smallest(pnames_var Head){//checks with name only
        char smallestName[30];
        char smallestLast[30];
        pnames_var x=Head;
        if (x==NULL){
            printf("\n the linked list is empty!");
            return;
        }else{
            strcpy(smallestName,x->first);
            strcpy(smallestLast,x->last);
        }
        while(x!=NULL){
            if (strcmp(x->first,smallestName)<0){
                strcpy(smallestName,x->first);
                strcpy(smallestLast,x->last);
            }
            x=x->next;
        }
        printf("\n the smallest value found is Name=%s , Surname=%s",smallestName,smallestLast);
    }
0
RBCC's Avatar, Join Date: Dec 2010
Light Poster
Is the head always the smallest? is the tail always the largest?
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
Quote:
Originally Posted by rbcc View Post
is the head always the smallest? Is the tail always the largest?

Νο
0
RBCC's Avatar, Join Date: Dec 2010
Light Poster
Can you show code that shows this? Con you do a conversion between arrays and linked list ie:
Code:
i=i+1 -> in linked list
(return a string or structure)  selection( pass a structire)
{
  int i,j,min,t ---> in linked list it is (____________)
 for (i=1;i<N;i++)------------->    in linked list it is(__________________________)
{
  min=j; //-------->(is there a routine for this? if so, what is it(__________________)
  for (j=i+1;j<=N;j++) --------> in linked lists (______________________)
 {
   if (a[j]<a[min])
   {
      min=j; ----------> in linked list (Routine??)(_____________________)
      t=a[min]; ----------> in linked list (__________________________)
      a[min]=a[i];--------->in linked list (___________________________)
      a[i]=t; --------------->in linked list (____________________________)
    }
}
}
can min=j; be a routine to find the minimum and return the minimum in the form of
min->fname?

John:juggl e:

Last edited by shabbir; 22Dec2010 at 08:30.. Reason: Code blocks
0
RBCC's Avatar, Join Date: Dec 2010
Light Poster
I learn by doing rather than the frustration of doing something wrong and quitting.
Can someone please cods that shows that minimum value from a linked list where the minimum is not the head value?

John