Want more features on Pastebin? Sign Up, it's FREE!
Guest

LinkedList.c

By: a guest on Oct 28th, 2012  |  syntax: C  |  size: 2.88 KB  |  views: 33  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2. * @file LinkedList.c
  3. * @author Davidthefat
  4. * @date 10/27/12
  5. * @version 1.0
  6. */
  7.  
  8. #include "LinkedList.h"
  9.  
  10. void *get(int index)
  11. {
  12.         if(index >= self->size)
  13.                 return NULL;
  14.         int i = 0;
  15.         self->iterator = self->head;
  16.         while(i++<index)
  17.                 self->iterator = self->iterator->next;
  18.         return self->iterator->data;
  19. }
  20. LList *construct()
  21. {
  22.         self = (LList*)malloc(sizeof(LList));
  23.         self->size = 2;
  24.         self->head = (Node*)malloc(sizeof(Node));
  25.         self->tail = (Node*)malloc(sizeof(Node));
  26.         self->head->next = self->tail;
  27.         self->tail->next = NULL;
  28.         self->head->data = NULL;
  29.         self->tail->data = NULL;
  30.         self->get = &get;
  31.         self->add = &add;
  32.         self->addEnd = &addEnd;
  33.         self->addBeg = &addBeg;
  34.         self->del = &del;
  35.         self->set = &set;
  36.         self->destruct = &destruct;
  37.         return self;
  38. }
  39. void add(void *in, int index)
  40. {
  41.         if(index >= self->size && self->size != 0)
  42.                 return (void)printf("Index out of bounds\n");
  43.         if(index == 0 && self->size == 0) {
  44.                 self->head->data = in;
  45.                 return;
  46.         } else if(index == 0)
  47.                 return addBeg(in);
  48.         int i = 0;
  49.         Node *temp = (Node*)malloc(sizeof(Node));
  50.         temp->data = in;
  51.         self->iterator = self->head;
  52.         while(i++<index-1)
  53.                 self->iterator = self->iterator->next;
  54.         temp->next = self->iterator->next;
  55.         self->iterator->next = temp;
  56.         self->size++;
  57. }
  58. void addEnd(void *in)
  59. {
  60.         Node *temp = (Node*)malloc(sizeof(Node));
  61.         temp->data = in;
  62.         temp->next = NULL;
  63.         self->tail->next = temp;
  64.         self->tail = temp;
  65.         self->size++;
  66. }
  67. void addBeg(void *in)
  68. {
  69.         Node *temp = (Node*)malloc(sizeof(Node));
  70.         temp->data = in;
  71.         temp->next = self->head;
  72.         self->head = temp;
  73.         self->size++;
  74. }
  75. char del(int index)
  76. {
  77.         Node *temp;
  78.         if(index >= self->size)
  79.                 return 0*printf("Index out of bounds\n");
  80.         int i = 0;
  81.         self->iterator = self->head;
  82.         while(i++<index-1)
  83.                 self->iterator = self->iterator->next;
  84.         temp = self->iterator;
  85.         self->iterator = self->iterator->next;
  86.         temp->next = self->iterator->next;
  87.         free(self->iterator->data);
  88.         free(self->iterator);
  89.         self->size--;
  90.         return 1;
  91. }
  92. void set(void *in, int index)
  93. {
  94.         if(index >= self->size)
  95.                 return (void)printf("Index out of bounds\n");
  96.         int i = 0;
  97.         self->iterator = self->head;
  98.         while(i++<index)
  99.                 self->iterator = self->iterator->next;
  100.         self->iterator->data = in;
  101. }
  102. void destruct()
  103. {
  104.         while(self->head->next && del(self->size-1))
  105.                 ;
  106. }
clone this paste RAW Paste Data