holy ai spree
This commit is contained in:
1 parent
abbb01049d
commit
b9b768fe45
15 files changed
+2581
No files matched your search
@@ -53,6 +53,27 @@ void add_to_array_list(ArrayList *list, int value)
|
||||
list->data[list->size++] = value;
|
||||
}
|
||||
|
||||
// Inserts an element at a specific index
|
||||
void insert_at(ArrayList *list, int index, int value)
|
||||
{
|
||||
if (index < 0 || index > list->size)
|
||||
{
|
||||
fprintf(stderr, "Index out of bounds\n");
|
||||
return;
|
||||
}
|
||||
if (list->size == list->capacity)
|
||||
{
|
||||
resize_array_list(list);
|
||||
}
|
||||
// Shift elements to the right
|
||||
for (int i = list->size; i > index; i--)
|
||||
{
|
||||
list->data[i] = list->data[i - 1];
|
||||
}
|
||||
list->data[index] = value;
|
||||
list->size++;
|
||||
}
|
||||
|
||||
// Removes the last element from the list
|
||||
int remove_from_array_list(ArrayList *list)
|
||||
{
|
||||
@@ -64,6 +85,24 @@ int remove_from_array_list(ArrayList *list)
|
||||
return list->data[--list->size];
|
||||
}
|
||||
|
||||
// Removes an element from a specific index
|
||||
int remove_at(ArrayList *list, int index)
|
||||
{
|
||||
if (index < 0 || index >= list->size)
|
||||
{
|
||||
fprintf(stderr, "Index out of bounds\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
int value = list->data[index];
|
||||
// Shift elements to the left
|
||||
for (int i = index; i < list->size - 1; i++)
|
||||
{
|
||||
list->data[i] = list->data[i + 1];
|
||||
}
|
||||
list->size--;
|
||||
return value;
|
||||
}
|
||||
|
||||
// Gets the element at a specific index
|
||||
int get_from_array_list(ArrayList *list, int index)
|
||||
{
|
||||
@@ -75,6 +114,17 @@ int get_from_array_list(ArrayList *list, int index)
|
||||
return list->data[index];
|
||||
}
|
||||
|
||||
// Sets the element at a 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");
|
||||
return;
|
||||
}
|
||||
list->data[index] = value;
|
||||
}
|
||||
|
||||
// Prints the list
|
||||
void print_array_list(ArrayList *list)
|
||||
{
|
||||
@@ -108,6 +158,15 @@ int main()
|
||||
|
||||
printf("Element at index 1: %d\n", get_from_array_list(list, 1));
|
||||
|
||||
insert_at(list, 1, 25);
|
||||
print_array_list(list);
|
||||
|
||||
printf("Removed from index 1: %d\n", remove_at(list, 1));
|
||||
print_array_list(list);
|
||||
|
||||
set_in_array_list(list, 0, 15);
|
||||
print_array_list(list);
|
||||
|
||||
free_array_list(list);
|
||||
return 0;
|
||||
}
|
||||
@@ -108,6 +108,35 @@ int pop_back(Deque *deque)
|
||||
return value;
|
||||
}
|
||||
|
||||
// Gets the front element of the deque without removing it
|
||||
int peek_front(Deque *deque)
|
||||
{
|
||||
if (deque->size == 0)
|
||||
{
|
||||
fprintf(stderr, "Deque is empty\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return deque->data[deque->front];
|
||||
}
|
||||
|
||||
// Gets the back element of the deque without removing it
|
||||
int peek_back(Deque *deque)
|
||||
{
|
||||
if (deque->size == 0)
|
||||
{
|
||||
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
|
||||
int is_empty(Deque *deque)
|
||||
{
|
||||
return deque->size == 0;
|
||||
}
|
||||
|
||||
// Prints the deque
|
||||
void print_deque(Deque *deque)
|
||||
{
|
||||
|
||||
@@ -84,6 +84,39 @@ void push_back(DoublyLinkedList *list, uintptr_t data)
|
||||
list->size++;
|
||||
}
|
||||
|
||||
// Inserts a value at a specific index
|
||||
void insert_at(DoublyLinkedList *list, uintptr_t data, int index)
|
||||
{
|
||||
if (index < 0 || index > list->size)
|
||||
{
|
||||
fprintf(stderr, "Index out of bounds\n");
|
||||
return;
|
||||
}
|
||||
if (index == 0)
|
||||
{
|
||||
push_front(list, data);
|
||||
return;
|
||||
}
|
||||
if (index == list->size)
|
||||
{
|
||||
push_back(list, data);
|
||||
return;
|
||||
}
|
||||
|
||||
Node *current = list->head;
|
||||
for (int i = 0; i < index; i++)
|
||||
{
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
Node *new_node = create_node(data);
|
||||
new_node->next = current;
|
||||
new_node->prev = current->prev;
|
||||
current->prev->next = new_node;
|
||||
current->prev = new_node;
|
||||
list->size++;
|
||||
}
|
||||
|
||||
// Removes a value from the front of the list
|
||||
uintptr_t pop_front(DoublyLinkedList *list)
|
||||
{
|
||||
@@ -132,6 +165,54 @@ uintptr_t pop_back(DoublyLinkedList *list)
|
||||
return data;
|
||||
}
|
||||
|
||||
// Removes a value at a specific index
|
||||
uintptr_t remove_at(DoublyLinkedList *list, int index)
|
||||
{
|
||||
if (index < 0 || index >= list->size)
|
||||
{
|
||||
fprintf(stderr, "Index out of bounds\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (index == 0)
|
||||
{
|
||||
return pop_front(list);
|
||||
}
|
||||
if (index == list->size - 1)
|
||||
{
|
||||
return pop_back(list);
|
||||
}
|
||||
|
||||
Node *current = list->head;
|
||||
for (int i = 0; i < index; i++)
|
||||
{
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
uintptr_t data = current->data;
|
||||
current->prev->next = current->next;
|
||||
current->next->prev = current->prev;
|
||||
free(current);
|
||||
list->size--;
|
||||
return data;
|
||||
}
|
||||
|
||||
// Gets a value at a specific index
|
||||
int get(DoublyLinkedList *list, int index, uintptr_t *data)
|
||||
{
|
||||
if (index < 0 || index >= list->size)
|
||||
{
|
||||
return -1; // Index out of bounds
|
||||
}
|
||||
|
||||
Node *current = list->head;
|
||||
for (int i = 0; i < index; i++)
|
||||
{
|
||||
current = current->next;
|
||||
}
|
||||
*data = current->data;
|
||||
return 0; // Success
|
||||
}
|
||||
|
||||
// Prints the list from front to back
|
||||
void print_list(DoublyLinkedList *list)
|
||||
{
|
||||
@@ -145,6 +226,19 @@ void print_list(DoublyLinkedList *list)
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// Prints the list from back to front
|
||||
void print_reverse(DoublyLinkedList *list)
|
||||
{
|
||||
Node *current = list->tail;
|
||||
printf("List (reversed): ");
|
||||
while (current != NULL)
|
||||
{
|
||||
printf("%lu ", current->data);
|
||||
current = current->prev;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// Frees the entire list
|
||||
void free_list(DoublyLinkedList *list)
|
||||
{
|
||||
@@ -173,6 +267,24 @@ int main()
|
||||
printf("Popped from back: %lu\n", pop_back(list));
|
||||
print_list(list);
|
||||
|
||||
insert_at(list, 25, 1);
|
||||
print_list(list);
|
||||
|
||||
remove_at(list, 1);
|
||||
print_list(list);
|
||||
|
||||
uintptr_t data;
|
||||
if (get(list, 0, &data) == 0)
|
||||
{
|
||||
printf("Value at index 0: %lu\n", data);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Failed to get value at index 0\n");
|
||||
}
|
||||
|
||||
print_reverse(list);
|
||||
|
||||
free_list(list);
|
||||
return 0;
|
||||
}
|
||||
+123
@@ -0,0 +1,123 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define MAX_SIZE 100
|
||||
|
||||
// Structure to represent a queue
|
||||
typedef struct
|
||||
{
|
||||
int items[MAX_SIZE];
|
||||
int front;
|
||||
int rear;
|
||||
} Queue;
|
||||
|
||||
// Function to create a new queue
|
||||
Queue *createQueue()
|
||||
{
|
||||
Queue *q = (Queue *)malloc(sizeof(Queue));
|
||||
q->front = -1;
|
||||
q->rear = -1;
|
||||
return q;
|
||||
}
|
||||
|
||||
// Function to check if the queue is full
|
||||
int isFull(Queue *q)
|
||||
{
|
||||
return q->rear == MAX_SIZE - 1;
|
||||
}
|
||||
|
||||
// Function to check if the queue is empty
|
||||
int isEmpty(Queue *q)
|
||||
{
|
||||
return q->front == -1;
|
||||
}
|
||||
|
||||
// Function to add an item to the queue (enqueue)
|
||||
void enqueue(Queue *q, int value)
|
||||
{
|
||||
if (isFull(q))
|
||||
{
|
||||
printf("Queue is full\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (q->front == -1)
|
||||
{
|
||||
q->front = 0;
|
||||
}
|
||||
q->rear++;
|
||||
q->items[q->rear] = value;
|
||||
}
|
||||
}
|
||||
|
||||
// Function to remove an item from the queue (dequeue)
|
||||
int dequeue(Queue *q)
|
||||
{
|
||||
int item;
|
||||
if (isEmpty(q))
|
||||
{
|
||||
printf("Queue is empty\n");
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
item = q->items[q->front];
|
||||
q->front++;
|
||||
if (q->front > q->rear)
|
||||
{
|
||||
// Reset queue when it becomes empty
|
||||
q->front = q->rear = -1;
|
||||
}
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
// Function to get the front item of the queue without removing it
|
||||
int peek(Queue *q)
|
||||
{
|
||||
if (isEmpty(q))
|
||||
{
|
||||
printf("Queue is empty\n");
|
||||
return -1;
|
||||
}
|
||||
return q->items[q->front];
|
||||
}
|
||||
|
||||
// Function to print the queue
|
||||
void printQueue(Queue *q)
|
||||
{
|
||||
if (isEmpty(q))
|
||||
{
|
||||
printf("Queue is empty\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Queue: ");
|
||||
for (int i = q->front; i <= q->rear; i++)
|
||||
{
|
||||
printf("%d ", q->items[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
Queue *q = createQueue();
|
||||
|
||||
enqueue(q, 10);
|
||||
enqueue(q, 20);
|
||||
enqueue(q, 30);
|
||||
enqueue(q, 40);
|
||||
|
||||
printQueue(q);
|
||||
|
||||
printf("Dequeued item: %d\n", dequeue(q));
|
||||
printQueue(q);
|
||||
|
||||
printf("Front item: %d\n", peek(q));
|
||||
printQueue(q);
|
||||
|
||||
free(q);
|
||||
return 0;
|
||||
}
|
||||
@@ -68,6 +68,135 @@ int append(SinglyLinkedList *list, uintptr_t value)
|
||||
return 0; // Success
|
||||
}
|
||||
|
||||
// Adds a value 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
|
||||
}
|
||||
if (list->head == NULL)
|
||||
{
|
||||
list->head = new_node;
|
||||
list->tail = new_node;
|
||||
}
|
||||
else
|
||||
{
|
||||
new_node->next = list->head;
|
||||
list->head = new_node;
|
||||
}
|
||||
list->size++;
|
||||
return 0; // Success
|
||||
}
|
||||
|
||||
// Inserts a value at a specific index
|
||||
int insert_at(SinglyLinkedList *list, uintptr_t value, int index)
|
||||
{
|
||||
if (index < 0 || index > list->size)
|
||||
{
|
||||
return -1; // Index out of bounds
|
||||
}
|
||||
if (index == 0)
|
||||
{
|
||||
return prepend(list, value);
|
||||
}
|
||||
if (index == list->size)
|
||||
{
|
||||
return append(list, value);
|
||||
}
|
||||
|
||||
Node *new_node = create_node(value);
|
||||
if (!new_node)
|
||||
{
|
||||
return -1; // Memory allocation failed
|
||||
}
|
||||
|
||||
Node *current = list->head;
|
||||
for (int i = 0; i < index - 1; i++)
|
||||
{
|
||||
current = current->next;
|
||||
}
|
||||
new_node->next = current->next;
|
||||
current->next = new_node;
|
||||
list->size++;
|
||||
return 0; // Success
|
||||
}
|
||||
|
||||
// Removes a value 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
|
||||
}
|
||||
if (list->head == list->tail)
|
||||
{
|
||||
return remove_first(list);
|
||||
}
|
||||
|
||||
Node *current = list->head;
|
||||
while (current->next != list->tail)
|
||||
{
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
uintptr_t value = list->tail->value;
|
||||
free(list->tail);
|
||||
list->tail = current;
|
||||
list->tail->next = NULL;
|
||||
list->size--;
|
||||
return value;
|
||||
}
|
||||
|
||||
// Removes a value at a specific index
|
||||
uintptr_t remove_at(SinglyLinkedList *list, int index)
|
||||
{
|
||||
if (index < 0 || index >= list->size)
|
||||
{
|
||||
return (uintptr_t)-1; // Index out of bounds
|
||||
}
|
||||
if (index == 0)
|
||||
{
|
||||
return remove_first(list);
|
||||
}
|
||||
if (index == list->size - 1)
|
||||
{
|
||||
return remove_last(list);
|
||||
}
|
||||
|
||||
Node *current = list->head;
|
||||
for (int i = 0; i < index - 1; i++)
|
||||
{
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
Node *temp = current->next;
|
||||
uintptr_t value = temp->value;
|
||||
current->next = temp->next;
|
||||
free(temp);
|
||||
list->size--;
|
||||
return value;
|
||||
}
|
||||
|
||||
// Gets a value at a specific index
|
||||
int get(SinglyLinkedList *list, int index, uintptr_t *value)
|
||||
{
|
||||
if (index < 0 || index >= list->size)
|
||||
{
|
||||
return -1; // Index out of bounds
|
||||
}
|
||||
|
||||
Node *current = list->head;
|
||||
for (int i = 0; i < index; i++)
|
||||
{
|
||||
current = current->next;
|
||||
}
|
||||
*value = current->value;
|
||||
return 0; // Success
|
||||
}
|
||||
|
||||
// Removes a value from the front of the list
|
||||
uintptr_t remove_first(SinglyLinkedList *list)
|
||||
{
|
||||
@@ -130,6 +259,28 @@ int main()
|
||||
printf("Removed: %lu\n", remove_first(list));
|
||||
print_list(list);
|
||||
|
||||
prepend(list, 5);
|
||||
print_list(list);
|
||||
|
||||
insert_at(list, 25, 2);
|
||||
print_list(list);
|
||||
|
||||
printf("Removed last: %lu\n", remove_last(list));
|
||||
print_list(list);
|
||||
|
||||
printf("Removed at index 1: %lu\n", remove_at(list, 1));
|
||||
print_list(list);
|
||||
|
||||
uintptr_t value;
|
||||
if (get(list, 1, &value) == 0)
|
||||
{
|
||||
printf("Value at index 1: %lu\n", value);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Index out of bounds\n");
|
||||
}
|
||||
|
||||
free_list(list);
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user