C中键盘输入的链表
我正在编写一段代码,它接受用户输入的整数,创建一个链表,然后打印出链表。整数应该放在列表的前面 我需要一点帮助,我不知道如何把数字打印出来。C中键盘输入的链表,c,linked-list,C,Linked List,我正在编写一段代码,它接受用户输入的整数,创建一个链表,然后打印出链表。整数应该放在列表的前面 我需要一点帮助,我不知道如何把数字打印出来。 这是我的密码: Node.h #include <stdio.h> #include <stdlib.h> #ifndef _nodes #define _nodes typedef struct node_s { int d; struct node_s *next; } node1; node1
这是我的密码: Node.h
#include <stdio.h>
#include <stdlib.h>
#ifndef _nodes
#define _nodes
typedef struct node_s {
int d;
struct node_s *next;
} node1;
node1 list(int d);
node1 addbegin(int d, node1 *head);
node1 print(node1 *head);
#endif
#包括
#包括
#ifndef\u节点
#定义_节点
类型定义结构节点{
int d;
结构节点*next;
}节点1;
节点1列表(int d);
node1 addbegin(int d,node1*head);
node1打印(node1*头);
#恩迪夫
node.c
#include "node.h"
#include <stdio.h>
node1 *list_nodes(int d){
node1 *node;
node=(node1*)malloc(sizeof(node1));
node->d=d;
node->next=NULL;
return(node);
}
node1 init(node1 *head){
head->next=NULL;
}
node1 addbegin_nodes(node1 *head, int d){
node1 *newnode;
newnode=(node1*)malloc(sizeof(node1));
newnode=list_nodes(d);
head->next=newnode;
return(newnode);
}
node1 print_nodes(node1 *head){
node1 *temp;
temp=(node1*)malloc(sizeof(node1));
for(temp=head->next;
temp;
temp=temp->next)
printf("%d", temp->d);
return (d);
}
#include <stdlib.h>
#include <stdio.h>
#include "node.h"
int main(int argc, char* argv[]) {
node1 *head;
node1 *start;
int num;
printf("Please enter integers: ");
while(scanf("%d", &num)!=EOF){
head=(node1*)malloc(sizeof(node1));
list(head);
int i=0;
for(i=0;i>=0;i--){
addbegin(head, num);
print(head);
}
printf("\n");
print(head);
}
return 0;
}
#包括“node.h”
#包括
节点1*列表_节点(int d){
节点1*节点;
node=(node1*)malloc(sizeof(node1));
节点->d=d;
节点->下一步=空;
返回(节点);
}
节点1初始(节点1*头部){
head->next=NULL;
}
node1 addbegin_节点(node1*head,int d){
node1*newnode;
newnode=(node1*)malloc(sizeof(node1));
newnode=列表_节点(d);
head->next=newnode;
返回(newnode);
}
节点1打印节点(节点1*头){
节点1*温度;
temp=(node1*)malloc(sizeof(node1));
对于(温度=头部->下一步;
临时雇员
温度=温度->下一步)
printf(“%d”,temp->d);
返回(d);
}
Main.c
#include "node.h"
#include <stdio.h>
node1 *list_nodes(int d){
node1 *node;
node=(node1*)malloc(sizeof(node1));
node->d=d;
node->next=NULL;
return(node);
}
node1 init(node1 *head){
head->next=NULL;
}
node1 addbegin_nodes(node1 *head, int d){
node1 *newnode;
newnode=(node1*)malloc(sizeof(node1));
newnode=list_nodes(d);
head->next=newnode;
return(newnode);
}
node1 print_nodes(node1 *head){
node1 *temp;
temp=(node1*)malloc(sizeof(node1));
for(temp=head->next;
temp;
temp=temp->next)
printf("%d", temp->d);
return (d);
}
#include <stdlib.h>
#include <stdio.h>
#include "node.h"
int main(int argc, char* argv[]) {
node1 *head;
node1 *start;
int num;
printf("Please enter integers: ");
while(scanf("%d", &num)!=EOF){
head=(node1*)malloc(sizeof(node1));
list(head);
int i=0;
for(i=0;i>=0;i--){
addbegin(head, num);
print(head);
}
printf("\n");
print(head);
}
return 0;
}
#包括
#包括
#包括“node.h”
int main(int argc,char*argv[]){
node1*头部;
node1*启动;
int-num;
printf(“请输入整数:”);
while(scanf(“%d”,&num)!=EOF){
head=(节点1*)malloc(节点1)的大小;
名单(标题);
int i=0;
对于(i=0;i>=0;i--){
addbegin(head,num);
打印头;
}
printf(“\n”);
打印头;
}
返回0;
}
将打印节点更改为
//recursive version
void print_nodes(node1 *head)
{
if (head != NULL)
{
printf("%d\n", head->d);
print_nodes(head->next);
}
}
或
我现在不知道为什么这些方法应该返回任何东西,请在评论中澄清
node1 init(node1 *head){
head->next=NULL;
}
定义为返回node1,但似乎没有return语句。这可能是你问题的一部分吗?EOF是一个特殊的角色。你正在退出循环吗?是的,这是一个硬件问题,我已经为此工作了5个小时,自杀了!所以我最终决定寻求帮助。EOF是文件的结尾。调用
addBegin()
后,new\u节点中的值是您的新头,但在主文件中,您会放弃它。试试head=addBegin(…)
。哦,好吧,这很有道理!谢谢@ben当我试着说node.c:24:warning:control到达非void函数的末尾时,第24行在你的代码中,我不知道我的函数在什么地方。这也是警告,不是错误。代码有效吗?两种版本都试过了吗?