Skip to content

Instantly share code, notes, and snippets.

@markogresak
Created June 12, 2014 19:53
Show Gist options
  • Save markogresak/f2f0f92b439caa6fcc32 to your computer and use it in GitHub Desktop.
Save markogresak/f2f0f92b439caa6fcc32 to your computer and use it in GitHub Desktop.
// izpiti komplet 1/5 - naloga 2.
#include <stdio.h>
#include <stdlib.h>
#define ST_ELEMENTOV 10
typedef struct node {
int value;
struct node *next;
} node;
node* addItem(node *, int);
node* revertList(node *);
int main(int argc, char const *argv[])
{
// node *list = (node *) malloc(sizeof(node));
// list->value = 0;
node *list = NULL;
for(int i = 1; i <= ST_ELEMENTOV; i++) {
list = addItem(list, i);
}
node *next = NULL;
while ((next = next == NULL ? list : list->next) != NULL) {
printf("list val: %d | next val: \n", list->value);
if(list->next != NULL)
printf("%d\n", list->next->value);
else
printf("NULL\n");
}
// revertList(list);
free(list);
return 0;
}
node* addItem(node *list, int value) {
if(list == NULL) {
list = (node *) malloc(sizeof(node));
list->value = value;
list->next = NULL;
return list;
}
else
addItem(list->next, value);
return list;
}
node* revertList(node *list) {
node *next = NULL;
while((next = list->next) != NULL)
printf("%d\n", next->value);
free(next);
return NULL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment