Skip to content

Instantly share code, notes, and snippets.

@6167656e74323431
Created July 2, 2018 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 6167656e74323431/74f764ff43dd729aab8ce85bb98b57e4 to your computer and use it in GitHub Desktop.
Save 6167656e74323431/74f764ff43dd729aab8ce85bb98b57e4 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
typedef struct ListNode // list node structure
{
int value;
struct ListNode * next;
} node_t;
node_t * newList(unsigned int size, int node_values[]) // craete a new linked list
{
node_t * current_node, * new_node, * list_head;
// create head
current_node = malloc(sizeof(node_t));
current_node->value = node_values[0];
current_node->next = NULL;
list_head = current_node;
// assign more values
for (int i = 1; i < size; ++i)
{
new_node = malloc(sizeof(node_t));
new_node->value = node_values[i];
new_node->next = NULL;
current_node->next = new_node;
current_node = new_node;
}
return list_head;
}
int listLength(node_t * list_head) // find length of the list
{
unsigned int list_length = 1;
node_t * current_node;
current_node = list_head;
while (current_node->next != NULL) // go through every item in the list
{
current_node = current_node->next;
list_length++;
}
return list_length;
}
int getNodeValue(node_t * list_head, unsigned int node_location) // get the value of an item in a specific node
{
if (node_location < listLength(list_head)) // check if item exists
{
node_t * current_node;
current_node = list_head;
for (int i = 0; i < node_location; ++i) // go to the position
{
current_node = current_node->next;
}
return current_node->value;
}
}
void changeNodeValue(node_t * list_head, unsigned int node_location, int node_value) // change the value of a node
{
if (node_location < listLength(list_head)) // check if item exists
{
node_t * current_node;
current_node = list_head;
for (int i = 0; i < node_location; ++i) // go to the position
{
current_node = current_node->next;
}
current_node->value = node_value;
}
}
node_t * insertNode(node_t * list_head, unsigned int node_location, int node_value) // insert at a certain point in the list
{
node_t * current_node;
node_t * new_node;
current_node = list_head;
// create new node
new_node = malloc(sizeof(node_t));
new_node->value = node_value;
if (node_location >= listLength(list_head)) // append to the end
{
while (current_node->next != NULL) // go to the end of the list
{
current_node = current_node->next;
}
// point new node
new_node->next = NULL;
// point the last node to the current node
current_node->next = new_node;
return list_head; // return the header
}
else if (node_location == 0) // new node becomes head
{
new_node->next = list_head;
return new_node;
}
else // insert in the middle
{
for (int i = 0; i < node_location - 1; ++i) // goto the correct node
{
current_node = current_node->next;
}
// point new node
new_node->next = current_node->next;
// point the last node to the current node
current_node->next = new_node;
return list_head; // return the header
}
}
node_t * destroyNode(node_t * list_head, unsigned int node_location) // destroy a node
{
node_t * current_node;
if (node_location < listLength(list_head) && node_location != 0) // check if item exists
{
node_t * node_to_delete, * prev_node, * next_node;
current_node = list_head;
for (int i = 0; i < node_location - 1; ++i) // go to the prior to the node
{
current_node = current_node->next;
}
// find addressees for nodes around the the node being removed
prev_node = current_node;
current_node = current_node->next;
node_to_delete = current_node;
next_node = node_to_delete->next;
prev_node->next = next_node; // remove the node to delete from the chain
free(node_to_delete); // destroy the node
}
else if (node_location == 0) // if the head is being deleted
{
current_node = list_head->next;
free(list_head);
return current_node;
}
return list_head;
}
void destroyList(node_t * list_head) // free the entire list
{
node_t * current_node;
current_node = list_head;
while (current_node->next != NULL)
{
list_head = current_node;
current_node = current_node->next;
free(list_head);
}
list_head = current_node;
current_node = current_node->next;
free(list_head);
}
int main()
{
node_t * head;
int initial_list[6] = {2, 3, 7, 4, 4, 6};
head = newList(6, initial_list);
head = insertNode(head, 0, 1);
head = destroyNode(head, 3);
changeNodeValue(head, 4, 5);
// Print list
for (int i = 0; i < listLength(head); ++i)
{
printf("%d\n", getNodeValue(head, i));
}
destroyList(head);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment