C 链表不打印
我正在尝试使用C创建一个简单的链表,我想我能够构建链表本身,但是当我尝试打印它时,它会打印最后一个节点的值,而不是列表中的所有值C 链表不打印,c,linked-list,C,Linked List,我正在尝试使用C创建一个简单的链表,我想我能够构建链表本身,但是当我尝试打印它时,它会打印最后一个节点的值,而不是列表中的所有值 #include <stdio.h> #include <alloca.h> typedef int DATA; struct Node { DATA d; struct Node *next; }; void printList(struct Node **head) { struct Node *newNode
#include <stdio.h>
#include <alloca.h>
typedef int DATA;
struct Node
{
DATA d;
struct Node *next;
};
void printList(struct Node **head)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
struct Node *temp;
temp = *head;
while(temp!=NULL)
{
printf("%d \n", temp->d);
temp = temp->next;
}
}
int main()
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
struct Node *head = newNode;
struct Node *temp = newNode;
head->d = 1;
int i = 0;
printf("Enter 3 numbers");
for( i = 0; i< 3; i++)
{
scanf("%d", &temp->d);
temp->next = newNode;
temp = temp->next;
}
temp->next = NULL;
printf("%d \n", temp->d);
return 0;
}
#包括
#包括
typedef int数据;
结构体类型
{
数据d;
结构节点*下一步;
};
无效打印列表(结构节点**头)
{
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
结构节点*temp;
温度=*水头;
while(temp!=NULL)
{
printf(“%d\n”,temp->d);
温度=温度->下一步;
}
}
int main()
{
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
结构节点*head=newNode;
结构节点*temp=newNode;
头部->d=1;
int i=0;
printf(“输入3个数字”);
对于(i=0;i<3;i++)
{
scanf(“%d”,&temp->d);
temp->next=newNode;
温度=温度->下一步;
}
temp->next=NULL;
printf(“%d\n”,temp->d);
返回0;
}
任何帮助/提示都将不胜感激。请用以下代码替换您的代码:-
#include<stdio.h>
#include <alloca.h>
typedef int DATA;
struct Node
{
DATA d;
struct Node *next;
};
void printList(struct Node **head)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
struct Node *temp;
temp = *head;
while(temp->next!=NULL)
{
printf("%d \n", temp->d);
temp = temp->next;
}
}
int main()
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
struct Node *head = newNode;
struct Node *temp = newNode;
head->d = 1;
int i = 0;
printf("Enter 3 numbers");
for( i = 0; i< 3; i++)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
scanf("%d", &temp->d);
temp->next = newNode;
temp = temp->next;
}
printList(head);
return 0;
}
#包括
#包括
typedef int数据;
结构体类型
{
数据d;
结构节点*下一步;
};
无效打印列表(结构节点**头)
{
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
结构节点*temp;
温度=*水头;
while(临时->下一步!=NULL)
{
printf(“%d\n”,temp->d);
温度=温度->下一步;
}
}
int main()
{
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
结构节点*head=newNode;
结构节点*temp=newNode;
头部->d=1;
int i=0;
printf(“输入3个数字”);
对于(i=0;i<3;i++)
{
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
scanf(“%d”,&temp->d);
temp->next=newNode;
温度=温度->下一步;
}
印刷品清单(标题);
返回0;
}
在这里,在获取输入时,还需要在循环中声明newNode
。因为,当前值被过度写入,旧值丢失,并且只打印最后一个节点的值
同样在打印时,检查
temp->next=空值
而不是temp=空
用以下代码替换您的代码:-
#include<stdio.h>
#include <alloca.h>
typedef int DATA;
struct Node
{
DATA d;
struct Node *next;
};
void printList(struct Node **head)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
struct Node *temp;
temp = *head;
while(temp->next!=NULL)
{
printf("%d \n", temp->d);
temp = temp->next;
}
}
int main()
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
struct Node *head = newNode;
struct Node *temp = newNode;
head->d = 1;
int i = 0;
printf("Enter 3 numbers");
for( i = 0; i< 3; i++)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
scanf("%d", &temp->d);
temp->next = newNode;
temp = temp->next;
}
printList(head);
return 0;
}
#包括
#包括
typedef int数据;
结构体类型
{
数据d;
结构节点*下一步;
};
无效打印列表(结构节点**头)
{
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
结构节点*temp;
温度=*水头;
while(临时->下一步!=NULL)
{
printf(“%d\n”,temp->d);
温度=温度->下一步;
}
}
int main()
{
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
结构节点*head=newNode;
结构节点*temp=newNode;
头部->d=1;
int i=0;
printf(“输入3个数字”);
对于(i=0;i<3;i++)
{
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
scanf(“%d”,&temp->d);
temp->next=newNode;
温度=温度->下一步;
}
印刷品清单(标题);
返回0;
}
在这里,在获取输入时,还需要在循环中声明newNode
。因为,当前值被过度写入,旧值丢失,并且只打印最后一个节点的值
同样在打印时,检查
temp->next=空值
而不是temp=空
您的代码中存在多个问题:
- 您没有正确创建列表。您只为一个节点分配内存,并且不正确地使用指针,导致链表指向同一内存
- 您根本没有调用printList函数
- 不清楚为什么要在printList中再次分配内存,而它应该打印已经创建的列表
- 另外,不需要为printList传递双指针,因为您没有修改列表
#include<stdio.h>
#include <alloca.h>
typedef int DATA;
struct Node
{
DATA d;
struct Node *next;
};
void printList(struct Node *head)
{
struct Node *temp = head;
while(temp!=NULL)
{
printf("%d \n", temp->d);
temp = temp->next;
}
}
struct Node *createNode()
{
struct Node *newNode;
newNode = malloc(sizeof(struct Node));
if (NULL == newNode)
return NULL;
memset(newNode, 0, sizeof(struct Node));
return newNode;
}
int main()
{
struct Node *newNode = NULL;
struct Node *headNode = NULL;
struct Node *temp = NULL;
int i = 0;
int data = 0;
printf("Enter 3 numbers\n");
for( i = 0; i< 3; i++)
{
scanf("%d", &data);
newNode = createNode();
if (NULL == newNode)
break;
newNode->d = data;
if (headNode == NULL)
{
headNode = newNode;
temp = newNode;
}
else
{
temp->next = newNode;
temp = temp->next;
}
}
printList(headNode);
return 0;
}
#包括
#包括
typedef int数据;
结构体类型
{
数据d;
结构节点*下一步;
};
无效打印列表(结构节点*头)
{
结构节点*温度=头部;
while(temp!=NULL)
{
printf(“%d\n”,temp->d);
温度=温度->下一步;
}
}
结构节点*createNode()
{
结构节点*newNode;
newNode=malloc(sizeof(struct Node));
if(NULL==newNode)
返回NULL;
memset(newNode,0,sizeof(struct Node));
返回newNode;
}
int main()
{
结构节点*newNode=NULL;
结构节点*headNode=NULL;
结构节点*temp=NULL;
int i=0;
int数据=0;
printf(“输入3个数字\n”);
对于(i=0;i<3;i++)
{
scanf(“%d”和数据);
newNode=createNode();
if(NULL==newNode)
打破
newNode->d=数据;
if(headNode==NULL)
{
headNode=newNode;
temp=newNode;
}
其他的
{
temp->next=newNode;
温度=温度->下一步;
}
}
打印列表(头节点);
返回0;
}
您的代码中存在多个问题:
- 您没有正确创建列表。您只为一个节点分配内存,并且不正确地使用指针,导致链表指向同一内存
- 您根本没有调用printList函数
- 不清楚为什么要在printList中再次分配内存,而它应该打印已经创建的列表
- 另外,不需要为printList传递双指针,因为您没有修改列表
#include<stdio.h>
#include <alloca.h>
typedef int DATA;
struct Node
{
DATA d;
struct Node *next;
};
void printList(struct Node *head)
{
struct Node *temp = head;
while(temp!=NULL)
{
printf("%d \n", temp->d);
temp = temp->next;
}
}
struct Node *createNode()
{
struct Node *newNode;
newNode = malloc(sizeof(struct Node));
if (NULL == newNode)
return NULL;
memset(newNode, 0, sizeof(struct Node));
return newNode;
}
int main()
{
struct Node *newNode = NULL;
struct Node *headNode = NULL;
struct Node *temp = NULL;
int i = 0;
int data = 0;
printf("Enter 3 numbers\n");
for( i = 0; i< 3; i++)
{
scanf("%d", &data);
newNode = createNode();
if (NULL == newNode)
break;
newNode->d = data;
if (headNode == NULL)
{
headNode = newNode;
temp = newNode;
}
else
{
temp->next = newNode;
temp = temp->next;
}
}
printList(headNode);
return 0;
}