I'm currently coding a LinkedList implementation in C. I'm stumbling on the following problem: variable 'currentNode' set but not used.
I don't really understand this. I'm using the currentNode variable!
This is my code:
#include <stdio.h>
#include <stdlib.h>
struct node {
int val;
struct node * next;
};
int main()
{
struct node root;
root.val = 0;
root.next = NULL;
struct node currentNode;
currentNode = root;
int i;
for (i = 0; i < 10; ++i)
{
struct node newNode;
newNode.val = i;
currentNode.next = &newNode;
currentNode = newNode;
}
return 0;
}
You are never reading the currentNode variable. If you removed all lines that mention the variable, your program would be exactly the same.
(This is indeed a useful warning: in your case, which presumably was intended to build up a list of ten elements, it points to a fatal bug in your code which means that the actual code does nothing.)
First of all, you should probably mention that this warning only happens if you use -Wall or specifically -Wunused-but-set-variable.
Second, gcc's definition of usage is reading from a variable, not assigning to a variable.
i found another problem.
#include <stdio.h>
#include <stdlib.h>
struct node {
int val;
struct node * next;
};
int main()
{
struct node root;
root.val = 0;
root.next = NULL;
struct node currentNode;
currentNode = root;
// ....
int i;
for (i = 0; i < 10; ++i)
{
// newNode is a temporary value,
// its life time end before the next cycle
// u should new it
struct node newNode;
newNode.val = i;
currentNode.next = &newNode;
currentNode = newNode;
}
// ...maybe like this
int i;
for (i = 0; i < 10; ++i)
{
struct node* pNewNode = (struct node*)malloc(siof(struct node));
pNewNode->val = i;
currentNode.next = pNewNode;
currentNode = *pNewNode;
}
// free node
// ...
return 0;
}
Related
I have started to learn about linked lists, and I have written this code.
It should be a recursive call to create a new link in a linked list in c.
But, if you’ll check the output, you’ll see it’s passing over the middle links.
I don’t know why I’m losing the middle links.
Btw, I do have a destroy function in my code, I just didn’t write it here.
I do have a different version of a working code, I don’t ask for solutions, I’m only asking why this recursive idea doesn’t work.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct node {
int data;
struct node *next;
}node;
node *create(node **head, int data)
{
if(!*head) {
*head = malloc(sizeof(node));
assert(*head);
(*head)->data = data;
(*head)->next = NULL;
return *head;
}
node *new = NULL;
new = create(&new,data);
(*head)->next = new;
return *head;
}
void display(node *head)
{
assert(head);
node *current = head;
do
{
printf("%d\t",current->data);
current = current->next;
}while(current);
}
int main()
{
int count = 0, data = 0;
node *head = NULL;
printf("Enter list count:\n");
while(count <= 0){
scanf("%d",&count);
if(count <= 0) printf("\nEnter a valid number:\n");
}
while(count){
scanf("%d",&data);
head = create(&head,data);
count--;
}
printf("\nHere are the elements:\n");
display(head);
return 0;
}
As implemented create() either adds a new node to the tail or iterates to the next linked node. Logic changed to affect that. It's confusing that the first argument is called head to changed it to n. Changed main() to retain the head and made the program non-interactive for ease of testing. Recatored display to use a for() loop:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
} node;
node *create(node **n, int data) {
if(!*n) {
*n = malloc(sizeof(**n));
assert(*n);
(*n)->data = data;
(*n)->next = NULL;
return *n;
}
node *n2 = (*n)->next;
(*n)->next = create(&n2, data);
return n2;
}
void display(node *head) {
assert(head);
for(node *c = head; c; c = c->next) {
printf("%d\t", c->data);
}
}
int main() {
node *head = NULL;
node *tail = NULL;
for(int i = 0; i < 10; i++) {
tail = create(&tail, i);
if(!head) head = tail;
}
display(head);
return 0;
}
and it displays:
0 1 2 3 4 5 6 7 8 9
If you compile your code with NDEBUG (some folks do that for production) then your code no longer has any error handling.
Thank you all for your answers. I see the problem now, after “explaining to the duck” a thousand times. In function create(), under the if() block, I assigned (*head)->next = new; without first making it point to the last link, so it’s just over write the next link in every call to the function.
The solution is:
Add a “current” pointer points to the head(to not lose it’s value)
Iterate through the list until we find the last link,
assign current->next the value of new.
Here is the fixed section:
node *new = NULL;
new = create(&new,data);
node *current = *head;
while(current->next) current = current->next;
current->next = new;
return *head;
I am trying to create a function that turns an array to a linked list for further usage
typedef struct ListNode {
int val;
struct ListNode *next;
} ListNode;
ListNode * create_linked_list(int *nums , int count)
{
ListNode * ptr = (ListNode*)malloc(count*sizeof(ListNode));
for (int i =0; i < count; i++)
{
ListNode new;
new.val = nums[i];
ptr[i] = new;
}
for (int j=0; j < count; j++)
{
if ( j>=count )
ptr[j].next = NULL;
else
ptr[j].next = &ptr[j+1];
}
return ptr;
}
int main()
{
int nums[] = {2,4,3};
ListNode *node_ptr = create_linked_list(nums , sizeof(nums)/sizeof(nums[0]));
ListNode start = node_ptr[0];
}
I have this simple function that turns an array of integers into a linked list, assume the last node in the list is called x_node now x_node.next is equal to NULL because it is defined inth the second for loop in create_linked_list, but when I try to add a while loop in the main it results in a segmentation fault
int main()
{
int nums[] = {2,4,3};
ListNode *node_ptr = create_linked_list(nums , sizeof(nums)/sizeof(nums[0]));
ListNode start = node_ptr[0];
while (start.next != NULL)
{
printf("%d \n", start.val);
start = *start.next; // at the last element it should stop but a segfault is thrown
}
}
If you wan t to loop over a linked list, you need to create the list properly in the first place. Your create_linked_list function is totally wrong.
You want this:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct ListNode {
int val;
struct ListNode* next;
} ListNode;
ListNode* create_linked_list(int* nums, int count)
{
ListNode* head = NULL;
ListNode *previous = NULL;
for (int i = 0; i < count; i++)
{
// create new node and put value into it
ListNode *new = malloc(sizeof(ListNode));
new->val = nums[i];
new->next = NULL;
if (i == 0) // head will point to the first element
head = new;
if (previous)
{
// if previous node exists, link to newly created node
previous->next = new;
}
previous = new; // new node becomes previous node
}
return head;
}
int main()
{
int nums[] = { 2,4,3 };
ListNode* head = create_linked_list(nums, sizeof(nums) / sizeof(nums[0]));
// now it's up to you to write the print_list function
// print_list(head);
}
Now writing the print_list function should be easy. Hint: take a pencil and a piece if paper and draw the nodes with arrows as pointers pointing to the next node.
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int key;
struct node* next;
} node_t;
typedef node_t* node_ptr;
void main()
{
node_ptr p = NULL;
node_ptr listhead =NULL;
/*need to create a listhead with key = 1,2,3..10*/
int i;
for (i =1; i<= 10;i ++)
{
node_ptr temp;
p =(node_ptr)malloc(sizeof(node_t));
p->key = i;
p->next = NULL;
if( list_head == NULL )
{
list_head= p;
}
else
{
temp = listhead;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = p;
}
}
}
I am still pretty confused about linked list and I am not sure if I'm doing correctly and pretty sure it is wrong, could anyone help me out? This is just
a practice question :)
And also uptil the line of node_ptr listhead=NULL; is given in the question so i cannot change the stuffs above that.
By the way just to be clear, the question asks to insert keys 1,2,3..10 to the listhead.
"Insert" can imply inserting at the start of a list rather than at some point in the list or at the end of a list. For this assignment, what is "insert" supposed to mean?
Example code with for loop and minor changes like using node_ptr instead of node_t * to correspond with assignment typedef, and int main() instead of void main().
#include <stdlib.h>
typedef struct node{
int key;
struct node* next;
} node_t;
typedef node_t* node_ptr;
int main()
{
node_ptr list_head = NULL;
node_ptr p;
node_ptr temp;
int i;
/* create a list with keys = 1,2,3..10 */
for (i = 1; i <= 10; i++)
{
p = (node_ptr)malloc(sizeof(node_t));
p->key = i;
p->next = NULL;
if( list_head == NULL )
{
list_head= p;
}
else
{
for(temp = list_head; temp->next != NULL; temp = temp->next);
temp->next = p;
}
}
return 0;
}
Alternate version using pointer to pointer to node. This eliminates the initial check for list_head == NULL. It's beyond what you'd be using now for the assignment, but knowing how to do this could be useful in later assignments.
#include <stdlib.h>
typedef struct node{
int key;
struct node* next;
} node_t;
typedef node_t* node_ptr;
typedef node_t ** node_ptr_ptr;
int main()
{
node_ptr list_head = NULL;
node_ptr p;
/* ptr to either list_head or to last node.next */
node_ptr_ptr pptemp;
int i;
/* create a list with keys = 1,2,3..10 */
for (i = 1; i <= 10; i++)
{
p = (node_ptr)malloc(sizeof(node_t));
p->key = i;
p->next = NULL;
for(pptemp = &list_head; *pptemp != NULL; pptemp = &(*pptemp)->next);
*pptemp = p;
}
return 0;
}
For this particular case, since temp (or pptemp) is in main, it only needs to be initialized once and advanced once per loop:
#include <stdlib.h>
typedef struct node{
int key;
struct node* next;
} node_t;
typedef node_t* node_ptr;
typedef node_t ** node_ptr_ptr;
int main()
{
node_ptr list_head = NULL;
node_ptr p;
/* ptr to either list_head or to last node.next */
node_ptr_ptr pptemp = &list_head;
int i;
/* create a list with keys = 1,2,3..10 */
for (i = 1; i <= 10; i++)
{
p = (node_ptr)malloc(sizeof(node_t));
p->key = i;
p->next = NULL;
*pptemp = p;
pptemp = &p->next;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node, *LinkedList;
void CreateList(LinkedList N, int n)
{
N = (LinkedList)malloc(sizeof(Node));
N->next = NULL;
LinkedList new = N;
Node *p;
for (int i = 0; i < n; ++i) {
p = (Node *)malloc(sizeof(Node));
scanf("%d", &(p->data));
new->next = p;
new = p;
}
new->next = NULL;
}
int main()
{
LinkedList list;
CreateList(list, 20);
printf("%d", list->data);
return 0;
}
As you can see, I want to create a linkedlist and make it a function.
But when I "printf" linkedlist's data, it can't appear what i want.
Can you help me?
The direct problem, as M. Oehm notes, is that you pass the list object to the create function. The create function creates the list, but because the list object is not returned to main, main cannot see the list. To achieve what you want, do:
In main, declare the list as:
LinkedList *N; // a pointer
declare create as:
void CreateList(LinkedList **N, int n) // address of a pointer that receives the value
and dereference it in create:
*N = malloc(sizeof(Node)); // assign the value to the pointer in main
and now call it from main as:
CreateList(&N, 20); // pass the address of the pointer
I further note that you pass create an int, the number of elements in the list, but a list is typically made for an unknown number of elements. So you should read until end-of-file.
(All other required modifications in create I leave to you.)
Thank you all!! I solved this problem, and this is my code.
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
void create(Node* *head, int n)
{
*head = malloc(sizeof(Node));
(*head)->next = NULL;
Node* new = *head;
Node* p;
for (int i = 0; i < n; ++i) {
p = malloc(sizeof(Node));
scanf("%d", &(p->data));
new->next = p;
new = p;
}
}
int main()
{
Node* list;
create(&list,20);
printf("%d", ((list->next)->next)->data); //for test
return 0;
}
I'm getting assignment makes pointer from integer without a cast errors on lines 46 and 53, the two lines with double asterisks on either side, and for the life of me, I cannot figure out why. Linked lists are very new to me, I don't know how they work completely yet.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct node_def
{
int data;
struct node_def *next;
};
typedef struct node_def node;
node *makeNode (int a);
node *insertFront(node *head,node *new);
void printList(node *head);
int numNodes = 0;
int main()
{
srand(time(0));
int r = rand() % 10000;
int i = 0;
node *head = NULL;
node *tmp = NULL;
printf("How many nodes? ", numNodes);
scanf("%d", numNodes);
printf("\n");
head = insertFront(head, tmp);
for(i = 0; i < numNodes; i++)
{
makeNode(r);
printList(head);
}
printf("\n");
return 0;
}
node *makeNode (int a)
{
node *new = malloc(sizeof(node));
**new = a;**
return new;
}
node *insertFront(node *head, node *new)
{
**new->data = head;**
new->next = new;
return 0;
}
void printList(node *head)
{
int j = 0;
for(j = 0; j < numNodes; ++j)
{
while (head != NULL)
{
printf(" %4d", head->data);
head = head->next;
}
if(j % 10 == 0)
printf("\n");
}
return;
}
new = a is meant to make new nodes and assign them a random number from 0 - 9999.
You try to assign r to new, but new is a struct.
You make a pointer to struct : node *new
What you want to do is assigning r to new->data, which is an int.
node *insertFront(node *head, node *new)
{
**new->data = head;** // ** is meaningless
new->next = new; // new is a reserved key word, don't use it this way
return 0;
}
What you try to do is to put a NULL pointer as the head of your list.
Just push element into it within your makeNode function.
insert like this :
void createNode(node *head)
{
Node *new_node = malloc(sizeof(Node*));
new_node->data = rand() % 100000;
new_node->next = NULL;
if(head == NULL)
head = new_node;
else if(head != NULL)
//Here you have to adapt your list, search (linked list crud functions)
}
You have a bad understanding about what pointers are.
Hope it helps bro