Guest User

Untitled

a guest
Jun 25th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include "List.h"
  5.  
  6. typedef struct _list *nodePointer;
  7.  
  8. typedef struct _list {
  9.    int value;
  10.    nodePointer next;
  11. } list;
  12.  
  13. int middleList(List target) {
  14.    int index = 1;
  15.    int position = (length(target) / 2) + 1;
  16.    int value = 0;
  17.    nodePointer current = target;
  18.    while (index < position) {
  19.       current = current->next;
  20.       index++;
  21.    }
  22.    value = current->value;
  23.    return value;
  24. }
  25.  
  26. int isListElement(List list, int value) {
  27.    int inList = 0;
  28.    nodePointer current = list;
  29.    while (current != NULL && inList == 0) {
  30.       if (current->value == value) {
  31.          inList = 1;
  32.       }
  33.       current = current->next;
  34.  
  35.    }
  36.    return inList;
  37. }
  38.  
  39. List halfList(List target, int sequence) {
  40.    nodePointer current = target;
  41.    nodePointer previous = target;
  42.    int index = 1;
  43.    if (sequence == 0) {
  44.       if (length(target) > 1) {
  45.          current = target->next;
  46.          target = current;
  47.          free(previous);
  48.          index++;
  49.          while (current != NULL) {
  50.             index++;
  51.             previous = current;
  52.             current = current->next;
  53.             if (index % 2 == 1 && current != NULL) {
  54.                current = current->next;
  55.                free(previous->next);
  56.                previous->next = current;
  57.                index++;
  58.             }
  59.          }
  60.       } else {
  61.          free(target);
  62.       }
  63.    } else if (sequence == 1) {
  64.      if (length(target) > 2) {
  65.         while (current != NULL) {
  66.             index++;
  67.             previous = current;
  68.             current = current->next;
  69.             if (index % 2 == 0 && current != NULL) {
  70.                current = current->next;
  71.                free(previous->next);
  72.                previous->next = current;
  73.                index++;
  74.             }
  75.          }
  76.       }
  77.    }
  78.    return target;
  79. }
  80.  
  81. List halfSwitch(List target) {
  82.  int firstStart, firstEnd, lastStart, lastEnd, middle, position;
  83.  nodePointer PFS, PFE, PLS, PLE, current, M;
  84.  if (length(target) > 1) {
  85.    if ((length(target) % 2) == 0) {
  86.       position = 1;
  87.       firstStart = 1;
  88.       firstEnd = (length(target) / 2);
  89.       lastStart = (length(target) / 2) + 1;
  90.       lastEnd = length(target);
  91.       current = target;
  92.    } else {
  93.       position = 1;
  94.       firstStart = 1;
  95.       firstEnd = (length(target) / 2);
  96.       middle = (length(target) / 2) + 1;
  97.       lastStart = (length(target) / 2) + 2;
  98.       lastEnd = length(target);
  99.       current = target;
  100.    }
  101.    while (current != NULL) {
  102.       if (position == firstStart) {
  103.      PFS = current;
  104.       } else if (position == firstEnd) {
  105.      PFE = current;
  106.       } else if (position == lastStart) {
  107.      PLS = current;
  108.       } else if (position == lastEnd) {
  109.      PLE = current;
  110.       }
  111.       if ((length(target) % 2) == 1 && position == middle) {
  112.          M = current;
  113.       }
  114.       current = current->next;
  115.       position++;
  116.    }
  117.    if ((length(target) % 2) == 0) {
  118.       PLE->next = PFS;
  119.       PFE->next = NULL;
  120.       target = PLS;
  121.    } else {
  122.       M->next = PFS;
  123.       PLE->next = M;
  124.       PFE->next = NULL;
  125.       target = PLS;
  126.    }
  127.  }
  128.  return target;
  129. }
  130.  
  131. List combineLists(List to, List from) {
  132.    nodePointer currentTo = to;
  133.    nodePointer currentFrom = from;
  134.    while (currentTo != NULL) {
  135.       currentTo->value += currentFrom->value;
  136.       currentTo = currentTo->next;
  137.       currentFrom = c(No subject)urrentFrom->next;
  138.    }
  139.    disposeList(from);
  140.    return to;
  141. }
  142.  
  143. List createList(int value) {
  144.    List start = malloc(sizeof(struct _list));
  145.    start->next = NULL;
  146.    start->value = value;
  147.    return start;
  148. }
  149.  
  150. int addElements(List target) {
  151.    int total = 0;
  152.    nodePointer current = target;
  153.    while (current != NULL) {
  154.       total += current->value;
  155.       current = current->next;
  156.    }
  157.    return total;
  158. }
  159.  
  160. int length(List target) {
  161.    int length = 0;
  162.    nodePointer current = target;
  163.    while (current != NULL) {
  164.       current = current->next;
  165.       length++;
  166.    }
  167.    return length;
  168. }
  169.  
  170. List stackLists(List to, List from, int position) {
  171.    nodePointer currentTo = to;
  172.    nodePointer nextTo = to;
  173.    nodePointer currentFrom = from;
  174.    int index = 0;
  175.    if (position > 0 && position < length(to)) {
  176.       while (index < position) {
  177.      currentTo = currentTo->next;
  178.          index++;
  179.       }
  180.       nextTo = currentTo->next;
  181.       while (currentFrom->next != NULL) {
  182.          currentFrom = currentFrom->next;
  183.       }
  184.       currentTo->next = from;
  185.       currentFrom->next = nextTo;
  186.    } else if (position == 0) {
  187.       while (currentFrom->next != NULL) {
  188.          currentFrom = currentFrom->next;
  189.       }
  190.       currentFrom->next = to;
  191.       to = from;
  192.    } else if (position == length(to)) {
  193.       while (currentTo->next != NULL) {
  194.          currentTo = currentTo->next;
  195.       }
  196.       currentTo->next = from;
  197.    }
  198.    return to;
  199. }
  200.  
  201. List reverseList(List target) {
  202.    nodePointer next = target;
  203.    nodePointer current = target;
  204.    nodePointer previous = target;
  205.    if (length(target) > 2) {
  206.       next = target->next->next;
  207.       current = target->next;
  208.       previous = target;
  209.       current->next = previous;
  210.       while (next->next != NULL) {
  211.          previous = current;
  212.          current = next;
  213.          next = next->next;
  214.          current->next = previous;
  215.       }
  216.       next->next = current;
  217.       target->next = NULL;
  218.    } else if (length(target) == 2) {
  219.       next = target->next;
  220.       target->next = NULL;
  221.       next->next = target;
  222.    }
  223.    return next;
  224. }
  225.  
  226. List lastToFirst(List target) {
  227.    nodePointer current = target;
  228.    nodePointer previous = target;
  229.    if (length(target) > 2) {
  230.       while(current->next != NULL) {
  231.          previous = current;
  232.          current = current->next;
  233.       }
  234.       current->next = target->next;
  235.       previous->next = target;
  236.       target->next = NULL;
  237.    } else if (length(target) == 2) {
  238.       current = target->next;
  239.       current->next = target;
  240.       target->next = NULL;
  241.    }
  242.    return current;
  243. }
  244.  
  245. List disposeElement(List target, int position) {
  246.    nodePointer current = target;
  247.    nodePointer previous = target;
  248.    int index = 0;
  249.    while (index != position && position < length(target)) {
  250.       previous = current;
  251.       current = current->next;
  252.       index++;
  253.    }
  254.    if (index != 0 && index < (length(target) - 1)) {
  255.       previous->next = current->next;
  256.       free(current);
  257.    } else if (index == 0 && position < length(target)) {
  258.       previous = target;
  259.       target = target->next;
  260.       free(previous);
  261.    } else if (index == length(target) - 1) {
  262.       previous->next = NULL;
  263.       free(current);
  264.    }
  265.    return target;
  266. }
  267.  
  268. void addRear(List target, int value) {
  269.    List new = createList(value);
  270.    nodePointer current = target;
  271.    while (current->next != NULL) {
  272.       current = current->next;
  273.    }
  274.    current->next = new;
  275. }
  276.  
  277. List addFront(List target, int value) {
  278.    List new = createList(value);
  279.    new->next = target;
  280.    return new;
  281. }
  282.  
  283. void disposeList(List target) {
  284.    nodePointer current = target;
  285.    nodePointer previous = target;
  286.    while (current != NULL) {
  287.       previous = current;
  288.       current = current->next;
  289.       free(previous);
  290.    }
  291. }
  292.  
  293. void printList(List target) {
  294.    nodePointer current = target;
  295.    printf("[FIRST]->");
  296.    while (current != NULL) {
  297.       printf("[%d]->", current->value);
  298.       current = current->next;
  299.    }
  300.    printf("[X]\n");
  301. }
  302.  
  303. int checkValue(List target, int position) {
  304.    int index = 0;
  305.    nodePointer current = target;
  306.    while (index != position && position < length(target)) {
  307.       current = current->next;
  308.       index++;
  309.    }
  310.    return current->value;
  311. }
Advertisement
Add Comment
Please, Sign In to add comment