Here is the question posed.
Create a structure that has one variable called value and one pointer to the list (making it a linked list). Prompt for 5 values from the keyboard as input and store them in the linked list. Print out the current contents of the list. Allow the user to add one more value to the linked list, and print the contents of the list again.
Here is my code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 30
struct ValueList
{
char valuename[MAX];
struct ValueList *value;
};
struct ValueList *firstvalue;
int main()
{
void readValue();
void display();
firstvalue=NULL;
readValue();
display();
return 0;
}
void readValue()
{
char value[MAX];
void insert(char *);
printf("Please enter 5 whole numbers, one per line");
printf("\nTo stop entering whole numbers, enter a single x\n");
while(1)
{
printf("Enter a whole number: ");
gets(value);
if (strcmp(value, "x") ==0)
break;
insert(value);
}
}
void insert(char *value)
{
struct ValueList *linear Locate(*char);
struct Valuelist *newnode, *here;
newnode= (struct ValueList *) malloc(sizeof(struct ValueList));
if (newnode == (struct ValueList *) NULL)
{
printf("\n\nCould not allocate enough space\n");
exit(1);
}
if(firstValue==NULL)
{
newnode->nextAddr=NULL;
firstValue=newaddr;
}
else if(strcmp(value, firstValue->value) <0)
{
newaddr->nextAddr = firstValue;
firstValue=newaddr;
}
else
{
here = linear Locate(value);
newaddr->nextAddr = here->nextAddr;
here->nextAddr = newaddr;
}
strcpy(newaddr->value,value);
}
struct ValueList *linear Locate(char *value)
{
struct ValueList *one, *two;
one = firstValue;
two = one->nextAddr;
if(two==NULL)
return (one);
while(1)
{
if(strcmp(value, two->value) <0)
break;
else if(two->nextAddr == NULL)
{
one = two;
break;
}
else
{
one = two;
two = one->nextAddr;
}
}
return(one);
}
void display()
{
struct ValueList *contents;
contents = firstValue;
printf("\nThe numbers currently in the list are\n");
while(contents != NULL)
{
printf("%s\n",contents->value);
contents = contents->nextAddr;
}
}
Now I know I am supposed to add more values however the example from my textbook is garbage and I am suffering from a dislocated shoulder so I am really not liking coding with two semi functioning arms and hands.
Any help would be appreciated and I thank all of you for the help in advance.
Todd