C 打印链接列表中链接的值
我已经用C写了一个链表程序。但是我打印链表的代码显示了一个错误。我不明白出了什么问题。有人能帮我找出代码中的错误吗C 打印链接列表中链接的值,c,C,我已经用C写了一个链表程序。但是我打印链表的代码显示了一个错误。我不明白出了什么问题。有人能帮我找出代码中的错误吗 #include<stdio.h> #include<stdlib.h> struct node{ int link_data; struct node *next; }; void add_node(struct node *,int); int delete_node(struct node *); struct node *front = NU
#include<stdio.h>
#include<stdlib.h>
struct node{
int link_data;
struct node *next;
};
void add_node(struct node *,int);
int delete_node(struct node *);
struct node *front = NULL,*rear = NULL;
int main(){
struct node *l = (struct node *)malloc(sizeof(struct node *));
struct node *inc = (struct node *)malloc(sizeof(struct node *));
int number,i,option,del_node;
while(1){
printf("Enter the option:\n1. Add node\n2. Delete node\n");
scanf("%d",&option);
switch(option){
case 1:
printf("Enter the number to be add\n");
scanf("%d",&number);
add_node(l,number);
break;
case 2:
del_node = delete_node(l);
printf("%d has been removed\n",del_node);
break;
case 3:
for(inc = front ; inc < rear ; inc = inc->next){
print("%d",inc->link_data); <<<<<<<<<<<<<<<<< Error part of the program
}
break;
case 4:
exit(0);
break;
}
}
}
void add_node(struct node *l,int number){
struct node *newnode = (struct node *)malloc(sizeof(struct node *));
if(front == NULL){
newnode->link_data = number;
newnode->next = NULL;
front = newnode;
l = newnode;
}
else{
newnode->link_data = number;
l -> next = newnode;
l = newnode;
}
}
int delete_node(struct node *l){
int node_del;
struct node *inc;
for(inc = front ; inc < l ;inc = inc -> next );
node_del = inc->link_data;
l = inc;
l -> next = NULL;
return node_del;
}
#包括
#包括
结构节点{
int-link_数据;
结构节点*下一步;
};
void add_节点(结构节点*,int);
int delete_节点(结构节点*);
结构节点*front=NULL,*rear=NULL;
int main(){
结构节点*l=(结构节点*)malloc(sizeof(结构节点*);
结构节点*inc=(结构节点*)malloc(sizeof(结构节点*);
整数,i,选项,del_节点;
而(1){
printf(“输入选项:\n1.添加节点\n2.删除节点\n”);
scanf(“%d”,选项(&O);
开关(选件){
案例1:
printf(“输入要添加的编号\n”);
scanf(“%d”和编号);
添加_节点(l,编号);
打破
案例2:
删除节点=删除节点(l);
printf(“%d已被删除\n”,del_节点);
打破
案例3:
用于(inc=前;inc<后;inc=inc->next){
打印(“%d”,inc->link_data);嗯,inc
和read
是指针。因此inc
没有多大意义(这样比较地址是没有用的)。您可以尝试:
for (inc = front; inc != rear; inc = inc->next)
嗯,inc
和read
是指针。因此inc
没有多大意义(这样比较地址是没有用的)。您可以尝试:
for (inc = front; inc != rear; inc = inc->next)
您想使用printf
,而不是print
。
还有一个名为i
的变量,您不使用它
在实际的打印位上,您从不分配rear
。您可能也不想使用testinc
;通常链接列表在其next
指针为NULL
时结束
在add_node
中,您将front
指向新节点。您可能不是有意这样做的
您想使用printf
,而不是print
。
还有一个名为i
的变量,您不使用它
在实际的打印位上,您从不分配rear
。您可能也不想使用testinc
;通常链接列表在其next
指针为NULL
时结束
在add_node
中,您将front
指向新节点。您可能不是有意这样做的。您能告诉我们显示的错误是什么吗?您能告诉我们显示的错误是什么吗?+1:但如果指针在同一对象内具有相同的类型和点,这是完全敏感的可以比较它们。但是,OP有指向不同对象的指针,比较没有多大意义。+1:但是,如果指针在同一对象内具有相同的类型和点,比较它们是完全明智的。然而,OP有指向不同对象的指针,比较没有多大意义。