Skip to content

Instantly share code, notes, and snippets.

@yoshiokatsuneo
Created February 22, 2013 13:46
Show Gist options
  • Save yoshiokatsuneo/5013486 to your computer and use it in GitHub Desktop.
Save yoshiokatsuneo/5013486 to your computer and use it in GitHub Desktop.
Reverse linked list in Finnish C...
#include <stdlib.h>
#include <stdio.h>
struct node_t{
node_t *seuraava;
int val;
};
node_t *rev_linked_list(node_t *list)
{
node_t *edellinen = NULL;
node_t *tama = list;
if(!tama){return NULL;}
node_t *seuraava = list->seuraava;
while(tama){
tama->seuraava = edellinen;
edellinen = tama;
tama = seuraava;
if(seuraava){
seuraava = seuraava->seuraava;
}
}
return edellinen;
}
void show_list(node_t *ptr)
{
while(ptr){
printf("val=%d\n", ptr->val);
ptr = ptr->seuraava;
}
}
int main(void)
{
node_t n1, n2, n3;
n1.val = 1;
n2.val = 2;
n3.val = 3;
n1.seuraava = &n2;
n2.seuraava = &n3;
n3.seuraava = NULL;
node_t * ptr = &n1;
show_list(ptr);
printf("reversing...\n");
ptr = rev_linked_list(ptr);
printf("reversing end...\n");
show_list(ptr);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment