ok, so I have to do a linked list having the user enter 5 numbers and then asking them to add in one more value. I have the linked list, but cannot figure out how get it ask for one more value. Any ideas?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXNUMS 6
#define DEBUG 0
/*Here is the declaration of a stack structure*/
struct numRec
{
char value[MAXNUMS];
struct numRec *nextAddr;
};
/*here is the definition of the top of stack pointer*/
struct numRec *firstRec;
int main()
{
void readInsert(); /*function prototype*/
void display();
firstRec = NULL; /*initialize the top of stack pointer*/
readInsert();
display();
return 0;
}
/*get a number and insert it into the stacked list */
void readInsert()
{
char value[MAXNUMS];
void insert(char *);
printf("Enter 5 numbers, one per line");
printf("\nTo stop entering numbers, enter a single x\n");
while (1)
{
printf("Enter a number: ");
gets(value);
if (strcmp(value,"x") == 0)
break;
insert(value);
}
}
void insert(char *value)
{
struct numRec *linearlocate(char *);
struct numRec *newaddr, *here; /* pointer to structure of type numRec*/
newaddr = (struct numRec *) malloc(sizeof(struct numRec));
if (newaddr == (struct numRec *) NULL)
{
printf("\nFailed to allocate memory for this structure\n");
exit(1);
}
if (firstRec == NULL)
{
newaddr->nextAddr = NULL;
firstRec = newaddr;
}
else if (strcmp(value, firstRec->value)<0)
{
newaddr->nextAddr = firstRec;
firstRec = newaddr;
}
else
{
here = linearlocate (value);
newaddr->nextAddr = here->nextAddr;
here->nextAddr = newaddr;
}
strcpy(newaddr->value,value); /*store the number*/
}
struct numRec *linearlocate (char *value)
{
struct numRec *one, *two;
one = firstRec;
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 numRec *contents;
contents = firstRec;
printf("\nThe numbers currently in the list are:");
while(contents != NULL)
{
printf("%s\n", contents->value);
contents = contents->nextAddr;
}
fflush(stdin); /* clear input area so you can pause */
getchar(); /* force the computer to pause until you press a key on the keyboard */
}
Thank you!