Add-an-element function isn't working in a C program linked list -



Add-an-element function isn't working in a C program linked list -

i'm working on simple c programme intended create linked list , display elements using 2 functions shto() - add together alement, , afisho() - display elements of list. problem function shto() used add together element @ end of linked list doesn't work well. when list null doesn't add together single element @ list. i'm guessing part of code isn't correct.

if (koka == null) koka = shtesa;

actually i've placed these 3 lines initialize list element. if remove these 3 lines programme won't work.

koka = (node_s *)malloc(sizeof(node_s)); koka->vlera = 0; koka->next = null;

could help me create programme work, if remove these 3 lines ? appreciate help. in advance!

the code of programme below.

#include <stdio.h> #include <stdlib.h> typedef struct node { int vlera; struct node *next; } node_s; node_s *koka, *temp, *shtesa; node_s* shto (node_s *koka, int vlera) { shtesa = (node_s *)malloc(sizeof(node_s)); shtesa->vlera = vlera; shtesa->next = null; if (koka == null) koka = shtesa; else { temp = koka; while (temp->next != null) temp = temp->next; temp->next = shtesa; } homecoming koka; } void afisho (node_s *koka) { if (koka == null) printf("there's no element in list.\n"); else { temp = koka; { printf("%d\n", temp->vlera); } while ((temp = temp->next) != null); } } int main () { int i; koka = (node_s *)malloc(sizeof(node_s)); koka->vlera = 0; koka->next = null; (i=1; i<11; i++) shto (koka, i); afisho(koka); homecoming 1; }

when list empty, shto( ) detects in , sets koka (which head, guess) new created element:

if (koka == null) koka = shtesa; else ...

but within function koka al local variable, doesn't impact global koka. shto() returns koka (the local maybe modified variable), should plenty replace

for (i=1; i<11; i++) shto (koka, i);

by

for (i=1; i<11; i++) koka = shto (koka, i);

c linked-list

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -