/* ============================================================================ Name : LinkedList.c Author : Alex Nelson Version : Copyright : Description : Starter list file for In-Class Assignment ============================================================================ */ #include "LinkedList.h" //Create a new node using malloc and return the pointer to it NODE * CreateNode(int newdata) { NODE * newnode; newnode = (NODE *) malloc(sizeof(NODE)); if (newnode == NULL) { printf("No Memory left !!\n"); return NULL; } //set data of node to the integer passed to the function newnode->data = newdata; //initialize next pointer to NULL newnode->next = NULL; return newnode; } //Create a new list using malloc and return the pointer to it LIST * CreateList() { //malloc a new list LIST * myList = (LIST *) malloc(sizeof(LIST)); //Set initial head pointer to NULL myList->head = NULL; //Initialize size to 0 myList->size = 0; return myList; } //Delete a node at given index and free it int DeleteIndex(LIST * myList, int index) { //Create pointers to nodes, prev and curr NODE* prevNode, *currNode; //set curr node to head currNode = myList->head; if (currNode == NULL) { //if list is empty printf("List must not be empty!\n"); return 1; } else if (index > myList->size - 1) { //index is greater than list size printf("List is not that big\n"); return 1; } else if (myList->size == 1) { //if list is size 1, set head pointer to null myList->head = NULL; free(currNode); myList->size--; return 0; } else if (index == 0) { //if index is 0, change head pointer to next myList->head = currNode->next; free(currNode); myList->size--; return 0; } else if (index > 0) { //as long as index is positive, iterate through and change pointer int i = 0; prevNode = currNode; for (i = 0; i < index - 1; i++) { prevNode = prevNode->next; } currNode = prevNode->next; prevNode->next = currNode->next; free(currNode); myList->size--; return 0; } else { printf("Must be a positive number\n"); return 1; } } //Wrapper function to create a new node and insert into the list void Insert(LIST * myList, int pos, int val) { //create new node with value NODE * myNode = CreateNode(val); //Insert node into that position InsertNode(myList, myNode, pos); } //Insert a new node into the list at a given position void InsertNode(LIST * myList, NODE * newdata, int pos) { //create pointers to curr and prev nodes //set currnode to point at head NODE* currNode = myList->head, *prevNode; if (myList->size == 0 && pos==0) { //If list size is 0, and pos = 0 set head pointer myList->head = newdata; //head pointer = new node newdata->next = NULL; //new node next is null myList->size = 1; //size is now 1 } else if (pos > myList->size || pos < 0) { printf("Cannot be inserted outside list bounds\n"); } else if (pos == 0) { //if pos = 0, set head node myList->head = newdata; //head node points to new data newdata->next = currNode; //new data next points to head node myList->size = myList->size + 1; //increase size of list by 1 } else if (pos <= myList->size) { //as long as in the range to be added int i; prevNode = currNode; //set prev node to head node for (i = 0; i < pos - 1; i++) { prevNode = prevNode->next; //iterate to point at node before insert point } currNode = prevNode->next; //set curr node to point at node after insert point newdata->next = currNode; //set new data next to point to currNode prevNode->next = newdata; //set prev node next to point to next data myList->size = myList->size + 1; //increase list size by 1 } } //Print the linked list starting with the node pointed to by the head void PrintList(LIST * myList) { //create pointer to current NODE * curr; if (myList->head == NULL) { //if list is empty printf("Empty List !!\n"); //print empty list } else { //curr points to the first node in the list curr = myList->head; printf("List of nodes: "); while (curr != NULL) { //iterate until end of list printf("%d ", curr->data); //print current node curr = curr->next; //Move to the next node } printf("\n"); //print new line } } void DeleteList(LIST* myList){ NODE * curr = myList->head; NODE * prev = curr; while(curr != NULL){ curr = curr->next; free(prev); prev = curr; } free(myList); }