1
好吧,這讓我流汗。我有一個正在編譯的程序,但是當我在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語句的,我相信有什麼不對的頭,我只是找不到issue.Any想法?
這對我來說工作得很好。我得到一個素數列表。 –
適合我。我收到了一個看起來像素數高達1000的列表。 – Matt
您使用的是什麼'gcc'版本?我在Repl.it上編譯它,它有gcc版本4.6.3 –