I can not pass by reference my node - c

I dont want create general *head node and I want to pass by reference and chance my data but although create new node for next node I cant reach my new node on main.
İf I look n1.next in main I see it is null.Why ?What is wrong ?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node{
int data;
struct node* next;
};
void add(struct node** head,int data){
struct node * tmp = *head;
while(tmp != NULL){
tmp = tmp->next;
}
tmp = (struct node*) malloc(sizeof(struct node));
tmp->data= data;
tmp->next=NULL;
}
int main()
{
struct node n1;
n1.data=5;
n1.next=NULL;
add(&(n1.next),15);
printf("%d",n1.next->data);
return 0;
}

Instead of using a head pointer, are you trying to pass in the last next pointer in the list, and then update that to point to your new node? If so, add() should be
void add(struct node** head, int data) {
struct node* p = malloc(sizeof(*p));
p->data = data;
p->next = NULL;
*head = p;
}

Related

Not able to push when implementing linked list in C

here's part of my code for the linked list:
struct node {
float data;
int key;
struct node* next;
};
typedef struct{
struct node *head;
struct node *current;
int length;
} linked_list;
linked_list *init_list(){
linked_list *out = malloc(sizeof(linked_list));
struct node *head = NULL;
struct node *current = NULL;
out->head = head;
out->current = current;
out->length = 0;
return out;
}
void push_core(struct node *head, int key, float data){
struct node *link = malloc(sizeof(struct node));
link->data = data;
link->key = key;
link->next = head;
// readjust to point at the new first node
head = link;
printf("%f; ", head->data);
}
void push(linked_list *list, int key, float data){
push_core(list->head, key, data);
list->length ++;
}
void print_list_core(struct node *head){
struct node* ptr = head;
printf("\n[");
while(ptr != NULL){
printf("(%d,%f)", ptr->key, ptr->data);
ptr = ptr->next;
}
}
void print_list(linked_list *list){
print_list_core(list->head);
}
But in the main, after I initialized the linked list structure, I wasn't able to use push() to link new pointers, why is that?
linked_list *S = init_list();
for (int i = 0; i < n; i++)
{
push(S,i,0);
print_list(S);
printf("%d;", S->length);
}
To clarify, the length of the list does update correctly. But when I try to print the list it doesn't work. Also, it's interesting that in another file when I initially just worked with the node struct and defined global variables for head and current, the code works fine. But when I try to wrap them up inside this linked_list struct, things aren't quite working as expected.
The problem occurred because you passed the pointer value of list->head to your push_code function as a parameter. This is a function call-by-value. So, when you change the head pointer inside the push_core function, it actually do not change the list->head pointer that you are expecting to. One quick fix would be returning the newly created link pointer from the push_core function and save it as list->head. The following code should fix your problem.
struct node * push_core(struct node *head, int key, float data){
struct node *link = malloc(sizeof(struct node));
link->data = data;
link->key = key;
link->next = head;
return link;
}
void push(linked_list *list, int key, float data){
list->head = push_core(list->head, key, data);
list->length ++;
}

How to return pointer adress for structure variable

I was working on data structures and while I was writing some code, I needed to return the address of the pointer that was defined in the structure. So here's my code but when I compile and run it, it doesn't work and give an error message as " assignment makes pointer from integer without a cast ". How should I rewrite it?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node{
int x;
struct Node *next;
};
void main(){
int i;
struct Node *head;
head = (struct Node*) malloc(sizeof(struct Node));
head->next=NULL;
/*Ekle(5,head);
Ekle(10,head);
Ekle(15,head);
printf("Enter the value of 'ara' element");
scanf("%d",&i);
Print(head);
ArayaEkle(i,20,head);
Print(head);*/
head = siraliEkle(10,head);
head = siraliEkle(5,head);
Print(head);
}
void Print(struct Node *root){
while(root->next!=NULL){
root = root->next;
printf("%d\n",root->x);
}
}
struct Node *siraliEkle(int sayi, struct Node *root){
if(root==NULL){
root = (struct Node*) malloc(sizeof(struct Node));
root->x = sayi;
root->next = NULL;
return root;
}
else if(root->next==NULL){
if(root->x>sayi){
struct Node *temp = (struct Node*) malloc(sizeof(struct Node));
temp->x = sayi;
temp->next = root;
root = temp;
return root;
}
}
}
You must declare (or define) functions before using them.
Without declaration nor definition, types of function arguments are assumed to int and it will cause trouble when actual types are not int.
struct Node{
int x;
struct Node *next;
};
/* add these declarations */
void Print(struct Node *root);
struct Node *siraliEkle(int sayi, struct Node *root);
int main(){ /* also return type should be standard int */
Also don't forget to return something from siraliEkle even if root != NULL && (root->next != NULL || root->x <= sayi).
If you want to return the address for the struct pointer use the declaration
struct Node *siraliEkle(int sayi, struct Node *root);
As below and return:
return &root;

Node pointer to current Node during mutile function call

I have declared a global pointer ptr and want that it should point to current node during different function call.
This is a sample code where I am creating a new node in fun1 and inserting in link list. In func2 I want to update the other members of newNode in linklist with a different value.
Currently I am traversing the link list to get the current Node or last Node which I dont want since during insertion of new Records already we have to traverse to reach to last Node thus storing the address of last Node.
But by doing the below I am not getting the proper values. Kindly someone suggest where I went wrong.
I am doing like this :
#include<stdio.h>
#include <stdlib.h>
struct Node
{
int data1;
int data2;
struct Node* next;
};
struct Node* head=NULL;
struct Node* ptr =NULL; /* Global pointer */
void insertNode(struct Node ** , struct Node* );
void fun1();
void fun2();
void fun1()
{
struct Node* ptr1 =NULL;
ptr1 = (struct Node*)malloc(sizeof(struct Node*));
ptr1->data1=1; /* intilaizing with some values */
insertNode(&head,ptr1);
}
void fun2()
{
/* Updating the current Node in the linklist with new value . */
ptr->data2=2;
}
void insertNode(struct Node ** head, struct Node* NewRec)
{
if(*head ==NULL )
{
NewRec->next = *head;
*head = NewRec;
ptr=*head;
}
else
{
/* Locate the node before the point of insertion */
struct Node* current=NULL;
current = *head;
while (current->next!=NULL )
{
current = current->next;
}
NewRec->next = current->next;
current->next = NewRec;
ptr=current->next;
}
}
int main ()
{
fun1();
fun2();
while(head!=NULL)
{
printf("%d", head->data1);
printf("%d",head->data2);
head=head->next;
}
return 0;
}
You made a classic mistake.
This is wrong:
ptr1 = (struct Node*)malloc(sizeof(struct Node*));
The allocated space here is sizeof(struct Node*) which is the size of a pointer (usually 4 or 8 bytes depending on the platform). But you need to allocate space for the whole struct Node structure, whose size is sizeof(struct Node).
So you simply need this:
ptr1 = (struct Node*)malloc(sizeof(struct Node));
BTW: in C you don't cast the return value of malloc so you actually should write this:
ptr1 = malloc(sizeof(struct Node));

linked list of strings in C

I am trying to create a linked list of strings in C and have had problems adding the first Node into the list. For whatever reason my program prints NULL even though I reference the head variable to newNode but it does not copy the string from struct pointer to struct pointer. Any help is appreciated. Thanks!
#include "stdafx.h"
#include <stdlib.h>
#include <string.h>
typedef struct stringData {
char *s;
struct stringData *next;
} Node;
Node *createNode(char *s) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->s = s;
newNode->next = NULL;
return newNode;
}
void insert(Node *head, Node *newNode) {
if (head == NULL) {
head->s = newNode->s;
head = newNode;
}
}
void printList(Node *head) {
while (head != NULL) {
printf("%s\n", head->s);
head = head->next;
}
}
int main()
{
Node *head = createNode(NULL);
Node *a = createNode("A");
insert(head, a);
printList(head);
return 0;
}
Following code snippet is wrong:
void insert(Node *head, Node *newNode) {...}
...
insert(head, a);
You need to pass the pointer by reference. Currently you are changing local copy (argument).
Fix
Change your insert as:
void insert(Node **head, Node *newNode) {...}
And call as:
insert(&head, a);
What elseAtleast insert (and possibly) more functions are not fool-proof (guaranteed null pointer dereference, else case not handled etc). You need to debug and fix many such cases. Working your approach properly on paper before coding may help.
Here is a modified version of the code that gives an example of inserting new nodes at both the start of a list and the end of a list. In fact, the insert function could be used to insert a new node at any position in the list, since all it needs is a pointer to a link and a pointer to the node to be inserted.
#include <stdlib.h>
#include <stdio.h>
typedef struct stringData {
char *s;
struct stringData *next;
} Node;
Node *createNode(char *s) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->s = s;
newNode->next = NULL;
return newNode;
}
void insert(Node **link, Node *newNode) {
newNode->next = *link;
*link = newNode;
}
void printList(Node *head) {
while (head != NULL) {
printf("%s\n", head->s);
head = head->next;
}
}
int main(void)
{
Node *head = NULL;
Node *tail = NULL;
Node *n;
n = createNode("B");
// First node at start of list - head is updated.
insert(&head, n);
// First node is also the tail.
tail = n;
n = createNode("A");
// Insert node at start of list - head is updated.
insert(&head, n);
n = createNode("C");
// Insert node at end of list.
insert(&tail->next, n);
// Update tail.
tail = n;
printList(head);
return 0;
}

Having trouble with a simple linked list in C

I'm trying to understand linked lists. I created this code which is supposed to give me experience with linked lists. When I run this code I get 0, though the output should be 10,9,8,7,6,5,4,3,2,1,0. Could someone help me figure this out?
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int value;
struct node *next;
}node;
node * addnode (node *ptr, int value);
void traverse(node *ptr);
int main (void)
{
int i;
node *ptr,*root;
root=(node *)malloc(sizeof(node));
ptr=root;
root->value=0;
root->next=0;
for(i=1;i>10;i++)
ptr=addnode(ptr,i);
traverse(ptr);
return(0);
}
void traverse(node *ptr)
{
printf("%d\n",ptr->value);
if(ptr->next) /*assuming that the pointer to the next node is initialized to NULL, so if its NULL then the if codeblock won't execute*/
traverse(ptr->next);
}
node * addnode (node *ptr, int value)
{
node *newnode;
newnode=(node *)malloc(sizeof(node));
newnode->value=value;
newnode->next=ptr;
return(newnode);
}
There are two problems with your code:
Ideally, you would do traverse(root) instead of traverse(ptr).
You are adding nodes at the beginning of your linked list, when you should add them on the end of the list.
You can do it like this (source):
void Push(struct node** headRef, int newData) {
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
new_node->data = newData;
new_node->next = *headRef;
*headRef = new_node;
}
which in your case would look like:
void addnode(struct node** headRef, int newData) {
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
new_node->value = newData;
new_node->next = *headRef;
*headRef = new_node;
}
Then you add nodes like this: addnode(&root, some_value);

Resources