Advertisement
muntasir007

Untitled

Feb 10th, 2016
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct node {
  4.     int val;
  5.     struct node *NEXT;
  6.     struct node *PREV;
  7. } node_t;
  8.  
  9. node_t *first, *last, *temp;
  10.  
  11. void create () {
  12.     first = (node_t*) malloc (sizeof(node_t));
  13.     last = (node_t*) malloc (sizeof (node_t));
  14.     first = last = NULL;
  15. }
  16. int isEmpty() {
  17.     if (first == NULL) return 1;
  18.     else return 0;
  19. }
  20.  
  21. void display () {
  22.     temp = first;
  23.     if (isEmpty ()) printf("The list is empty.");
  24.     while (temp != NULL) {
  25.         printf("%d ", temp->val);
  26.         temp = temp->NEXT;
  27.     }
  28.     printf("\n");
  29. }
  30.  
  31. void reverseDisplay() {
  32.     temp = last;
  33.     if (isEmpty ()) printf("The list is empty.");
  34.     while (temp != NULL) {
  35.         printf("%d ", temp->val);
  36.         temp = temp->PREV;
  37.     }
  38.     printf("\n");
  39. }
  40.  
  41. void insertFirst (int num) {
  42.     temp = (node_t*) malloc (sizeof (node_t));
  43.     temp->val = num;
  44.     temp->NEXT = first;
  45.     temp->PREV = NULL;
  46.     if (first!=NULL) first->PREV = temp;
  47.     else last = temp;
  48.     first = temp;
  49. }
  50.  
  51. void insertLast (int num) {
  52.     temp = (node_t*) malloc (sizeof (node_t));
  53.     temp->val = num;
  54.     temp->PREV = last;
  55.     temp->NEXT = NULL;
  56.     if (last!=NULL )last->NEXT = temp;
  57.     else first = temp;
  58.     last = temp;
  59. }
  60.  
  61.  
  62. int popFirst() {
  63.     int toBeReturned=0;
  64.     if (!isEmpty()) {
  65.         toBeReturned = first->val;
  66.         temp = (node_t*) malloc (sizeof (node_t));
  67.         temp = first->NEXT;
  68.         if (first->NEXT == NULL) {
  69.             first = last = NULL;
  70.         }
  71.         else {
  72.             free(first);
  73.             temp->PREV = NULL;
  74.             first = temp;
  75.         }
  76.     }
  77.     return toBeReturned;
  78. }
  79.  
  80. int popLast () {
  81.     int toBeReturned=0;
  82.     if (!isEmpty()) {
  83.         toBeReturned = last->val;
  84.         temp = (node_t*) malloc (sizeof (node_t));
  85.         temp = last->PREV;
  86.         if (first->NEXT == NULL) {
  87.             first = last = NULL;
  88.         }
  89.         else {
  90.             free(last);
  91.             temp->NEXT = NULL;
  92.             last = temp;
  93.         }
  94.     }
  95.     return toBeReturned;
  96. }
  97.  
  98. int displayFirst() {
  99.     if (isEmpty()) return 0;
  100.     return first->val;
  101. }
  102.  
  103. int displayLast() {
  104.     if (isEmpty()) return 0;
  105.     return last->val;
  106. }
  107. void menu() {
  108.     while (1) {
  109.         printf("Press 1 to insert at the beginning.\n");
  110.         printf("Press 2 to insert at the end.\n");
  111.         printf("Press 3 to pop an element from the beginning.\n");
  112.         printf("Press 4 to pop an element from the end.\n");
  113.         printf("Press 5 to show the first element.\n");
  114.         printf("Press 6 to show the last element.\n");
  115.         printf("Press 7 to display the list from first to last.\n");
  116.         printf("Press 8 to display the list in reverse order.\n");
  117.         printf("Press 0 to Exit.\n");
  118.         int n;
  119.         scanf("%d", &n);
  120.         if (n==0) break;
  121.         else if (n==1) {
  122.             printf("Enter the number: ");
  123.             int num;
  124.             scanf("%d", &num);
  125.             insertFirst(num);
  126.         }
  127.         else if (n==2) {
  128.             printf("Enter the number: ");
  129.             int num;
  130.             scanf("%d", &num);
  131.             insertLast(num);
  132.         }
  133.         else if (n==3) {
  134.             if (!isEmpty()) printf("Popped value = %d\n", popFirst());
  135.             else printf("The linked list is empty.\n");
  136.         }
  137.         else if (n==4) {
  138.             if (!isEmpty()) printf("Popped value = %d\n", popLast());
  139.             else printf("The linked list is empty.\n");
  140.         }
  141.         else if (n==5) {
  142.             if (!isEmpty()) printf("The first element = %d\n", displayFirst());
  143.             else printf("The linked list is empty.\n");
  144.         }
  145.         else if (n==6) {
  146.             if (!isEmpty()) printf("The last element = %d\n", displayLast());
  147.             else printf("The linked list is empty.\n");
  148.         }
  149.         else if (n==7) display();
  150.         else if (n==8) reverseDisplay();
  151.         else {
  152.             printf("Invalid choice! Try again.\n");
  153.         }
  154.     }
  155. }
  156. int main() {
  157.     create();
  158.     menu();
  159.  
  160.     return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement