请帮助有关C中的链表 [英] please help about linked list in c

查看:74
本文介绍了请帮助有关C中的链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是..

像这样制作菜单

1.添加数据
2.删除数据
3.显示数据.
4.退出
输入:__

如果用户在菜单中选择"1",程序应打印

输入值:__

并且仅将整数值用作输入值.

程序必须按规则顺序将输入数据取到链表中.
如果用户输入非整数值,则应打印错误:输入值无效",
在链接列表中查找先前的输入数据,将其删除并再次获取输入数据.
当程序擦除非整数值时,它会显示数据已擦除."
如果没有输入数据,则应打印相应数据不存在.".

如果用户选择3.显示数据,则应按顺序打印所有数据.
(在一行中打印5个数据)

如果用户选择4.退出,程序应结束.
(此时,应释放已创建的链表.



->我的问题是我该如何取值(当我选择菜单1.添加数据时)只能是整数.
当用户输入非整数值(例如3.5、5.6)时,我必须得到警告消息错误:输入值无效"
而且我不知道如何在每行上打印5个数据.
(例如

1 2 5 6 8
7 0 9 4 11
...

)

战车与问候





这是我编写的代码.

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;
}

推荐答案

目前,您的input_node例程有点基本:
At the moment you input_node routine is a bit basic:
int input_node(int* pNum, int nFlag)
{
    printf("*%s : ", (nFlag == 1)?"Input value":"To delete value");
    scanf_s("%d", pNum);
    return 0;
}


但是,如果您查看 scanf_s [ ^ ],您将看到它有一个返回值,您将忽略它.
描述告诉我们返回码是什么:

返回成功转换和分配的字段数;返回值不包括已读取但未分配的字段.返回值0表示未分配任何字段.对于错误或错误,返回值为EOF在第一次尝试读取字符时遇到文件结尾字符或字符串结尾字符."

那么,当用户输入错误的整数值时,您会怎么办?

也许,如果您测试了返回码中的有效数字?还是针对错误代码值?

顺便说一句:帮自己一个忙:注释该代码!您可能还记得今天做什么,如何做到,但是下周呢?下个月?养成使用描述性名称并在操作过程中注释所有内容的习惯-这将为您节省很多时间!


But if you look at the definition of scanf_s[^] you will see that it has a return value, which you are ignoring.
The description tells us what the return code is:

"Returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end-of-file character or the end-of-string character is encountered in the first attempt to read a character."

So, what do you suppose happens when your user enters a bad integer value?

Perhaps, if you tested the return code for a valid number? Or against the error code value?

BTW: Do yourself a big favour: comment that code! You may remember what it does and how it does it today, but next week? Next month? Get into the habits of using descriptive names and commenting everything as you go along - it will save you a lot of time in the future!


这篇关于请帮助有关C中的链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆