Reputation: 197
Okay this is making me sweat. I have a program that is compiling, but I am not receiving my desired output when I execute it on gcc, instead the output is Error
. I am pretty sure my code and call for Print
is at least correct. I can't find any error in my program that would mess up the output.
This is my code
#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;
}
Because of the if statement before the printf, I believe there is something wrong with head, I just can't find the issue.Any thoughts?
Upvotes: 1
Views: 820
Reputation: 2668
your code is correct. i run it under ubuntu 16.04 LTS with gcc 5.4.0 and all things are OK.
Upvotes: 1