According to my current knowledge of how C works, if I use the function List_int_add(linklist, 50)
in this way without having to assign linklist = List_int_add(linklist, 50)
, the value of linklist would've been updated to the return value of List_int_add(linklist, 50)
, since in place of linklist
is the argument Node_int * head
, and the function returns head
. Since the argument is a pointer, shouldn't List_int_add(linklist, 50)
be sufficient to update linklist
?
When using List_int_add(linklist, 50)
, the output of List_int_print(linklist)
would start at 100, and not 50. But it works fine if I assign to linklist=List_int_add(linklist, 50)
.
(please ignore the typecasting at return, it is due to how I write the code previously, and I am in the midst of editing the code, I know it's not necessary)
Thanks in advance.
In main.c:
#include <stdio.h>
#include <stdlib.h>
#include "LinkedList/linkedlist.h"
int main(int argc, char ** argv){
Node_int * linklist = NULL;
linklist = List_int_add(linklist, 50);
linklist = List_int_add(linklist, 100);
linklist = List_int_add(linklist, 150);
linklist = List_int_add(linklist, 200);
linklist = List_int_add(linklist, 250);
linklist = List_int_add(linklist, 300);
linklist = List_int_add(linklist, 350);
linklist = List_int_remove(linklist,50);
List_int_print(linklist);
List_int_destroy(linklist);
if (linklist == NULL)
List_int_print(linklist);
return EXIT_SUCCESS;
}
In linkedlist.c:
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include "linkedlist.h"
/* Node_int_construct: create a Node_int */
static Node_int * Node_int_construct(int val);
/* Node_int_construct: create a Node_int */
static Node_int * Node_int_construct(int val){
Node_int * nd = (Node_int *) malloc(sizeof(Node_int));
nd->next = NULL;
nd->prev = NULL;
nd->value = val;
return nd;
}
Node_int * List_int_add(Node_int * head, int val){
Node_int * nd = Node_int_construct(val);
// insert at the end
Node_int * ptr = (Node_int *) head;
if (ptr == NULL){
head = nd;
head -> next = NULL;
head -> prev = NULL;
return (Node_int *) head;
}
while (ptr->next != NULL){
ptr = ptr->next;
}
nd->prev = ptr;
ptr->next = nd;
return (Node_int *) head;
}
Node_int * List_int_remove(Node_int * head, int val){
Node_int * target = List_int_search((const Node_int * const) head, val);
if (target == NULL){
return target;
}
if (target == head){
head = head->next;
head->prev = NULL;
free(target);
}
else if (target->next == NULL){
Node_int * ptr = target->prev;
ptr->next = NULL;
free(target);
}
else {
Node_int * prev = target->prev;
Node_int * next = target->next;
prev->next = next;
next->prev = prev;
free(target);
}
return head;
}
void List_int_destroy(Node_int * head){
Node_int * ptr = head;
Node_int * temp;
while (ptr != NULL){
temp = ptr;
ptr = ptr->next;
free(temp);
}
}
Node_int * List_int_search(const Node_int * const head, int val){
Node_int * ptr = (Node_int *) head;
while (ptr != NULL){
if (ptr->value == val){
return ptr;
}
ptr = ptr->next;
}
return ptr;
}
void List_int_print(const Node_int * const head){
Node_int * ptr = (Node_int*) head;
while (ptr != NULL){
printf("> %d \n", ptr->value);
ptr = ptr->next;
}
}
in linkedlist.h:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
typedef struct _node_int {
struct _node_int * next;
struct _node_int * prev;
int value;
} Node_int;
/* List_int_add: add Node_int at the end of list. */
Node_int * List_int_add(Node_int * head, int val);
/* List_int_remove: remove first item that matches val.
Returns head if successful, returns NULL if failure */
Node_int * List_int_remove(Node_int * head, int val);
/* List_int_destroy: Delete entire list. */
void List_int_destroy(Node_int * head);
/* List_int_search: Returns pointer to node of the first matching item in list,
NULL if node is not found. */
Node_int * List_int_search(const Node_int * const head, int val);
/* List_int_print: print int list. */
void List_int_print(const Node_int * const head);
#endif