Created
November 23, 2016 02:03
-
-
Save sideb0ard/c2827c16f55001deba981bc429638c9c to your computer and use it in GitHub Desktop.
scrappy - was reading Joel on Software, and wrote this in relation to an interview question in there - "how we you reverse a linked list?"
This file contains 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
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
// linked list | |
// | |
typedef struct sweary sweary; | |
struct sweary { | |
char wurd[64]; | |
sweary *next; | |
}; | |
// stack ///////////////////////////////////////////////// | |
typedef struct stack stack; | |
typedef struct stackitem stackitem; | |
struct stack { | |
int count; | |
void *head; | |
}; | |
struct stackitem { | |
void *item; | |
stackitem *next; | |
}; | |
stack *new_stack() | |
{ | |
printf("NEW STACK!\n"); | |
stack *s = calloc(1, sizeof(stack)); | |
s->count = 0; | |
return s; | |
} | |
void stack_push(stack* s, void *item) | |
{ | |
stackitem *si = calloc(1, sizeof(stackitem)); | |
si->item = item; | |
if (s->count == 0) { | |
s->head = si; | |
} | |
else { | |
si->next = s->head; | |
s->head = si; | |
} | |
s->count++; | |
} | |
void *stack_pop(stack *s) | |
{ | |
if (s->count == 0) | |
return NULL; | |
stackitem *item = s->head; | |
void *goods = item->item; | |
if (item->next != NULL) { | |
s->head = item->next; | |
} else { | |
s->head = NULL; | |
} | |
free(item); | |
s->count--; | |
return goods; | |
} | |
// end stack //////////////////////////////////////////// | |
sweary *new_swearwurd(char *wurd) | |
{ | |
if (strlen(wurd) > 63) { | |
printf("Git tae fock\n"); | |
return NULL; | |
} | |
sweary *w = calloc(1, sizeof(sweary)); | |
strcpy(w->wurd, wurd); | |
w->next = NULL; | |
return w; | |
} | |
void add_swearwurd(sweary *list, char *wurd) | |
{ | |
if ( (list == NULL) | |
|| (strlen(wurd) > 63)) { | |
printf("Git tae fock\n"); | |
return; | |
} | |
sweary *current; | |
current = list; // head of list | |
while (current->next != NULL) | |
current = current->next; | |
sweary *new = new_swearwurd(wurd); | |
current->next = new; | |
} | |
/////////////////////////////////////////////////////////////////// | |
// LIST Ops | |
void print_list(sweary *list) | |
{ | |
sweary *current; | |
current = list; | |
while (current->next != NULL) | |
{ | |
printf("%s!\n", current->wurd); | |
current = current->next; | |
} | |
printf("%s!\n\n", current->wurd); | |
} | |
sweary *reverse_list(sweary *list) | |
{ | |
// assert list > 1 yada yada | |
stack *s = new_stack(); | |
sweary *current; | |
current = list; | |
sweary *last_head = current; // save ptr to current head - it'll be the last in the reversed list | |
while (1) | |
{ | |
stack_push(s, current); | |
current = current->next; | |
if (current->next == NULL) | |
{ | |
stack_push(s, current); | |
break; | |
} | |
} | |
// REeeeeeeeeewind! //////////////////////////////////////////////////////////////// | |
sweary *newlist = stack_pop(s); // we'll return this as pointer to head | |
sweary *next; // these two change as we iterate through list | |
sweary *curnew; // | |
curnew = newlist; | |
while (1) | |
{ | |
next = stack_pop(s); | |
curnew->next = next; | |
curnew = next; | |
if (curnew == last_head) | |
{ | |
curnew->next = NULL; | |
break; | |
} | |
} | |
return newlist; | |
} | |
int main() | |
{ | |
sweary *sw_list = new_swearwurd("jobbie"); | |
add_swearwurd(sw_list, "boabie"); | |
add_swearwurd(sw_list, "boobie"); | |
add_swearwurd(sw_list, "patootie"); | |
print_list(sw_list); | |
sweary *rev = reverse_list(sw_list); | |
print_list(rev); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment