Okay, so I have a menu and a linked list, but I have three problems. First of, if you select option one and enter a name it keeps prompting you to enter another and another. It won't accept any other prompt to do anything else like, for instance, exit the program.
Next, I want to be able to insert a name into the list or modify a name. I just can't figure out how to do those two. Any help would be greatly appreciated. The current code that I have generates the following errors:
error C2062: type 'void' unexpected
error C3861: 'modify': identifier not found
If I remove void insertend(void), void modify(void); those errors disappear, but I included them in anticipation of getting the insert and modify features working.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct names {
char name[30];
struct names *next; /* pointer to next entry */
struct names *prior; /* pointer to previous record */
};
struct names *start; /* pointer to first entry in list */
struct names *last; /* pointer to last entry */
struct names *find(char *);
void enter(void);
void load(void), list(void);
void insertend(void), void modify(void);
void dls_store(struct names *i, struct names **start,
struct names **last);
void inputs(char *, char *, int), display(struct names *);
int menu_select(void);
int main(void)
{
start = last = NULL; /* initialize start and end pointers */
for(;;) {
switch(menu_select()) {
case 1: enter(); /* enter a name */
break;
case 2: insertend(); /* add a name to the list*/
break;
case 3: modify(); /* modify a name on the list*/
break;
case 5: list(); /* display the list */
break;
case 6: load(); /* read from disk */
break;
case 7: exit(0);
}
}
return 0;
}
/* Select an operation. */
int menu_select(void)
{
char s[80];
int c;
printf("1. Create a list of names\n");
printf("2. Insert a name into the list.\n");
printf("3. Modify a name on list.\n");
printf("4. Delete a name from list.\n");
printf("5. Display the list\n");
printf("6. Load the file\n");
printf("6. Quit\n");
do {
printf("\nEnter your choice: ");
gets(s);
c = atoi(s);
} while(c<0 || c>7);
return c;
}
/* Enter names. */
void enter(void)
{
struct names *info;
for(;;) {
info = (struct names *)malloc(sizeof(struct names));
if(!info) {
printf("\nout of memory");
return;
}
inputs("Enter name: ", info->name, 30);
if(!info->name[0]) break; /* stop entering */
dls_store(info, &start, &last);
} /* entry loop */
}
/* This function will input a string up to
the length in count and will prevent
the string from being overrun. It will also
display a prompting message. */
void inputs(char *prompt, char *s, int count)
{
char p[255];
do {
printf(prompt);
fgets(p, 254, stdin);
if(strlen(p) > count) printf("\nToo Long\n");
} while(strlen(p) > count);
p[strlen(p)-1] = 0; /* remove newline character */
strcpy(s, p);
}
/* Create a doubly linked list in sorted order. */
void dls_store(
struct names *i, /* new element */
struct names **start, /* first element in list */
struct names **last /* last element in list */
)
{
struct names *old, *p;
if(*last==NULL) { /* first element in list */
i->next = NULL;
i->prior = NULL;
*last = i;
*start = i;
return;
}
p = *start; /* start at top of list */
old = NULL;
while(p) {
if(strcmp(p->name, i->name)<0){
old = p;
p = p->next;
}
else {
if(p->prior) {
p->prior->next = i;
i->next = p;
i->prior = p->prior;
p->prior = i;
return;
}
i->next = p; /* new first element */
i->prior = NULL;
p->prior = i;
*start = i;
return;
}
}
old->next = i; /* put on end */
i->next = NULL;
i->prior = old;
*last = i;
}
/* Remove an element from the list. */
void mldelete(struct names **start, struct names **last)
{
struct names *info;
char s[80];
inputs("Enter name: ", s, 30);
info = find(s);
if(info) {
if(*start==info) {
*start=info->next;
if(*start) (*start)->prior = NULL;
else *last = NULL;
}
else {
info->prior->next = info->next;
if(info!=*last)
info->next->prior = info->prior;
else
*last = info->prior;
}
free(info); /* return memory to system */
}
}
/* Find a name. */
struct names *find( char *name)
{
struct names *info;
info = start;
while(info) {
if(!strcmp(name, info->name)) return info;
info = info->next; /* get next name */
}
printf("Name not found.\n");
return NULL; /* not found */
}
/* Display the entire list. */
void list(void)
{
struct names *info;
info = start;
while(info) {
display(info);
info = info->next; /* get next name*/
}
printf("\n\n");
}
/* This function prints the fields. */
void display(struct names *info)
{
printf("%s\n", info->name);
printf("\n\n");
}
/* Load the file. */
void load()
{
struct names *info;
FILE *fp;
fp = fopen("mlist", "rb");
if(!fp) {
printf("Cannot open file.\n");
exit(1);
}
/* free any previously allocated memory */
while(start) {
info = start->next;
free(info);
start = info;
}
/* reset top and bottom pointers */
start = last = NULL;
printf("\nLoading File\n");
while(!feof(fp)) {
info = (struct names *) malloc(sizeof(struct names));
if(!info) {
printf("Out of Memory");
return;
}
if(1 != fread(info, sizeof(struct names), 1, fp)) break;
dls_store(info, &start, &last);
}
fclose(fp);
}