Created
September 22, 2015 13:27
-
-
Save hello-josh/fa59faf713b7e48fe4d8 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
#include <stdio.h> | |
#include <stdlib.h> | |
typedef struct node { | |
struct node *prev; | |
struct node *next; | |
int info; | |
} Node_t; | |
void display(Node_t *start); | |
Node_t *prepend(Node_t *start, int info); | |
Node_t *append(Node_t *start, int info); | |
Node_t *insert_before(Node_t *start, int info, int before); | |
Node_t *insert_after(Node_t *start, int info, int after); | |
Node_t *reverse(Node_t *start); | |
int main() { | |
int choice, data, x; | |
Node_t *start = NULL; | |
while (1) { | |
printf("\n"); | |
printf("1. Display list\n"); | |
printf("2. Prepend to list\n"); | |
printf("3. Append to list\n"); | |
printf("4. Insert after value\n"); | |
printf("5. Insert before value\n"); | |
printf("6. Delete value\n"); | |
printf("7. Reverse\n"); | |
printf("9. End Program\n"); | |
printf("\nEnter your choice: "); | |
scanf("%d", &choice); | |
switch (choice) { | |
case 1: | |
display(start); | |
break; | |
case 2: | |
printf("What number do you want to prepend: "); | |
scanf("%d", &data); | |
start = prepend(start, data); | |
display(start); | |
break; | |
case 3: | |
printf("What number do you want to append: "); | |
scanf("%d", &data); | |
start = append(start, data); | |
display(start); | |
break; | |
case 7: | |
printf("Reversing list\n"); | |
start = reverse(start); | |
display(start); | |
break; | |
case 9: | |
printf("Goodbye\n\n"); | |
exit(0); | |
break; | |
default: | |
printf("Bad choice %d\n", choice); | |
} | |
} | |
} | |
void display(Node_t *ptr) { | |
if (ptr) { | |
printf("Item is %d, has prev %d, has next %d\n", | |
ptr->info, !!ptr->prev, !!ptr->next); | |
display(ptr->next); | |
} else { | |
printf("End of List\n"); | |
} | |
} | |
Node_t *create(int info) { | |
Node_t *temp = (Node_t *) malloc(sizeof(Node_t)); | |
temp->info = info; | |
temp->prev = temp->next = NULL; | |
return temp; | |
} | |
Node_t *prepend(Node_t *start, int info) { | |
Node_t *temp = create(info); | |
temp->next = start; | |
start = temp; | |
if (start->next) { | |
start->next->prev = start; | |
} | |
return temp; | |
} | |
Node_t *append(Node_t *start, int info) { | |
Node_t *p, *temp; | |
temp = create(info); | |
if (!start) { | |
return temp; | |
} | |
p = start; | |
while (p->next) { | |
p = p->next; | |
} | |
p->next = temp; | |
temp->prev = p; | |
return start; | |
} | |
Node_t *reverse(Node_t *start) { | |
if (!start) { | |
return start; | |
} | |
Node_t *current = start, *next = start->next; | |
current->next = NULL; | |
current->prev = next; | |
while (next) { | |
next->prev = next->next; | |
next->next = current; | |
current = next; | |
next = next->prev; | |
} | |
start = current; | |
return start; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment