Does array initialize memory in c? [duplicate] - c

This question already has answers here:
initial value of int array in C
(10 answers)
Closed 2 years ago.
Having this linked list:
#include <stdio.h>
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
typedef struct node node_t;
void printlist(const node_t*);
node_t *create_node(int);
int main(void){
int values[3] = {1,2,3};
node_t *nodes[3];
for(int i =0; i<3 ; i++)
{
nodes[i] = create_node(values[i]);
if(i!=2)
nodes[i]->next = nodes[i+1]; //HERE, can I assign next (un)initialized node?
}
node_t *header = nodes[0];
printlist(header);
}
void printlist(const node_t* header){
for(const node_t *i = header; i; i=i->next)
printf("value is %i\n",i->value);
}
node_t *create_node(int value){
node_t *new = malloc(sizeof(node_t));
new->value=value;
new->next = 0;
return new;
}
Which gives:
value is 1
value is 29590344
Command terminated
As I can see from output, the first node (header), does not have assign next struct member, which should happened from the loop. But I am assigning the next (pointer to new node) to an (un)initialized member from array (of pointer to nodes). I expect a initilization of array should also initilize a memory, when it has size. But does it? If so, then I do not understand why the assignment does not work, otherwise I understand and have to implement other loop. Thanks for answers.

Your code does not create the dynamic list as you have an array of 3 nodes. next is not needed.
You probably want something like this.
/* you need to check if memory allocation was successful */
#include <stdlib.h>
#include <stdio.h>
typedef struct node {
int value;
struct node *next;
} node_t;
void printlist(const node_t*);
node_t *append_node(node_t *, int);
int main()
{
node_t *head = NULL, *parent = head;
for(int x = 0; x < 10; x++)
if(!head)
{
head = append_node(NULL, x);
parent = head;
}
else
{
parent = append_node(parent, x);
}
printlist(head);
}
void printlist(const node_t* head){
while(head)
{
printf("value is %i\n",head->value);
head = head -> next;
}
}
node_t *append_node(node_t *parent, int value){
node_t *new = malloc(sizeof(*new));
new->value=value;
new->next = NULL;
if(parent) parent -> next = new;
return new;
}

Related

Double pointer in linked list

I almost figured out this code, but there are two details I can't figure out.
I found this code on YouTube.
source: https://www.youtube.com/watch?v=VOpjAHCee7c
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int vaule;
struct node *next;
}node_t;
void printlist(node_t *head)
{
node_t *temp = head;
while(temp != NULL)
{
printf("%d - ", temp->vaule);
temp = temp->next;
}
printf("\n");
}
node_t *create_node(int var)
{
node_t *res = malloc(sizeof(node_t));
res->vaule = var;
res->next = NULL;
return res;
}
node_t *insert_at_head(node_t **head, node_t *node_to_insert)
{
node_to_insert->next = *head;
*head = node_to_insert;
return node_to_insert;
}
void find_node()
int main()
{
node_t *tmp;
node_t *head = NULL;
for(int i = 0; i < 15; i++)
{
tmp = create_node(i);
head = insert_at_head(&head, tmp);
}
printlist(head);
return 0;
}
1) Why do we use the nested struct?
typedef struct node{
int vaule;
struct node *next;
}node_t;
I know about nested structures but I didn't understand why we use it here.
2) Why do we use double pointer?
node_t *insert_at_head(node_t **head, node_t *node_to_insert)
{
node_to_insert->next = *head;
*head = node_to_insert;
return node_to_insert;
}
if I change this code like this:
node_t *insert_at_head(node_t *head, node_t *node_to_insert)
{
node_to_insert->next = head;
return node_to_insert;
}
then nothing will change
Why do we use the nested struct?It's not a nested struct. struct node *next is a pointer, and as its name indidcates, it points to the next element.
Why do we use double pointer? Read this: How do I modify a pointer that has been passed into a function in C?
1)Why do we use the nested struct?
It is not a nested struct, but a linked list. Each node has a pointer to the next node (or to NULL for the last node of a list
2)Why do we use double pointer?
C only passes parameters by value. The idiomatic ways to change a variable from the caller are:
assign the return value to that variable. It is the best way, but you can only return one single value that way
pass a pointer to the variable and use the pointer to change the value. As we want to change the value of head which is already a pointer, we have to pass a pointer to pointer.
Here the write of the code has decided to change the passed header to clearly show that it is an input/output parameter, and also returns it because it had no better value to return.

How to change sinlgy linked list to doubly linked list?

I have a program that I am supposed to change it from a singly linked list to a doubly linked list. This means that I use pointer that points to the next node and a pointer that points to previous node.
How do I do this while recycling my previous code. Is there a way to do this with minimum steps involved?
#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable:4996)
//declaring structure
typedef struct node
{
char songName[20];
int songLength;
int copyright;
struct node * next;
}node;
//prototypes
node *create(int n);
void display_recursive(node *n);
int main()
{
int n = 0;
node *head = NULL;
printf("How many entries?\n");
scanf("%d", &n);
//call to create list
head = create(n);
printf("\nThe linked list in order is:\n");
display_recursive(head);
return 0;
}
node *create(int n)
{
node *head = NULL;
node *temp = NULL;
node *p = NULL;
for (int i = 0; i < n; i++)
{
temp = (node*)malloc(sizeof(node));
printf("What is the name of song %d\n", i + 1);
scanf("%s", &temp->songName);
printf("What is the length of song %d (in seconds)?\n", i + 1);
scanf("%d", &temp->songLength);
printf("Is song %d copyrighted?(1 = YES, 0 = NO)\n", i + 1);
scanf("%d", &temp->copyright);
temp->next = NULL;
if (head == NULL)
{
head = temp;
}
else
{
// if not empty, attach new node at the end
p = head;
while (p->next != NULL)
{
p = p->next;
}
p->next = temp;
}
}
return head;
}
void display_recursive(node *n) {
if (!n) {
return;
}
display_recursive(n->next);
printf("Song: %s, ", n->songName);
printf("%d minutes, ",n->songLength);
if (n->copyright == 1)
{
printf("Copyrights\n");
}
else if (n->copyright == 0)
{
printf("No copyrights\n");
}
}
I don't really know how the code should look or what I have to add to achieve a doubly linked list.
You just need a pointer point to previous node
typedef struct node
{
char songName[20];
int songLength;
int copyright;
struct node * next;
struct node* prev;
}node;
just like #T1412 said, you need to add a new member to the structure.
typedef struct node
{
char songName[20];
int songLength;
int copyright;
struct node * next;
struct node* prev;
}node
now you need to modify the create() function so that each node's prev pointer is pointing to previous node, and the HEAD node's prev points to NULL.
Similarly, you need to modify all the linked list related functions to incorporate the prev pointer.
1) strongly suggest changing:
typedef struct node
{
char songName[20];
int songLength;
int copyright;
struct node * next;
}node;
to:
struct NODE
{
char songName[20];
int songLength;
int copyright;
struct NODE * prev;
struct NODE * next;
};
typedef struct NODE node;
Then wherever in the code that it is linking in a new node, add the necessary statement to set the 'prior' field. Remember that the firs node will contain NULL in the 'prior' field.

How to print Linked List in C?

#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;
}

error in function that counts the number of times an int appears in a list

I'm trying to count the number of times a given int occurs in a list, but I'm having a difficult time getting my pointers to work. Can someone spot where is my logic failing? Is it because of how I'm implementing the "follows" "->" in the counting function?
//this is in my .h file
typedef struct list_struct LIST;
///// the rest is in my .c file
typedef struct node {
ElemType val;
struct node *next;
} NODE;
struct list_struct {
NODE *front;
NODE *back;
};
//this is my counting function
int lst_count(LIST *l, ElemType x) {
LIST *current = l;
int count = 0;
while (current != NULL) {
if ((current->front->val) == x) count++;
current = current->front->next;
//in the line above I get the following warning:
//"incompatible pointer types assigning to 'LIST*' (aka 'struct list_struct*') from 'struct node*'"
}
return count;
}
Your problem is in the while loop
You are in a list struct, then you do
current->front->next;
Now you are in a NODE type struct, in the next iteration there is no front in NODE.
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int val;
struct node *next;
struct node *previous;
} NODE;
int lst_count(NODE *l, int x) {
NODE *current = l;
NODE *start = current; /* so that we wont loose the start*/
int count = 0;
while (current != NULL) {
if ((current->val) == x)
count++;
current = current->next;
}
return count;
}
int main()
{
NODE* p = (NODE*)malloc(sizeof(NODE));
NODE* p1 = (NODE*)malloc(sizeof(NODE));
NODE* p2 = (NODE*)malloc(sizeof(NODE));
NODE* start = p;
p->val = 5;
p->next = p1;
p1->next = p2;
p2->next=NULL;
p1->val = 5;
p2->val = 5;
printf("%d", lst_count(start, 5));
}
I got the function to work thanks to your all advises
int lst_count(LIST *l, int x) {
NODE *current = l->front;
int count = 0;
while (current != NULL) {
if ((current->val) == x) count++;
current = current->next;
}
return count;
}

C linked list push function, just checking if this is correct

So I'm just wondering if this push function, which pushes a new value to the top a linked list stack is correct
void push(node** hd, int v){
node temp;
temp = (node*)malloc(sizeof(node));
temp -> val = v;
temp -> next = *hd;
*hd = temp;
}
Thanks in advance!
Also I'm wondering how I would make a pop function, to pop the most recently pushed value off of the stack.
The struct code looks like this by the way...
typedef struct nodeStruct
{
int val;
struct nodeStruct* next;
}node;
typedef node* list;
Dont define pointer types. They are confusing. Define a normal type and dereference it explicitly. Here is what you were trying to do without the pointer types.
#include <stdlib.h>
#include <stdio.h>
typedef struct nodeStruct
{
int val;
struct nodeStruct *next;
} node;
static void push(node **head, int v)
{
node *temp = malloc(sizeof(node));
temp->val = v;
temp->next = *head;
*head = temp;
}
int main(int argc, char **argv)
{
(void) argv;
(void) argc;
node *list = NULL;
for (int i=0; i<10; ++i) {
push(&list, i);
}
for(node *l = list; l != NULL; l = l->next) {
printf("%d ", l->val);
}
printf("\n");
return 0;
}
Note that you should check for failure of malloc. In other words, malloc can return NULL, which should be handled - left to you.

Resources