Reputation: 25
#include <stdio.h>
#include <stdlib.h>
struct node{
int coff;
int pow;
struct node* next;
};
struct node* head1 = NULL;
struct node* head2 = NULL;
void insert(int c, int p, struct node* head){
struct node* newnode = (struct node*)malloc(sizeof(struct node));
struct node* temp = head;
newnode->coff = c;
newnode->pow = p;
newnode->next = NULL;
if(head == NULL){
head = newnode;
}else{
while(temp->next != NULL){
temp = temp->next;
}
temp->next = newnode;
}
}
void display(struct node* h){
struct node* temp;
temp = h;
while(temp != NULL){
printf("%dx^%d ", temp->coff, temp->pow);
temp = temp->next;
}
printf("\n");
}
int main(){
insert(3,2,head1);
insert(5,1,head1);
insert(4,0,head1);
insert(9,2,head2);
insert(6,1,head2);
insert(2,0,head2);
display(head1);
display(head2);
return 0;
}
I have created node struct for my linked list. Then i have created two functions for insert and display polynomials. I have created head1 and head2 for store address of two different polynominals. And I want use that head1 and head2 as arguments in insert and display functions. then finally I wanna print both polynomials. But there is a bottleneck. When I execute my program it exits without giving any output.
My expected output is :
3x^2 5x^1 2x^0
9x^2 6x^1 4x^0
How can i solve this problem? Is there anything wrong with (struct node* head) arguments in the functions? Why this program exits without giving any output?
Thank you so much!
Upvotes: 0
Views: 97
Reputation: 510
Take a look at
void insert(int c, int p, struct node* head)
Because 'head' is passed by value (and all C parameters do so),
caller of this function has no chance to get the 'inside modified' head
.
So, in your case, in main()
, head1
is always NULL.
I modified a bit from your code, it seems works:)
But don't forget to free
all allocated nodes.
include <stdio.h>
#include <stdlib.h>
struct node{
int coff;
int pow;
struct node* next;
};
struct node* head1 = NULL;
struct node* head2 = NULL;
struct node* insert(int c, int p, struct node* head){
struct node* newnode = (struct node*)malloc(sizeof(struct node));
struct node* temp = head;
newnode->coff = c;
newnode->pow = p;
newnode->next = NULL;
if(head == NULL){
head = newnode;
}else{
while(temp->next != NULL){
temp = temp->next;
}
temp->next = newnode;
}
return head;
}
void display(struct node* h){
struct node* temp;
temp = h;
while(temp != NULL){
printf("%dx^%d ", temp->coff, temp->pow);
temp = temp->next;
}
printf("\n");
}
int main(){
head1 = insert(3,2,head1);
insert(5,1,head1);
insert(4,0,head1);
head2 = insert(9,2,head2);
insert(6,1,head2);
insert(2,0,head2);
display(head1);
display(head2);
return 0;
}
Upvotes: 1
Reputation: 1295
Your line head = newnode;
isn't actually changing head1
and head2
because the pointers themselves were passed by value. This results in head1
and head2
remaining NULL
, and thus resulting in no output. You can fix this issue by passing a pointer to a pointer to the head node to insert
. A quick modification to your code to implement this:
#include <stdio.h>
#include <stdlib.h>
struct node{
int coff;
int pow;
struct node* next;
};
struct node* head1 = NULL;
struct node* head2 = NULL;
void insert(int c, int p, struct node **head){
struct node* newnode = (struct node*)malloc(sizeof(struct node));
struct node* temp = *head;
newnode->coff = c;
newnode->pow = p;
newnode->next = NULL;
if(*head == NULL){
*head = newnode;
}else{
while(temp->next != NULL){
temp = temp->next;
}
temp->next = newnode;
}
}
void display(struct node* h){
struct node* temp;
temp = h;
while(temp != NULL){
printf("%dx^%d ", temp->coff, temp->pow);
temp = temp->next;
}
printf("\n");
}
int main(){
insert(3, 2, &head1);
insert(5, 1, &head1);
insert(4, 0, &head1);
insert(9, 2, &head2);
insert(6, 1, &head2);
insert(2, 0, &head2);
display(head1);
display(head2);
return 0;
}
Upvotes: 2