好的,这让我很生气。我有一个正在编译的程序,但是当我在gcc上执行它时,我没有收到我想要的输出,而是输出为Error
。我很确定我的代码和Print
的调用至少是正确的。我在程序中找不到任何会弄乱输出的错误。
这是我的代码
#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;
}
由于printf之前的if语句,我相信头部有问题,我只是找不到问题。有什么想法吗?