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?
|
Ambitious contributor
|
|
| 13Dec2010,09:49 | #2 |
|
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 |
|
Light Poster
|
|
| 14Dec2010,11:00 | #3 |
|
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? |
|
Light Poster
|
|
| 20Dec2010,21:05 | #4 |
|
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 |
|
Pro contributor
|
![]() |
| 20Dec2010,23:30 | #5 |
|
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);
}
|
|
Light Poster
|
|
| 21Dec2010,06:55 | #6 |
|
Is the head always the smallest? is the tail always the largest?
|
|
Pro contributor
|
![]() |
| 21Dec2010,12:31 | #7 |
|
Light Poster
|
|
| 21Dec2010,23:40 | #8 |
|
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 (____________________________)
}
}
}
min->fname? John ![]() ![]() ![]() ![]() :juggl e:
|
|
Light Poster
|
|
| 31Mar2011,06:58 | #9 |
|
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 |









