Advertisement
Guest User

Untitled

a guest
Jun 26th, 2014
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.92 KB | None | 0 0
  1. /*
  2.  * DoubleLinkedList.c
  3.  *
  4.  *  Created on: Jun 26, 2014
  5.  *      Author: Arun
  6.  */
  7.  
  8.  
  9. /*
  10.  * DoubleLinkedList.c
  11.  *
  12.  *  Created on: Jun 18, 2014
  13.  *      Author: Arun
  14.  */
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <stdbool.h>
  19. #include "DoubleLinkedList.h"
  20.  
  21.  
  22. typedef struct DNode mainTemp;
  23. typedef struct DoubleLinkedList mainList;
  24.  
  25. //1 DONE
  26. DoubleLinkedList* allocDList(uint elementSize){
  27.  
  28.     struct DoubleLinkedList* list = &mainList;
  29.  
  30.      list->head = NULL;
  31.      list->tail = NULL;
  32.  
  33.      list->length = 0;
  34.      list->elementSize = elementSize;
  35.  
  36.      return list;
  37.  
  38.  
  39.  
  40. }
  41. //2 DONE
  42. void releaseDList(DoubleLinkedList* list){
  43.  
  44.     struct DNode* node = list->head;
  45.     struct DNode* next = NULL;
  46.  
  47.     while(node){
  48.  
  49.         struct DNode* next = node->next;
  50.         free(node);
  51.         node = next;
  52.  
  53.     }
  54.  
  55. }
  56. //3 DONE
  57. void insertDListElementAt(DoubleLinkedList* list, Object newElement, uint position){
  58.  
  59.     struct DNode* newNode = calloc(list, sizeOf(newElement));
  60.     newNode->data = newElement;
  61.  
  62.     int counter = 0;
  63.  
  64.     struct _DNode* current = list->head;
  65.  
  66.     while(counter < list->length){
  67.  
  68.             if(counter == position){
  69.  
  70.                 current->next = newNode;
  71.                 newNode->prev = current;
  72.                 newNode->next = current->next;
  73.                 current->next->prev = newNode;
  74.  
  75.             }
  76.             counter++;
  77.             current = current->next;
  78.         }
  79.  
  80. }
  81. //4 DONE
  82. void appendDList(DoubleLinkedList* list, Object newElement){
  83.  
  84.     struct DNode* newNode = calloc(list, sizeOf(newElement));
  85.     newNode->data = newElement;
  86.  
  87.     newNode = list->tail->next; // setting newNode as current tail's next
  88.     newNode->prev = list->tail; // setting up current tail as newNodes's prev
  89.     newNode = list->tail; // newNode as the new tail
  90.  
  91.  
  92. }
  93. //5 DONE
  94. void insertDList(DoubleLinkedList* list, Object newElement){
  95.  
  96.  
  97.  
  98.     struct DNode* newNode = (DNode*)calloc(list, sizeOf(newElement));
  99.  
  100.     newNode->data = newElement;
  101.  
  102.  
  103.     newNode->next = list->head;
  104.     newNode = list->head->prev;
  105.     newNode->prev = NULL;
  106.     list->head = newNode;
  107.  
  108. }
  109. //6 DONE
  110. DoubleLinkedList* reverseDList(DoubleLinkedList* list){
  111.  
  112.     struct DoubleLinkedList* newList = NULL;
  113.     newList = (struct DoubleLinkedList*) malloc(sizeOf(DoubleLinkedList));
  114.  
  115.     struct DNode* temp = NULL;
  116.  
  117.     temp = (DNode*)malloc(sizeOf(DNode));
  118.  
  119.     temp = list->tail;
  120.  
  121.  
  122.  
  123.     while(temp->prev != NULL){
  124.  
  125.         appendDList(newList, temp->data);
  126.         temp = temp->prev;
  127.  
  128.     }
  129.  
  130.     return newList;
  131.  
  132.  
  133.  
  134.  
  135. }
  136. //7 DONE
  137. DoubleLinkedList* halfList(DoubleLinkedList* list){
  138.  
  139.     struct DNode* slow = list->head;
  140.     struct DNode* fast = list->head;
  141.     struct DoubleLinkedList* newList = malloc(uint);
  142.  
  143.     if(list->head != NULL){
  144.  
  145.         while(fast != NULL && fast->next != NULL){
  146.  
  147.             fast = fast->next->next;
  148.             slow = slow->next;
  149.  
  150.         } // slow now ='s midpoint
  151.  
  152.         while(slow != NULL){
  153.  
  154.             appendDList(newList, slow->data);
  155.             slow = slow->next;
  156.  
  157.         }
  158.  
  159.         return newList;
  160.     }
  161.  
  162.     return newList;
  163.  
  164. }
  165. //8 DONE
  166. Object removeDList(DoubleLinkedList* list, int position){
  167.  
  168.     int counter = 0;
  169.     struct _DNode* temp = list->head;
  170.  
  171.     while(counter < list->length){
  172.  
  173.         if(counter == position){
  174.  
  175.             temp->prev->next = temp->next;
  176.             temp->next->prev = temp->prev;
  177.  
  178.             return temp->data;
  179.  
  180.         }
  181.         counter++;
  182.         temp = temp->next;
  183.     }
  184.  
  185. return NULL;
  186.  
  187. }
  188.  
  189. //9 DONE
  190. void printDList(DoubleLinkedList* list){
  191.  
  192.     struct _DNode* temp = list->head;
  193.  
  194.     while(temp->next != NULL){
  195.  
  196.         printf("%d", temp->data);
  197.         temp = temp->next;
  198.  
  199.     }
  200.  
  201. }
  202.  
  203. void debugDList(DoubleLinkedList* list){
  204.     DNode* iter = list->head;
  205.     while(iter){
  206.         printf("%p\t", iter);
  207.         iter = iter->next;
  208.     }
  209.     printf("\n");
  210.     iter = list->tail;
  211.     while(iter){
  212.         printf("%p\t", iter);
  213.         iter = iter->prev;
  214.     }
  215.     printf("\n\n");
  216. }
  217. // end of DoubleLinkedList.c
  218.  
  219.  
  220. // DoubleLinkedList.h
  221.  
  222. /*
  223.  * DoubleLinkedList.h
  224.  *
  225.  *  Created on: Jun 18, 2014
  226.  *      Author: Arun
  227.  */
  228.  
  229. #ifndef DOUBLELINKEDLIST_H_
  230. #define DOUBLELINKEDLIST_H_
  231.  
  232. #include <stdio.h>
  233. #include <stdlib.h>
  234. #include <stdbool.h>
  235.  
  236.  
  237. typedef unsigned int uint;
  238. typedef unsigned long ulong;
  239. typedef void* Object;
  240.  
  241. typedef struct _DNode{
  242.  
  243.     Object data;
  244.  
  245.     struct _DNode* prev;
  246.  
  247.     struct _DNode* next;
  248.  
  249. } DNode;
  250.  
  251.  
  252. typedef struct _DoubleLinkedList{
  253.  
  254.     DNode* head;
  255.  
  256.     DNode* tail;
  257.  
  258.     uint length;
  259.  
  260.     uint elementSize;
  261.  
  262. } DoubleLinkedList;
  263.  
  264. //1 DONE
  265. DoubleLinkedList* allocDList(uint elementSize);
  266. //2 DONE
  267. void releaseDList(DoubleLinkedList* list);
  268. //3 DONE
  269. void insertDListElementAt(DoubleLinkedList* list, Object newElement, uint position);
  270. //4 DONE
  271. void appendDList(DoubleLinkedList* list, Object newElement);
  272. //5 DONE
  273. void insertDList(DoubleLinkedList* list, Object newElement);
  274. //6 DONE
  275. DoubleLinkedList* reverseDList(DoubleLinkedList* list);
  276. //7 DONE
  277. DoubleLinkedList* halfList(DoubleLinkedList* list);
  278. //8 DONE
  279. Object removeDList(DoubleLinkedList* list, int position);
  280. //9 DONE
  281. void printDList(DoubleLinkedList* list);
  282. void debugDList(DoubleLinkedList* list);
  283.  
  284.  
  285. #endif /* DOUBLELINKEDLIST_H_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement