#include<stdio.h>
#include<malloc.h>
typedef struct Node {
int data;
struct Node * next;
} Node;
void push(Node **headRef, int i){
//why does headRef == NULL in below if condition gives segmentation fault?
if(*headRef == NULL){
*headRef = malloc(sizeof(Node));
Node *head = *headRef;
head->data = i;
}
}
int main(int argc, char ** argv){
Node *head = NULL;
push(&head, 2);
printf("%d\n", head->data);
}
This code is of linked list where I try to push some data into the list.
My question is in the comment of push function.
No need for the test. If *headRef happens to be NULL, newnode->next will be set to NULL, otherwise to *headRef.
void push(Node **headRef, int i){
Node *new;
new = malloc(sizeof *new);
/* check for new==NULL omitted */
new->next = *headRef;
new->data = i;
*headRef = new;
}
Yes, segfault is later at head->data access (if you use headRef==NULL)
Related
#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!
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;
}
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;
}
I'm having issue with an assignment for my CS class. I've been working with a friend, and we've identified that our code has a memory leak, but we can't find what the problem is. Essentially, the code is supposed to create a linked list with digits from 2-1000. Then, the code uses deletemultiples in order to delete numbers that aren't prime. It does this by taking a number and deleting any multiples of that number in the linked list. When we used valgrind, it returned a memory leak.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct node{
int info;
struct node *next;
};
typedef struct node node;
node *inserthead(node *head, int a);
node *inserttail(node *head, int a);
node *deletemultiples(node *head, int a);
void printlist(node *head);
void freelist(node *head);
int main(){
node *head1 = NULL;
int i,j;
for(i = 2;i <= 1000;i++)
head1 = inserttail(head1,i);
for(i = 2; i <= 32; i++){
head1 = deletemultiples(head1,i);
}
printlist(head1);
freelist(head1);
}
node *inserthead(node *head, int a){
node *ptr;
ptr = (node*)malloc(sizeof(node));
ptr->info = a;
ptr->next = head;
return(ptr);
}
node *inserttail(node *head, int a){
node *ptr;
node *ptr2 = head;
ptr = (node*)malloc(sizeof(node));
ptr->info = a;
ptr->next = NULL;
if(head == NULL)
return(ptr);
else if (head->next == NULL){
head->next = ptr;
return(head);
}
while(head->next != NULL)
head = head->next;
head->next = ptr;
return(ptr2);
}
void printlist(node *head){
while(head!=NULL){
printf("%i ",head->info);
head = head->next;
}
printf("\n");
}
void freelist(node *head){
node *ptr = head;
while(head != NULL){
head = head->next;
free(ptr);
ptr = head;
}
}
node *deletemultiples(node *head, int a){
node *ptr = head, *temp = head;
while (ptr != NULL) {
if(ptr->info % a > 0){
ptr = ptr->next;
temp = temp->next;
}
else{
ptr = ptr->next;
temp->next = ptr;
}
}
return(head);
}
If anyone can help us figure out what we did wrong, it would be greatly appreciated!
Your deletemultiples() function never frees the nodes it unlinks, so freelist() never reaches them when it walks the list to delete it. Delete the nodes you remove the same way you did in freelist().
Alternatively, you could create an array of 1,000 nodes (It’s a good habit to turn constants like that into symbolic names.) and link and unlink nodes within that array as weak references. You would free the entire array in a single call when you destroy all lists that depend on it.
Kudos for looking for memory leaks in your code. It’ll save you a lot of grief.
I've been messing aroung with the linked list implementation in C and got stuck at this problem: why is it giving me expected expression before 'Node' in the following lines?:
Node *newNode1= malloc(sizeOf(Node));
Node *newNode2= malloc(sizeOf(Node));
Node *newNode3= malloc(sizeOf(Node));
Node *newNode4= malloc(sizeOf(Node));
I've never seen such problems in C before. What went wrong ?
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node_{
int data;
struct Node_ *next;
}Node;
void insertNodeBegin(Node **head, Node *newNode){
if((*head)==NULL){
(*head)->next = NULL;
*head = newNode;
}
else{
newNode->next = (*head)->next;
(*head)->next = newNode;
}
}
void printList(Node *head){
Node *current = head;
while(current != NULL){
printf("%d ", current->data);
current = current->next;
}
}
int main()
{
Node *head = NULL;
Node *newNode1= malloc(sizeOf(Node));
newNode1->data = 12;
Node *newNode2 = malloc(sizeOf(Node));
newNode2->data = 16;
Node *newNode3 = malloc(sizeOf(Node));
newNode3->data = 55;
Node *newNode4 = malloc(sizeOf(Node));
newNode4->data = 8;
insertNodeBegin(&head, newNode1);
insertNodeBegin(&head, newNode2);
insertNodeBegin(&head, newNode3);
insertNodeBegin(&head, newNode4);
printList(head);
return 0;
}
sizeOf(Node) should be sizeof(Node), note the small o. After all this is C, not Java. :)
In short reorder if commands inside insertNodeBegin()
if(*head == NULL){
*head = newNode;
(*head)->next = NULL;
}
Because in first execution of program head points to NULL and for it we haven't any allocated memory. As head in first execution not points to a Node location using head -> next not valid and cause segmentation fault in run time.
In node creation with malloc() we must check is memory allocation successful or not. for that i use createNode() function.
We check if memory allocation for Node is OK then add that Node to list. And finally Use void as main(void) argument.
After these change your code becomes
Code
#include <stdio.h>
#include <stdlib.h>
typedef struct Node_{
int data;
struct Node_ *next;
}Node;
void insertNodeBegin(Node **head, Node *newNode){
if(*head == NULL){
*head = newNode;
(*head)->next = NULL;
}
else{
newNode->next = (*head)->next;
(*head)->next = newNode;
}
}
void printList(Node *head){
Node *current = head;
while(current != NULL){
printf("%d ", current->data);
current = current->next;
}
}
int createNode(Node **node, int value){
*node = malloc(sizeof(Node));
if(*node){
(*node)->data = value;
return 1;
}else{
fprintf(stdout, "%s", "can't allocate ...\n");
return 0;
}
}
int main(void)
{
Node *head = NULL;
Node *newNode1;
Node *newNode2;
Node *newNode3;
Node *newNode4;
if(createNode(&newNode1, 12)) {
insertNodeBegin(&head, newNode1);
}
if(createNode(&newNode2, 16)) {
insertNodeBegin(&head, newNode2);
}
if(createNode(&newNode3, 55)) {
insertNodeBegin(&head, newNode3);
}
if(createNode(&newNode4, 8)) {
insertNodeBegin(&head, newNode4);
}
printList(head);
return 0;
}
I am trying to create a linked list in C and my code is as below.
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
}node_t;
void insert_into_list(node_t *,int);
void print_list(node_t *);
node_t *create_node(int );
void insert_into_list(node_t *head, int value){
node_t *temp ;
temp = create_node(value);
if(head == NULL){
printf("Inserting node for the first time\n");
head = temp;
}else {
head->next = temp;
}
}
void print_list(node_t *head){
node_t *current = head;
while(current!=NULL){
printf("%d----->",current->data);
current = current->next;
}
printf("NULL");
}
node_t *create_node(int value){
node_t *new_node = malloc(sizeof(node_t));
if(new_node==NULL){
printf("Memory allocation failed for the list creation. :(");
return NULL;
}
new_node->data = value;
new_node->next = NULL;
return new_node;
}
int main(int argc, char *argv[]) {
node_t *head = NULL;
insert_into_list(head,10);
if(head==NULL){
printf("Still head is NULL :(");
}else{
printf("Head is not NULL:)");
}
print_list(head);
return 0;
}
In main, I am calling insert_into_list and even after successful memory allocation, i am not able to get the head value with the newly created node. Still showing the value as NULL.
I have debugged with gdb and found that upto below code, head is not NULL
printf("Inserting node for the first time\n");
head = temp;
I thought I am passing by reference and expected the value to get reflected in the caller function.
Please correct me.
If you want to pass by reference (or rather, the equivalent) in C you must pass a pointer. To pass a pointer by reference you have to pass a pointer to the pointer.
So in e.g. insert_into_list you must declare head as a pointer to a pointer:
void insert_into_list(node_t **head, int value)
And use the dereference operator when accessing the head variable.
You call it using the address-of operator &:
node_t *head = NULL;
insert_into_list(&head,10);
struct node{
int data; struct node *next;
};
void push(struct node* head, struct node* n){
if(n!= NULL){
if(head==NULL)
head = n;
else {
n->next = head;
head = n;
}
} else printf("Cannot insert a NULL node");
}
struct node* pop(struct node* head){
if(head!=NULL){
struct node *n = head;
head = head->next;
return n;
} else {
printf("The stack is empty");
return NULL;
}
}
int main(){
int i;
struct node *head = NULL, *n;
for(i=15;i>0;i--){
struct node *temp = malloc(sizeof(struct node));
temp -> data = i;
temp->next = NULL;
push(head,temp);
}
n = head;
while(n!=NULL){
printf("%d ",n->data);
n=n->next;
}
return 0;
}
You need to pass the address of the pointer head to the function push. I your case the head is not getting modified because you are only passing the value in the head.
void push(struct node** head, struct node* n){
if(n!= NULL){
if(*head==NULL)
*head = n;
else {
n->next = *head;
*head = n;
}
} else printf("Cannot insert a NULL node");}
int main(){
int i;
struct node *head = NULL, *n;
for(i=15;i>0;i--){
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp -> data = i;
temp->next = NULL;
push(&head,temp);
}
n = head;
while(n!=NULL){
printf("%d ",n->data);
n=n->next;
}
return 0;}
You are passing the head pointer by value to the function push(head,temp);. The changes to head done inside push will not be reflected in the main() function.
You should pass address of head to push().
push(&head, temp);
and inside push():
*head = n;
Similar change will be required for pop(). You can verify what I am saying by adding a printf inside the loop in main() as: printf("%p\n", head);. The value of head will remain unchanged.
BTW, it is good practice to add a \n at the end of statement inside printf, it flushes the stdout stream immmediately hence your output is printed immediately on stdout (your computer screen).