Skip to content

Instantly share code, notes, and snippets.

@icyflame
Created November 12, 2016 09:37
Show Gist options
  • Save icyflame/639f2ddf1d44bcf4faeef6aad68c1a29 to your computer and use it in GitHub Desktop.
Save icyflame/639f2ddf1d44bcf4faeef6aad68c1a29 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int element;
struct node * prev;
struct node * next;
} node;
node * HEAD, * TAIL;
int main() {
HEAD = NULL;
TAIL = NULL;
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int i, temp;
node * thisNode;
for(i = 0; i < n; ++i) {
printf("Enter a LL element: ");
scanf("%d", &temp);
thisNode = ((node *) malloc(sizeof(node)));
thisNode->element = temp;
thisNode->next = NULL;
thisNode->prev = NULL;
if (!HEAD) {
HEAD = thisNode;
}
if (TAIL) {
thisNode->prev = TAIL;
TAIL->next = thisNode;
}
TAIL = thisNode;
}
printf("\nLinked list in order: ");
thisNode = HEAD;
while(thisNode) {
printf("%d ->", thisNode->element);
thisNode = thisNode->next;
}
printf("\nLinked list in reverse order: ");
thisNode = TAIL;
while(thisNode) {
printf(" %d ->", thisNode->element);
thisNode = thisNode->prev;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment