Created
July 10, 2013 01:22
-
-
Save tbl3rd/5962768 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~/C # cat reverse.c | |
// make CFLAGS+=-std=c99 reverse | |
// ./reverse a b c d e f g h i j k l | |
#include <stdio.h> | |
#include <stdlib.h> | |
// A linked list of strings. | |
// .next is the next Item. | |
// .s is the payload string. | |
// | |
typedef struct Item { | |
struct Item *next; | |
const char *s; | |
} Item; | |
// Free the Item at p. | |
// | |
static void itemDelete(Item *p) | |
{ | |
free(p); | |
} | |
// Return new Item p with string s and next. | |
// Call itemDelete(p) eventually. | |
// | |
static Item *itemNew(Item *next, const char *s) | |
{ | |
Item *const result = malloc(sizeof *result); | |
result->next = next; | |
result->s = s; | |
return result; | |
} | |
// Free each Item in list at p. | |
// | |
static void listDelete(Item *p) | |
{ | |
while (p) { | |
Item *next = p->next; | |
itemDelete(p); | |
p = next; | |
} | |
} | |
// Return a new list p of Items containing the ac strings in av. | |
// Call listDelete(p) eventually. | |
// | |
static Item *listNew(int ac, const char *av[]) | |
{ | |
Item *result = 0; | |
for (int i = ac; i > 0; --i) { | |
result = itemNew(result, av[i - 1]); | |
} | |
return result; | |
} | |
// Return a new list p containing each Item from list in reverse order. | |
// Call listDelete(p) eventually. | |
// | |
static Item *listReverseNew(const Item *list) | |
{ | |
Item *result = 0; | |
for (const Item *p = list; p; p = p->next) { | |
result = itemNew(result, p->s); | |
} | |
return result; | |
} | |
// Return list in reversed order by reusing each Item from list. | |
// | |
static Item *listReverseOld(Item *list) | |
{ | |
Item *result = 0; | |
Item *p = list; | |
while (p) { | |
Item *const next = p->next; | |
p->next = result; | |
result = p; | |
p = next; | |
} | |
return result; | |
} | |
// Show list in order on one line after a : and preceeded by tag. | |
// | |
static void listShow(const char *tag, Item *list) | |
{ | |
printf("%s:", tag); | |
for (Item *p = list; p; p = p->next) printf(" %s", p->s); | |
printf("\n"); | |
} | |
int main(int ac, const char *av[]) | |
{ | |
Item *list = listNew(ac - 1, av + 1); | |
listShow(" list", list); | |
Item *reversed = listReverseNew(list); | |
listShow(" reversed", reversed); | |
Item *unreversed = listReverseOld(reversed); | |
listShow("unreversed", unreversed); | |
Item *rereversed = listReverseOld(unreversed); | |
listShow("rereversed", rereversed); | |
listDelete(rereversed); | |
listDelete(list); | |
return 0; | |
} | |
~/C # make CFLAGS+=-std=c99 reverse | |
cc -std=c99 reverse.c -o reverse | |
~/C # ./reverse a b c d e f g h i j k l | |
list: a b c d e f g h i j k l | |
reversed: l k j i h g f e d c b a | |
unreversed: a b c d e f g h i j k l | |
rereversed: l k j i h g f e d c b a | |
~/C # |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment