linked list - unable to modify global variable in c -
i'm learning c, , in program i'm trying implement simple linked list. each node of list contains integer, , pointer next node. pointer head
points first node in list, list empty, initialized head = null
.
i want 2 operations on list - populate it, , print it.
to populate list, i'm calling function insert_node
2 arguments: head
, , integer want insert.
the problem need function insert_node
change value of head
(so points updated list, instead of null). i'm not sure how that, made head
global variable, , i'm trying change value. reason, though value of head
changed inside function insert_node
, when call function again, head still has value of null.
questions:
why global variable value not changed globally?
i'm aware using global variables not practice, how can update pointer list? thinking having
insert_node
function return pointer list, way?
#include<stdio.h> #include<stdlib.h> struct node { int data; struct node *link; }; void insert_node(struct node *head, int n); void print_list(struct node *head); struct node *head = null; main() { int i; for(i=1; i<5; i++) insert_node(head, i*i); print_list(head); } void print_list(struct node *head) { if(head == null) return; else { printf("%i ", head->data); print_list(head->link); } return; } void insert_node(struct node *head, int n) { struct node n = {n, null}; struct node *next, *prev; int prev_data = 0; //case one: list empty - point head n, , set n.link null if(head == null) head = &n; //case two: n less first element in list: else if(n < head->data) { n.link = head; head = &n; } else { next = head; //case three: n.data equal existing element, nothing: while(next != null) { if(n == next->data) { printf("this element exists.\n\n"); return; } prev = next; //save current element next = next->link; //look @ next element } //case four: n.data greater last element: if(n > prev->data) { prev->link = &n; return; } //case five: n.data in between list elements: next = head; while(next != null) { prev_data = next->data; //save current element prev = next; //save pointer current element next = next->link; //look @ next element if((n > prev_data) && (n < next->data)) { prev->link = &n; n.link = next; return; } } } return; }
- because pass global
head
by value functioninsert_node()
.
functioninsert_node()
makes local variable (which, way, has namehead
might confuse you, because it's local , not global). modifies localhead
, changes not visible in global variablehead
. it's called shadowing (variable same name within local scope distinct other variable same name). - pass address of head function , make function parameter pointer pointer structure.
declaration
void insert_node(struct node **ptr_to_head, int n);
usage
insert_node(&head, 5);
now can modify head dereferencing ptr_to_head
in insert_node
function:
(*ptr_to_head)=&new_node; ^ ^ | | head = value returned malloc
and yes, can return head
insert_node function, don't forget make assignment head
in main function.
Comments
Post a Comment