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?
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
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?
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;:shout::worried::cryin: PLease write out what's you are doing! code it for me please! John
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); }
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:pleased::speechles:beatnik::euro::euro::juggle::hang:
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