C 链表不打印?
好吧,这让我汗流浃背。我有一个正在编译的程序,但是当我在gcc上执行它时,我没有收到我想要的输出,相反,输出是C 链表不打印?,c,linked-list,output,C,Linked List,Output,好吧,这让我汗流浃背。我有一个正在编译的程序,但是当我在gcc上执行它时,我没有收到我想要的输出,相反,输出是Error。我确信我的代码和要求打印的代码至少是正确的。我在我的程序中找不到任何会扰乱输出的错误。 这是我的密码 #include <stdio.h> #include <stdlib.h> typedef struct node{ int data; struct node *next; }node; node *Inserttail(node
Error
。我确信我的代码和要求打印的代码至少是正确的。我在我的程序中找不到任何会扰乱输出的错误。
这是我的密码
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *next;
}node;
node *Inserttail(node *head, int x){
node *temp = (node*)malloc(sizeof(node));
temp->data = x;
temp->next = NULL;
node *temp1 = head;
if(head==NULL)
return temp;
else if(head->next ==NULL){
head ->next = temp;
return head;
}
while(head->next != NULL)
head = head->next;
head->next = temp;
return temp1;
}
void Print(node *head){
if(head == NULL)
printf("Error");
while(head != NULL){
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
node *Deletemultiples(node *head, int k){
node *temp = head, *old = temp;
if(head == NULL)
return NULL;
if(head->data == 1)
head= head->next;
while(temp!=NULL){
if(temp->data %k ==0 && temp->data != k)
old->next = temp->next;
old=temp;
temp= temp->next;
}
return head;
}
void Freelist(node *head){
node *temp = head;
while(head != NULL){
head = head -> next;
free(temp);
temp = head;
}
}
int main(){
node *head = NULL;
int i;
for(i=1; i<=1000; i++)
head = Inserttail(head, i);
for(i=2; i<=32; i++){
head = Deletemultiples(head, i);
}
Print(head);
Freelist(head);
return 0;
}
#包括
#包括
类型定义结构节点{
int数据;
结构节点*下一步;
}节点;
节点*Inserttail(节点*head,int x){
node*temp=(node*)malloc(sizeof(node));
温度->数据=x;
temp->next=NULL;
节点*temp1=头部;
if(head==NULL)
返回温度;
else if(head->next==NULL){
头部->下一步=温度;
回流头;
}
while(head->next!=NULL)
头部=头部->下一步;
头部->下一步=温度;
返回temp1;
}
无效打印(节点*头){
if(head==NULL)
printf(“错误”);
while(head!=NULL){
printf(“%d”,头部->数据);
头部=头部->下一步;
}
printf(“\n”);
}
节点*Deletemultiples(节点*head,int k){
节点*温度=头部,*旧=温度;
if(head==NULL)
返回NULL;
如果(头部->数据==1)
头部=头部->下一步;
while(temp!=NULL){
如果(临时->数据%k==0和临时->数据!=k)
旧->下一步=临时->下一步;
旧=临时;
温度=温度->下一步;
}
回流头;
}
无效自由列表(节点*头){
节点*温度=头部;
while(head!=NULL){
头部=头部->下一步;
免费(临时);
温度=水头;
}
}
int main(){
node*head=NULL;
int i;
对于(i=1;i您的代码是正确的。我使用gcc 5.4.0和
一切都好
它对我来说运行得非常好。我得到了一个素数列表。对我来说很有效。我得到了一个高达1000的素数列表。我试过运行它,但我认为你缺少了一个}
在您的打印函数中。看不出逻辑有任何问题。我能看到的唯一问题是,从列表中删除项目时,您从未释放内存。@low\u avg\t存储当前指针,移动到下一个,然后删除存储的指针。