not sus at all
This commit is contained in:
1 parent
321323191a
commit
8202b00522
12 files changed
+224
-110
No files matched your search
Vendored
+1
-7
@@ -1,11 +1,5 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"algorithm": "cpp",
|
||||
"optional": "cpp",
|
||||
"system_error": "cpp",
|
||||
"type_traits": "cpp",
|
||||
"xlocmon": "cpp",
|
||||
"xtr1common": "cpp",
|
||||
"xutility": "cpp"
|
||||
"utility": "c"
|
||||
}
|
||||
}
|
||||
+23
-23
@@ -1,27 +1,27 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Array-based list structure
|
||||
// array list
|
||||
typedef struct ArrayList
|
||||
{
|
||||
int *data; // Pointer to the array
|
||||
int size; // Current number of elements
|
||||
int capacity; // Maximum capacity
|
||||
int *data; // pointer to array
|
||||
int size; // current number of elements
|
||||
int capacity; // capacity
|
||||
} ArrayList;
|
||||
|
||||
// Initializes a new array-based list
|
||||
// init a new arraylist
|
||||
ArrayList *init_array_list(int capacity)
|
||||
{
|
||||
ArrayList *list = (ArrayList *)malloc(sizeof(ArrayList));
|
||||
if (!list)
|
||||
{
|
||||
fprintf(stderr, "Memory allocation failed\n");
|
||||
fprintf(stderr, "malloc failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
list->data = (int *)malloc(capacity * sizeof(int));
|
||||
if (!list->data)
|
||||
{
|
||||
fprintf(stderr, "Memory allocation failed\n");
|
||||
fprintf(stderr, "malloc failed\n");
|
||||
free(list);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
@@ -30,20 +30,20 @@ ArrayList *init_array_list(int capacity)
|
||||
return list;
|
||||
}
|
||||
|
||||
// Resizes the array when capacity is exceeded
|
||||
// resize when capacity is exceeded
|
||||
void resize_array_list(ArrayList *list)
|
||||
{
|
||||
list->capacity *= 2;
|
||||
int *new_data = (int *)realloc(list->data, list->capacity * sizeof(int));
|
||||
if (!new_data)
|
||||
{
|
||||
fprintf(stderr, "Memory reallocation failed\n");
|
||||
fprintf(stderr, "malloc failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
list->data = new_data;
|
||||
}
|
||||
|
||||
// Adds an element to the end of the list
|
||||
// add an element to the end
|
||||
void add_to_array_list(ArrayList *list, int value)
|
||||
{
|
||||
if (list->size == list->capacity)
|
||||
@@ -53,12 +53,12 @@ void add_to_array_list(ArrayList *list, int value)
|
||||
list->data[list->size++] = value;
|
||||
}
|
||||
|
||||
// Inserts an element at a specific index
|
||||
// insert at specific index
|
||||
void insert_at(ArrayList *list, int index, int value)
|
||||
{
|
||||
if (index < 0 || index > list->size)
|
||||
{
|
||||
fprintf(stderr, "Index out of bounds\n");
|
||||
fprintf(stderr, "index out of bounds\n");
|
||||
return;
|
||||
}
|
||||
if (list->size == list->capacity)
|
||||
@@ -74,27 +74,27 @@ void insert_at(ArrayList *list, int index, int value)
|
||||
list->size++;
|
||||
}
|
||||
|
||||
// Removes the last element from the list
|
||||
// remove last element from the list
|
||||
int remove_from_array_list(ArrayList *list)
|
||||
{
|
||||
if (list->size == 0)
|
||||
{
|
||||
fprintf(stderr, "List is empty\n");
|
||||
fprintf(stderr, "list is empty\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return list->data[--list->size];
|
||||
}
|
||||
|
||||
// Removes an element from a specific index
|
||||
// remove from specific index (0 based)
|
||||
int remove_at(ArrayList *list, int index)
|
||||
{
|
||||
if (index < 0 || index >= list->size)
|
||||
{
|
||||
fprintf(stderr, "Index out of bounds\n");
|
||||
fprintf(stderr, "index out of bounds\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
int value = list->data[index];
|
||||
// Shift elements to the left
|
||||
// shift elements to the left
|
||||
for (int i = index; i < list->size - 1; i++)
|
||||
{
|
||||
list->data[i] = list->data[i + 1];
|
||||
@@ -103,29 +103,29 @@ int remove_at(ArrayList *list, int index)
|
||||
return value;
|
||||
}
|
||||
|
||||
// Gets the element at a specific index
|
||||
// get element at specific index
|
||||
int get_from_array_list(ArrayList *list, int index)
|
||||
{
|
||||
if (index < 0 || index >= list->size)
|
||||
{
|
||||
fprintf(stderr, "Index out of bounds\n");
|
||||
fprintf(stderr, "index out of bounds\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return list->data[index];
|
||||
}
|
||||
|
||||
// Sets the element at a specific index
|
||||
// set at specific index
|
||||
void set_in_array_list(ArrayList *list, int index, int value)
|
||||
{
|
||||
if (index < 0 || index >= list->size)
|
||||
{
|
||||
fprintf(stderr, "Index out of bounds\n");
|
||||
fprintf(stderr, "index out of bounds\n");
|
||||
return;
|
||||
}
|
||||
list->data[index] = value;
|
||||
}
|
||||
|
||||
// Prints the list
|
||||
// print the list
|
||||
void print_array_list(ArrayList *list)
|
||||
{
|
||||
printf("ArrayList: ");
|
||||
@@ -136,7 +136,7 @@ void print_array_list(ArrayList *list)
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// Frees the list
|
||||
// free the list
|
||||
void free_array_list(ArrayList *list)
|
||||
{
|
||||
free(list->data);
|
||||
|
||||
Whitespace-only changes.
+27
-26
@@ -1,29 +1,29 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Deque structure
|
||||
// ring deque my beloved
|
||||
typedef struct Deque
|
||||
{
|
||||
int *data; // Pointer to the array
|
||||
int front; // Index of the front element
|
||||
int back; // Index of the back element
|
||||
int size; // Current number of elements
|
||||
int capacity; // Maximum capacity
|
||||
int *data; // arr pointer
|
||||
int front; // front index
|
||||
int back; // back index
|
||||
int size; // current number of elements (please be (front - back) % capacity)
|
||||
int capacity; // maximum capacity
|
||||
} Deque;
|
||||
|
||||
// Initializes a new deque
|
||||
// make and init a new deque
|
||||
Deque *init_deque(int capacity)
|
||||
{
|
||||
Deque *deque = (Deque *)malloc(sizeof(Deque));
|
||||
if (!deque)
|
||||
{
|
||||
fprintf(stderr, "Memory allocation failed\n");
|
||||
fprintf(stderr, "malloc failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
deque->data = (int *)malloc(capacity * sizeof(int));
|
||||
if (!deque->data)
|
||||
{
|
||||
fprintf(stderr, "Memory allocation failed\n");
|
||||
fprintf(stderr, "malloc failed\n");
|
||||
free(deque);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
@@ -34,13 +34,13 @@ Deque *init_deque(int capacity)
|
||||
return deque;
|
||||
}
|
||||
|
||||
// Resizes the deque when capacity is exceeded
|
||||
// when i add too much to the deque
|
||||
void resize_deque(Deque *deque)
|
||||
{
|
||||
int *new_data = (int *)malloc(deque->capacity * 2 * sizeof(int));
|
||||
if (!new_data)
|
||||
{
|
||||
fprintf(stderr, "Memory reallocation failed\n");
|
||||
fprintf(stderr, "malloc failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ void resize_deque(Deque *deque)
|
||||
deque->capacity *= 2;
|
||||
}
|
||||
|
||||
// Adds an element to the front of the deque
|
||||
// add to the front of the deque
|
||||
void push_front(Deque *deque, int value)
|
||||
{
|
||||
if (deque->size == deque->capacity)
|
||||
@@ -68,7 +68,7 @@ void push_front(Deque *deque, int value)
|
||||
deque->size++;
|
||||
}
|
||||
|
||||
// Adds an element to the back of the deque
|
||||
// add to the back of the deque
|
||||
void push_back(Deque *deque, int value)
|
||||
{
|
||||
if (deque->size == deque->capacity)
|
||||
@@ -80,13 +80,13 @@ void push_back(Deque *deque, int value)
|
||||
deque->size++;
|
||||
}
|
||||
|
||||
// Removes an element from the front of the deque
|
||||
// take the front of the deque
|
||||
int pop_front(Deque *deque)
|
||||
{
|
||||
if (deque->size == 0)
|
||||
{
|
||||
fprintf(stderr, "Deque is empty\n");
|
||||
exit(EXIT_FAILURE);
|
||||
fprintf(stderr, "deque is empty\n");
|
||||
exit(EXIT_FAILURE); // die
|
||||
}
|
||||
int value = deque->data[deque->front];
|
||||
deque->front = (deque->front + 1) % deque->capacity;
|
||||
@@ -94,12 +94,12 @@ int pop_front(Deque *deque)
|
||||
return value;
|
||||
}
|
||||
|
||||
// Removes an element from the back of the deque
|
||||
// take the back of the deque
|
||||
int pop_back(Deque *deque)
|
||||
{
|
||||
if (deque->size == 0)
|
||||
{
|
||||
fprintf(stderr, "Deque is empty\n");
|
||||
fprintf(stderr, "deque is empty\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
deque->back = (deque->back - 1 + deque->capacity) % deque->capacity;
|
||||
@@ -108,39 +108,40 @@ int pop_back(Deque *deque)
|
||||
return value;
|
||||
}
|
||||
|
||||
// Gets the front element of the deque without removing it
|
||||
// get a look at the front
|
||||
int peek_front(Deque *deque)
|
||||
{
|
||||
if (deque->size == 0)
|
||||
{
|
||||
fprintf(stderr, "Deque is empty\n");
|
||||
fprintf(stderr, "deque is empty\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return deque->data[deque->front];
|
||||
}
|
||||
|
||||
// Gets the back element of the deque without removing it
|
||||
// get a look at the back
|
||||
int peek_back(Deque *deque)
|
||||
{
|
||||
if (deque->size == 0)
|
||||
{
|
||||
fprintf(stderr, "Deque is empty\n");
|
||||
fprintf(stderr, "deque is empty\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
int last_index = (deque->back - 1 + deque->capacity) % deque->capacity;
|
||||
return deque->data[last_index];
|
||||
}
|
||||
|
||||
// Checks if the deque is empty
|
||||
// is the deque empty?
|
||||
// pwease use when trying to pop
|
||||
int is_empty(Deque *deque)
|
||||
{
|
||||
return deque->size == 0;
|
||||
}
|
||||
|
||||
// Prints the deque
|
||||
// print deque
|
||||
void print_deque(Deque *deque)
|
||||
{
|
||||
printf("Deque: ");
|
||||
printf("deque: ");
|
||||
for (int i = 0; i < deque->size; i++)
|
||||
{
|
||||
printf("%d ", deque->data[(deque->front + i) % deque->capacity]);
|
||||
@@ -148,7 +149,7 @@ void print_deque(Deque *deque)
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// Frees the deque
|
||||
// free deque
|
||||
void free_deque(Deque *deque)
|
||||
{
|
||||
free(deque->data);
|
||||
|
||||
Whitespace-only changes.
+20
-20
@@ -2,23 +2,23 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// Node structure for the doubly linked list
|
||||
// node for the doubly linked list
|
||||
typedef struct Node
|
||||
{
|
||||
uintptr_t data; // Generic data pointer or integer value
|
||||
uintptr_t data;
|
||||
struct Node *prev;
|
||||
struct Node *next;
|
||||
} Node;
|
||||
|
||||
// Doubly linked list structure
|
||||
// doubly linked list structure
|
||||
typedef struct DoublyLinkedList
|
||||
{
|
||||
int size; // Number of elements in the list
|
||||
Node *head; // Pointer to the first node
|
||||
Node *tail; // Pointer to the last node
|
||||
int size; // number of elements in the list
|
||||
Node *head; // pointer to first node
|
||||
Node *tail; // pointer to last node
|
||||
} DoublyLinkedList;
|
||||
|
||||
// Initializes a new doubly linked list
|
||||
// init a new doubly linked list
|
||||
DoublyLinkedList *init_list()
|
||||
{
|
||||
DoublyLinkedList *list = (DoublyLinkedList *)malloc(sizeof(DoublyLinkedList));
|
||||
@@ -33,7 +33,7 @@ DoublyLinkedList *init_list()
|
||||
return list;
|
||||
}
|
||||
|
||||
// Creates a new node
|
||||
// create a new node
|
||||
Node *create_node(uintptr_t data)
|
||||
{
|
||||
Node *new_node = (Node *)malloc(sizeof(Node));
|
||||
@@ -48,7 +48,7 @@ Node *create_node(uintptr_t data)
|
||||
return new_node;
|
||||
}
|
||||
|
||||
// Adds a value to the front of the list
|
||||
// add to the front of the list
|
||||
void push_front(DoublyLinkedList *list, uintptr_t data)
|
||||
{
|
||||
Node *new_node = create_node(data);
|
||||
@@ -66,7 +66,7 @@ void push_front(DoublyLinkedList *list, uintptr_t data)
|
||||
list->size++;
|
||||
}
|
||||
|
||||
// Adds a value to the end of the list
|
||||
// adds to the end of the list
|
||||
void push_back(DoublyLinkedList *list, uintptr_t data)
|
||||
{
|
||||
Node *new_node = create_node(data);
|
||||
@@ -84,7 +84,7 @@ void push_back(DoublyLinkedList *list, uintptr_t data)
|
||||
list->size++;
|
||||
}
|
||||
|
||||
// Inserts a value at a specific index
|
||||
// insert at index
|
||||
void insert_at(DoublyLinkedList *list, uintptr_t data, int index)
|
||||
{
|
||||
if (index < 0 || index > list->size)
|
||||
@@ -117,7 +117,7 @@ void insert_at(DoublyLinkedList *list, uintptr_t data, int index)
|
||||
list->size++;
|
||||
}
|
||||
|
||||
// Removes a value from the front of the list
|
||||
// remove from the front of the list
|
||||
uintptr_t pop_front(DoublyLinkedList *list)
|
||||
{
|
||||
if (list->head == NULL)
|
||||
@@ -141,7 +141,7 @@ uintptr_t pop_front(DoublyLinkedList *list)
|
||||
return data;
|
||||
}
|
||||
|
||||
// Removes a value from the end of the list
|
||||
// remove from the end of the list
|
||||
uintptr_t pop_back(DoublyLinkedList *list)
|
||||
{
|
||||
if (list->tail == NULL)
|
||||
@@ -165,7 +165,7 @@ uintptr_t pop_back(DoublyLinkedList *list)
|
||||
return data;
|
||||
}
|
||||
|
||||
// Removes a value at a specific index
|
||||
// remove at an index
|
||||
uintptr_t remove_at(DoublyLinkedList *list, int index)
|
||||
{
|
||||
if (index < 0 || index >= list->size)
|
||||
@@ -196,12 +196,12 @@ uintptr_t remove_at(DoublyLinkedList *list, int index)
|
||||
return data;
|
||||
}
|
||||
|
||||
// Gets a value at a specific index
|
||||
// get at index
|
||||
int get(DoublyLinkedList *list, int index, uintptr_t *data)
|
||||
{
|
||||
if (index < 0 || index >= list->size)
|
||||
{
|
||||
return -1; // Index out of bounds
|
||||
return -1; // out of bounds
|
||||
}
|
||||
|
||||
Node *current = list->head;
|
||||
@@ -210,10 +210,10 @@ int get(DoublyLinkedList *list, int index, uintptr_t *data)
|
||||
current = current->next;
|
||||
}
|
||||
*data = current->data;
|
||||
return 0; // Success
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Prints the list from front to back
|
||||
// print the list front to back
|
||||
void print_list(DoublyLinkedList *list)
|
||||
{
|
||||
Node *current = list->head;
|
||||
@@ -226,7 +226,7 @@ void print_list(DoublyLinkedList *list)
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// Prints the list from back to front
|
||||
// print the list back to front
|
||||
void print_reverse(DoublyLinkedList *list)
|
||||
{
|
||||
Node *current = list->tail;
|
||||
@@ -239,7 +239,7 @@ void print_reverse(DoublyLinkedList *list)
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// Frees the entire list
|
||||
// free the entire list
|
||||
void free_list(DoublyLinkedList *list)
|
||||
{
|
||||
Node *current = list->head;
|
||||
|
||||
Whitespace-only changes.
+1
-1
@@ -3,7 +3,7 @@
|
||||
|
||||
#define MAX_SIZE 100
|
||||
|
||||
// Structure to represent a queue
|
||||
// Structure to represent a queue (i love when this queue looks like the deque)
|
||||
typedef struct
|
||||
{
|
||||
int items[MAX_SIZE];
|
||||
|
||||
+33
-33
@@ -2,28 +2,28 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// Node structure for singly linked list
|
||||
// linked list Node. sum internal stuff
|
||||
typedef struct Node
|
||||
{
|
||||
uintptr_t value; // Generic data pointer or integer value
|
||||
struct Node *next;
|
||||
uintptr_t value; // pointer++
|
||||
struct Node *next; // next in the list
|
||||
} Node;
|
||||
|
||||
// Singly linked list structure
|
||||
// linked list
|
||||
typedef struct SinglyLinkedList
|
||||
{
|
||||
int size; // Number of elements in the list
|
||||
Node *head; // Pointer to the first node
|
||||
Node *tail; // Pointer to the last node
|
||||
int size; // number of elements
|
||||
Node *head; // pointer to first node
|
||||
Node *tail; // pointer to last node
|
||||
} SinglyLinkedList;
|
||||
|
||||
// Initializes a new singly linked list
|
||||
// init singly linked list
|
||||
SinglyLinkedList *create_list()
|
||||
{
|
||||
SinglyLinkedList *list = (SinglyLinkedList *)malloc(sizeof(SinglyLinkedList));
|
||||
if (!list)
|
||||
{
|
||||
fprintf(stderr, "Memory allocation failed\n");
|
||||
fprintf(stderr, "malloc failed\n");
|
||||
return NULL;
|
||||
}
|
||||
list->size = 0;
|
||||
@@ -32,13 +32,13 @@ SinglyLinkedList *create_list()
|
||||
return list;
|
||||
}
|
||||
|
||||
// Creates a new node
|
||||
// make a new node
|
||||
Node *create_node(uintptr_t value)
|
||||
{
|
||||
Node *new_node = (Node *)malloc(sizeof(Node));
|
||||
if (!new_node)
|
||||
{
|
||||
fprintf(stderr, "Memory allocation failed\n");
|
||||
fprintf(stderr, "malloc failed\n");
|
||||
return NULL;
|
||||
}
|
||||
new_node->value = value;
|
||||
@@ -46,13 +46,13 @@ Node *create_node(uintptr_t value)
|
||||
return new_node;
|
||||
}
|
||||
|
||||
// Adds a value to the end of the list
|
||||
// add value to the end of the list
|
||||
int append(SinglyLinkedList *list, uintptr_t value)
|
||||
{
|
||||
Node *new_node = create_node(value);
|
||||
if (!new_node)
|
||||
{
|
||||
return -1; // Memory allocation failed
|
||||
return -1; // malloc failed
|
||||
}
|
||||
if (list->tail == NULL)
|
||||
{
|
||||
@@ -65,16 +65,16 @@ int append(SinglyLinkedList *list, uintptr_t value)
|
||||
list->tail = new_node;
|
||||
}
|
||||
list->size++;
|
||||
return 0; // Success
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Adds a value to the front of the list
|
||||
// add to the front of the list
|
||||
int prepend(SinglyLinkedList *list, uintptr_t value)
|
||||
{
|
||||
Node *new_node = create_node(value);
|
||||
if (!new_node)
|
||||
{
|
||||
return -1; // Memory allocation failed
|
||||
return -1; // malloc failed
|
||||
}
|
||||
if (list->head == NULL)
|
||||
{
|
||||
@@ -87,15 +87,15 @@ int prepend(SinglyLinkedList *list, uintptr_t value)
|
||||
list->head = new_node;
|
||||
}
|
||||
list->size++;
|
||||
return 0; // Success
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Inserts a value at a specific index
|
||||
// insert at index
|
||||
int insert_at(SinglyLinkedList *list, uintptr_t value, int index)
|
||||
{
|
||||
if (index < 0 || index > list->size)
|
||||
{
|
||||
return -1; // Index out of bounds
|
||||
return -1; // out of bounds
|
||||
}
|
||||
if (index == 0)
|
||||
{
|
||||
@@ -109,7 +109,7 @@ int insert_at(SinglyLinkedList *list, uintptr_t value, int index)
|
||||
Node *new_node = create_node(value);
|
||||
if (!new_node)
|
||||
{
|
||||
return -1; // Memory allocation failed
|
||||
return -1; // malloc failed
|
||||
}
|
||||
|
||||
Node *current = list->head;
|
||||
@@ -120,16 +120,16 @@ int insert_at(SinglyLinkedList *list, uintptr_t value, int index)
|
||||
new_node->next = current->next;
|
||||
current->next = new_node;
|
||||
list->size++;
|
||||
return 0; // Success
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Removes a value from the end of the list
|
||||
// remove from the end of the list
|
||||
uintptr_t remove_last(SinglyLinkedList *list)
|
||||
{
|
||||
if (list->tail == NULL)
|
||||
{
|
||||
fprintf(stderr, "List is empty\n");
|
||||
return (uintptr_t)-1; // Indicate error
|
||||
fprintf(stderr, "list is empty\n");
|
||||
return (uintptr_t)-1; // errored
|
||||
}
|
||||
if (list->head == list->tail)
|
||||
{
|
||||
@@ -150,12 +150,12 @@ uintptr_t remove_last(SinglyLinkedList *list)
|
||||
return value;
|
||||
}
|
||||
|
||||
// Removes a value at a specific index
|
||||
// remove at an index
|
||||
uintptr_t remove_at(SinglyLinkedList *list, int index)
|
||||
{
|
||||
if (index < 0 || index >= list->size)
|
||||
{
|
||||
return (uintptr_t)-1; // Index out of bounds
|
||||
return (uintptr_t)-1; // out of bounds
|
||||
}
|
||||
if (index == 0)
|
||||
{
|
||||
@@ -180,12 +180,12 @@ uintptr_t remove_at(SinglyLinkedList *list, int index)
|
||||
return value;
|
||||
}
|
||||
|
||||
// Gets a value at a specific index
|
||||
// get at index
|
||||
int get(SinglyLinkedList *list, int index, uintptr_t *value)
|
||||
{
|
||||
if (index < 0 || index >= list->size)
|
||||
{
|
||||
return -1; // Index out of bounds
|
||||
return -1; // out of bounds
|
||||
}
|
||||
|
||||
Node *current = list->head;
|
||||
@@ -197,13 +197,13 @@ int get(SinglyLinkedList *list, int index, uintptr_t *value)
|
||||
return 0; // Success
|
||||
}
|
||||
|
||||
// Removes a value from the front of the list
|
||||
// remove from the front of the list
|
||||
uintptr_t remove_first(SinglyLinkedList *list)
|
||||
{
|
||||
if (list->head == NULL)
|
||||
{
|
||||
fprintf(stderr, "List is empty\n");
|
||||
return (uintptr_t)-1; // Indicate error
|
||||
fprintf(stderr, "list is empty\n");
|
||||
return (uintptr_t)-1; // errored
|
||||
}
|
||||
Node *temp = list->head;
|
||||
uintptr_t value = temp->value;
|
||||
@@ -217,7 +217,7 @@ uintptr_t remove_first(SinglyLinkedList *list)
|
||||
return value;
|
||||
}
|
||||
|
||||
// Prints the list
|
||||
// print the list
|
||||
void print_list(const SinglyLinkedList *list)
|
||||
{
|
||||
Node *current = list->head;
|
||||
@@ -230,7 +230,7 @@ void print_list(const SinglyLinkedList *list)
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// Frees the entire list
|
||||
// free the whole list
|
||||
void free_list(SinglyLinkedList *list)
|
||||
{
|
||||
Node *current = list->head;
|
||||
|
||||
Whitespace-only changes.
Whitespace-only changes.
+119
@@ -0,0 +1,119 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
/*
|
||||
Basic operations are often defined
|
||||
1 init(): create an empty list
|
||||
2 isEmpty(): check if the list is empty
|
||||
3 insert(): add new item in a list
|
||||
4 remove(): remove an item from a list
|
||||
Some other possible operations can be used:
|
||||
1 length(): return the length of a list
|
||||
2 search(): search a specific element in a list
|
||||
3 display(): display a list
|
||||
4 sort(): sort all items in a list
|
||||
*/
|
||||
|
||||
typedef struct Node
|
||||
{
|
||||
uintptr_t ptr;
|
||||
node *next;
|
||||
node *prev;
|
||||
} node;
|
||||
|
||||
void pair2(node *a, node *b)
|
||||
{
|
||||
if (a)
|
||||
a->next = b;
|
||||
if (b)
|
||||
b->prev = a;
|
||||
}
|
||||
|
||||
void pair3(node *a, node *b, node *c)
|
||||
{
|
||||
if (b)
|
||||
{
|
||||
pair2(a, b);
|
||||
pair2(b, c);
|
||||
}
|
||||
}
|
||||
|
||||
uintptr_t node_into_inner(node *n)
|
||||
{
|
||||
uintptr_t prt = n->ptr;
|
||||
free(n);
|
||||
return prt;
|
||||
};
|
||||
|
||||
typedef struct DoublyLinkedList
|
||||
{
|
||||
int size;
|
||||
node *head;
|
||||
node *tail;
|
||||
} dll;
|
||||
|
||||
dll *init()
|
||||
{
|
||||
dll *l = (dll *)malloc(sizeof(dll));
|
||||
if (!l)
|
||||
return NULL;
|
||||
l->size = 0;
|
||||
l->head = NULL;
|
||||
l->tail = NULL;
|
||||
return l;
|
||||
}
|
||||
node *new(uintptr_t ptr)
|
||||
{
|
||||
node *n = (node *)malloc(sizeof(node));
|
||||
if (!n)
|
||||
return NULL;
|
||||
|
||||
n->ptr = ptr;
|
||||
n->prev = NULL;
|
||||
n->next = NULL;
|
||||
return n;
|
||||
}
|
||||
|
||||
void push_front(dll *list, node *node_)
|
||||
{
|
||||
// border case
|
||||
if (list->head == NULL)
|
||||
{
|
||||
list->head = node_;
|
||||
list->tail = node_;
|
||||
// teach never tells you what to do when this happens;
|
||||
}
|
||||
else
|
||||
{
|
||||
node *second = list->head;
|
||||
pair2(node_, second);
|
||||
list->head = node_;
|
||||
};
|
||||
list->size++;
|
||||
}
|
||||
void push_back(dll *list, node *node_)
|
||||
{
|
||||
// border case
|
||||
if (list->tail == NULL)
|
||||
{
|
||||
list->head = node_;
|
||||
list->tail = node_;
|
||||
// teach never tells you what to do when this happens;
|
||||
}
|
||||
else
|
||||
{
|
||||
node *secondlast = list->head;
|
||||
pair2(secondlast, node_);
|
||||
list->tail = node_;
|
||||
};
|
||||
list->size++;
|
||||
}
|
||||
|
||||
// void remove(dll* l, ){
|
||||
|
||||
// }
|
||||
|
||||
int main(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user