The Promblem is..
make the menu like this
1. Add data
2. Delete Data
3. Display Data.
4. Quit
Input: __
If user choose "1" in the menu, program should print
Input value: __
and take only integer value for input value.
Program has to take the input data in linked list in regular sequence.
If user input un-integer value, it should print "Error: Input value is invalid",
find previous input data in linked list, erase it and take input data again.
When program erase un-integer value, it prints "Data was erased."
If it dose not have input data, it should print "Corresponding data does not exist.".
If user choose 3. Display Data, it should print all data in regular sequence.
(print 5 data in one line)
If user choose 4. Quit, program should be over.
(in this time, linked list that made should be relieved.
-> My Question is How can I take the value(when I choose menu 1. Add Data) only integer.
I have to be warning message "Error : Input value is invalid" when user input un-integer value( like 3.5, 5.6)
And I don't know How to print 5 Data on each line.
(like
1 2 5 6 8
7 0 9 4 11
...
)
tanks and regards
And this is my code that I made.
#include <stdio.h>
#include <stdlib.h>
#define FUNC_SIZE 4
typedef struct node_record {
int nData;
struct node_record *next;
} _node;
typedef _node* list_node;
list_node head = NULL;
int list_menu(int*);
int input_num(int*, int);
int insert_node(void);
int delete_node(void);
int print_node(void);
int end_proc(void);
int main(void)
{
int nSelect=0, nRetry=1;
int (*func_node[FUNC_SIZE])(void)={insert_node, delete_node, print_node, end_proc};
while(nRetry)
{
list_menu(&nSelect);
nRetry = func_node[nSelect-1]();
}
return 0;
}
int list_menu(int* pSelect)
{
puts("********MENU********");
puts("* 1. Add Data *");
puts("* 2. Delete Data *");
puts("* 3. Display Data *");
puts("* 4. Quit *");
puts("********************");
do
{
fflush(stdin);
printf("*Choice Menu : ");
scanf_s("%d", pSelect);
} while(*pSelect > 4 || *pSelect < 1);
return 0;
}
int input_node(int* pNum, int nFlag)
{
printf("*%s : ", (nFlag == 1)?"Input value":"To delete value");
scanf_s("%d", pNum);
return 0;
}
int insert_node(void)
{
list_node temp, ptr, preptr;
int nData=0;
preptr = head;
temp = (list_node)malloc(sizeof(_node));
input_node(&nData, 1);
if(temp == NULL)
{
printf("*Memory wasn't enough.");
exit(1);
}
temp->nData = nData;
if(head == NULL || head->nData > nData)
{
temp->next = head;
head = temp;
}
else
{
ptr = preptr->next;
while(ptr != NULL)
{
if(ptr->nData > nData)
break;
preptr = ptr;
ptr = ptr->next;
}
temp->next = ptr;
preptr->next = temp;
}
return 1;
}
int delete_node(void)
{
list_node preptr, ptr;
int nData=0;
input_node(&nData, 2);
if(head == NULL)
{
puts("*Data does not exist.");
}
else if(head->nData == nData)
{
preptr = head;
head = preptr->next;
free(preptr);
puts("*Data was erased.");
return 1;
}
else
{
preptr = head;
ptr = preptr->next;
while(ptr != NULL && ptr->nData != nData)
{
preptr = ptr;
ptr = ptr->next;
}
if(ptr == NULL)
puts("*Corresponding data does not exist.");
else
{
preptr->next = ptr->next;
free(ptr);
puts("*Data was erased.");
}
}
return 1;
}
int print_node(void)
{
int nIdx=1;
list_node preptr;
preptr = head;
if(preptr == NULL)
puts("*Data does not exist.");
else
{
printf("*Display Data : ");
while(preptr != NULL)
{
printf("%d ", preptr->nData);
preptr = preptr->next;
}
printf("\n");
}
return 1;
}
int end_proc(void)
{
list_node temp;
while(head != NULL)
{
temp = head;
head = temp->next;
free(temp);
}
puts("Quit the program.");
return 0;
}