Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- #include "List.h"
- typedef struct _list *nodePointer;
- typedef struct _list {
- int value;
- nodePointer next;
- } list;
- int middleList(List target) {
- int index = 1;
- int position = (length(target) / 2) + 1;
- int value = 0;
- nodePointer current = target;
- while (index < position) {
- current = current->next;
- index++;
- }
- value = current->value;
- return value;
- }
- int isListElement(List list, int value) {
- int inList = 0;
- nodePointer current = list;
- while (current != NULL && inList == 0) {
- if (current->value == value) {
- inList = 1;
- }
- current = current->next;
- }
- return inList;
- }
- List halfList(List target, int sequence) {
- nodePointer current = target;
- nodePointer previous = target;
- int index = 1;
- if (sequence == 0) {
- if (length(target) > 1) {
- current = target->next;
- target = current;
- free(previous);
- index++;
- while (current != NULL) {
- index++;
- previous = current;
- current = current->next;
- if (index % 2 == 1 && current != NULL) {
- current = current->next;
- free(previous->next);
- previous->next = current;
- index++;
- }
- }
- } else {
- free(target);
- }
- } else if (sequence == 1) {
- if (length(target) > 2) {
- while (current != NULL) {
- index++;
- previous = current;
- current = current->next;
- if (index % 2 == 0 && current != NULL) {
- current = current->next;
- free(previous->next);
- previous->next = current;
- index++;
- }
- }
- }
- }
- return target;
- }
- List halfSwitch(List target) {
- int firstStart, firstEnd, lastStart, lastEnd, middle, position;
- nodePointer PFS, PFE, PLS, PLE, current, M;
- if (length(target) > 1) {
- if ((length(target) % 2) == 0) {
- position = 1;
- firstStart = 1;
- firstEnd = (length(target) / 2);
- lastStart = (length(target) / 2) + 1;
- lastEnd = length(target);
- current = target;
- } else {
- position = 1;
- firstStart = 1;
- firstEnd = (length(target) / 2);
- middle = (length(target) / 2) + 1;
- lastStart = (length(target) / 2) + 2;
- lastEnd = length(target);
- current = target;
- }
- while (current != NULL) {
- if (position == firstStart) {
- PFS = current;
- } else if (position == firstEnd) {
- PFE = current;
- } else if (position == lastStart) {
- PLS = current;
- } else if (position == lastEnd) {
- PLE = current;
- }
- if ((length(target) % 2) == 1 && position == middle) {
- M = current;
- }
- current = current->next;
- position++;
- }
- if ((length(target) % 2) == 0) {
- PLE->next = PFS;
- PFE->next = NULL;
- target = PLS;
- } else {
- M->next = PFS;
- PLE->next = M;
- PFE->next = NULL;
- target = PLS;
- }
- }
- return target;
- }
- List combineLists(List to, List from) {
- nodePointer currentTo = to;
- nodePointer currentFrom = from;
- while (currentTo != NULL) {
- currentTo->value += currentFrom->value;
- currentTo = currentTo->next;
- currentFrom = c(No subject)urrentFrom->next;
- }
- disposeList(from);
- return to;
- }
- List createList(int value) {
- List start = malloc(sizeof(struct _list));
- start->next = NULL;
- start->value = value;
- return start;
- }
- int addElements(List target) {
- int total = 0;
- nodePointer current = target;
- while (current != NULL) {
- total += current->value;
- current = current->next;
- }
- return total;
- }
- int length(List target) {
- int length = 0;
- nodePointer current = target;
- while (current != NULL) {
- current = current->next;
- length++;
- }
- return length;
- }
- List stackLists(List to, List from, int position) {
- nodePointer currentTo = to;
- nodePointer nextTo = to;
- nodePointer currentFrom = from;
- int index = 0;
- if (position > 0 && position < length(to)) {
- while (index < position) {
- currentTo = currentTo->next;
- index++;
- }
- nextTo = currentTo->next;
- while (currentFrom->next != NULL) {
- currentFrom = currentFrom->next;
- }
- currentTo->next = from;
- currentFrom->next = nextTo;
- } else if (position == 0) {
- while (currentFrom->next != NULL) {
- currentFrom = currentFrom->next;
- }
- currentFrom->next = to;
- to = from;
- } else if (position == length(to)) {
- while (currentTo->next != NULL) {
- currentTo = currentTo->next;
- }
- currentTo->next = from;
- }
- return to;
- }
- List reverseList(List target) {
- nodePointer next = target;
- nodePointer current = target;
- nodePointer previous = target;
- if (length(target) > 2) {
- next = target->next->next;
- current = target->next;
- previous = target;
- current->next = previous;
- while (next->next != NULL) {
- previous = current;
- current = next;
- next = next->next;
- current->next = previous;
- }
- next->next = current;
- target->next = NULL;
- } else if (length(target) == 2) {
- next = target->next;
- target->next = NULL;
- next->next = target;
- }
- return next;
- }
- List lastToFirst(List target) {
- nodePointer current = target;
- nodePointer previous = target;
- if (length(target) > 2) {
- while(current->next != NULL) {
- previous = current;
- current = current->next;
- }
- current->next = target->next;
- previous->next = target;
- target->next = NULL;
- } else if (length(target) == 2) {
- current = target->next;
- current->next = target;
- target->next = NULL;
- }
- return current;
- }
- List disposeElement(List target, int position) {
- nodePointer current = target;
- nodePointer previous = target;
- int index = 0;
- while (index != position && position < length(target)) {
- previous = current;
- current = current->next;
- index++;
- }
- if (index != 0 && index < (length(target) - 1)) {
- previous->next = current->next;
- free(current);
- } else if (index == 0 && position < length(target)) {
- previous = target;
- target = target->next;
- free(previous);
- } else if (index == length(target) - 1) {
- previous->next = NULL;
- free(current);
- }
- return target;
- }
- void addRear(List target, int value) {
- List new = createList(value);
- nodePointer current = target;
- while (current->next != NULL) {
- current = current->next;
- }
- current->next = new;
- }
- List addFront(List target, int value) {
- List new = createList(value);
- new->next = target;
- return new;
- }
- void disposeList(List target) {
- nodePointer current = target;
- nodePointer previous = target;
- while (current != NULL) {
- previous = current;
- current = current->next;
- free(previous);
- }
- }
- void printList(List target) {
- nodePointer current = target;
- printf("[FIRST]->");
- while (current != NULL) {
- printf("[%d]->", current->value);
- current = current->next;
- }
- printf("[X]\n");
- }
- int checkValue(List target, int position) {
- int index = 0;
- nodePointer current = target;
- while (index != position && position < length(target)) {
- current = current->next;
- index++;
- }
- return current->value;
- }
Advertisement
Add Comment
Please, Sign In to add comment