gpt4 book ai didi

c - 打印链表中链接的值

转载 作者:太空宇宙 更新时间:2023-11-04 06:07:37 25 4
gpt4 key购买 nike

我用 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 = 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;
}

最佳答案

$ gcc -o ll -Wall -g -O0 ll.c
ll.c: In function ‘main’:
ll.c:29:11: warning: implicit declaration of function ‘print’ [-Wimplicit-function-declaration]
ll.c:13:14: warning: unused variable ‘i’ [-Wunused-variable]

您想使用 printf , 不是 print .还有一个名为 i 的变量你不用的。

在实际的打印位上,您永远不会分配 rear .您可能也不想使用测试 inc < rear ;通常链表在它们的 next 结束时结束指针是 NULL .

add_node ,你点front到新节点。你可能不是故意的。

关于c - 打印链表中链接的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6912839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com