chatgpt model impl
now it works
This commit is contained in:
1 parent
75998e4752
commit
fd814b8572
3 files changed
+438
No files matched your search
@@ -0,0 +1,113 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Array-based list structure
|
||||
typedef struct ArrayList
|
||||
{
|
||||
int *data; // Pointer to the array
|
||||
int size; // Current number of elements
|
||||
int capacity; // Maximum capacity
|
||||
} ArrayList;
|
||||
|
||||
// Initializes a new array-based list
|
||||
ArrayList *init_array_list(int capacity)
|
||||
{
|
||||
ArrayList *list = (ArrayList *)malloc(sizeof(ArrayList));
|
||||
if (!list)
|
||||
{
|
||||
fprintf(stderr, "Memory allocation failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
list->data = (int *)malloc(capacity * sizeof(int));
|
||||
if (!list->data)
|
||||
{
|
||||
fprintf(stderr, "Memory allocation failed\n");
|
||||
free(list);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
list->size = 0;
|
||||
list->capacity = capacity;
|
||||
return list;
|
||||
}
|
||||
|
||||
// Resizes the array 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");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
list->data = new_data;
|
||||
}
|
||||
|
||||
// Adds an element to the end of the list
|
||||
void add_to_array_list(ArrayList *list, int value)
|
||||
{
|
||||
if (list->size == list->capacity)
|
||||
{
|
||||
resize_array_list(list);
|
||||
}
|
||||
list->data[list->size++] = value;
|
||||
}
|
||||
|
||||
// Removes the last element from the list
|
||||
int remove_from_array_list(ArrayList *list)
|
||||
{
|
||||
if (list->size == 0)
|
||||
{
|
||||
fprintf(stderr, "List is empty\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return list->data[--list->size];
|
||||
}
|
||||
|
||||
// Gets the element at a specific index
|
||||
int get_from_array_list(ArrayList *list, int index)
|
||||
{
|
||||
if (index < 0 || index >= list->size)
|
||||
{
|
||||
fprintf(stderr, "Index out of bounds\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return list->data[index];
|
||||
}
|
||||
|
||||
// Prints the list
|
||||
void print_array_list(ArrayList *list)
|
||||
{
|
||||
printf("ArrayList: ");
|
||||
for (int i = 0; i < list->size; i++)
|
||||
{
|
||||
printf("%d ", list->data[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// Frees the list
|
||||
void free_array_list(ArrayList *list)
|
||||
{
|
||||
free(list->data);
|
||||
free(list);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ArrayList *list = init_array_list(4);
|
||||
|
||||
add_to_array_list(list, 10);
|
||||
add_to_array_list(list, 20);
|
||||
add_to_array_list(list, 30);
|
||||
add_to_array_list(list, 40);
|
||||
print_array_list(list);
|
||||
|
||||
printf("Removed: %d\n", remove_from_array_list(list));
|
||||
print_array_list(list);
|
||||
|
||||
printf("Element at index 1: %d\n", get_from_array_list(list, 1));
|
||||
|
||||
free_array_list(list);
|
||||
return 0;
|
||||
}
|
||||
+147
@@ -0,0 +1,147 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Deque structure
|
||||
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
|
||||
} Deque;
|
||||
|
||||
// Initializes a new deque
|
||||
Deque *init_deque(int capacity)
|
||||
{
|
||||
Deque *deque = (Deque *)malloc(sizeof(Deque));
|
||||
if (!deque)
|
||||
{
|
||||
fprintf(stderr, "Memory allocation failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
deque->data = (int *)malloc(capacity * sizeof(int));
|
||||
if (!deque->data)
|
||||
{
|
||||
fprintf(stderr, "Memory allocation failed\n");
|
||||
free(deque);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
deque->front = 0;
|
||||
deque->back = 0;
|
||||
deque->size = 0;
|
||||
deque->capacity = capacity;
|
||||
return deque;
|
||||
}
|
||||
|
||||
// Resizes the deque when capacity is exceeded
|
||||
void resize_deque(Deque *deque)
|
||||
{
|
||||
int *new_data = (int *)malloc(deque->capacity * 2 * sizeof(int));
|
||||
if (!new_data)
|
||||
{
|
||||
fprintf(stderr, "Memory reallocation failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
for (int i = 0; i < deque->size; i++)
|
||||
{
|
||||
new_data[i] = deque->data[(deque->front + i) % deque->capacity];
|
||||
}
|
||||
|
||||
free(deque->data);
|
||||
deque->data = new_data;
|
||||
deque->front = 0;
|
||||
deque->back = deque->size;
|
||||
deque->capacity *= 2;
|
||||
}
|
||||
|
||||
// Adds an element to the front of the deque
|
||||
void push_front(Deque *deque, int value)
|
||||
{
|
||||
if (deque->size == deque->capacity)
|
||||
{
|
||||
resize_deque(deque);
|
||||
}
|
||||
deque->front = (deque->front - 1 + deque->capacity) % deque->capacity;
|
||||
deque->data[deque->front] = value;
|
||||
deque->size++;
|
||||
}
|
||||
|
||||
// Adds an element to the back of the deque
|
||||
void push_back(Deque *deque, int value)
|
||||
{
|
||||
if (deque->size == deque->capacity)
|
||||
{
|
||||
resize_deque(deque);
|
||||
}
|
||||
deque->data[deque->back] = value;
|
||||
deque->back = (deque->back + 1) % deque->capacity;
|
||||
deque->size++;
|
||||
}
|
||||
|
||||
// Removes an element from the front of the deque
|
||||
int pop_front(Deque *deque)
|
||||
{
|
||||
if (deque->size == 0)
|
||||
{
|
||||
fprintf(stderr, "Deque is empty\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
int value = deque->data[deque->front];
|
||||
deque->front = (deque->front + 1) % deque->capacity;
|
||||
deque->size--;
|
||||
return value;
|
||||
}
|
||||
|
||||
// Removes an element from the back of the deque
|
||||
int pop_back(Deque *deque)
|
||||
{
|
||||
if (deque->size == 0)
|
||||
{
|
||||
fprintf(stderr, "Deque is empty\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
deque->back = (deque->back - 1 + deque->capacity) % deque->capacity;
|
||||
int value = deque->data[deque->back];
|
||||
deque->size--;
|
||||
return value;
|
||||
}
|
||||
|
||||
// Prints the deque
|
||||
void print_deque(Deque *deque)
|
||||
{
|
||||
printf("Deque: ");
|
||||
for (int i = 0; i < deque->size; i++)
|
||||
{
|
||||
printf("%d ", deque->data[(deque->front + i) % deque->capacity]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// Frees the deque
|
||||
void free_deque(Deque *deque)
|
||||
{
|
||||
free(deque->data);
|
||||
free(deque);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
Deque *deque = init_deque(4);
|
||||
|
||||
push_back(deque, 10);
|
||||
push_back(deque, 20);
|
||||
push_front(deque, 30);
|
||||
push_front(deque, 40);
|
||||
print_deque(deque);
|
||||
|
||||
printf("Popped from front: %d\n", pop_front(deque));
|
||||
print_deque(deque);
|
||||
|
||||
printf("Popped from back: %d\n", pop_back(deque));
|
||||
print_deque(deque);
|
||||
|
||||
free_deque(deque);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// Node structure for the doubly linked list
|
||||
typedef struct Node
|
||||
{
|
||||
uintptr_t data; // Generic data pointer or integer value
|
||||
struct Node *prev;
|
||||
struct Node *next;
|
||||
} Node;
|
||||
|
||||
// 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
|
||||
} DoublyLinkedList;
|
||||
|
||||
// Initializes a new doubly linked list
|
||||
DoublyLinkedList *init_list()
|
||||
{
|
||||
DoublyLinkedList *list = (DoublyLinkedList *)malloc(sizeof(DoublyLinkedList));
|
||||
if (!list)
|
||||
{
|
||||
fprintf(stderr, "Memory allocation failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
list->size = 0;
|
||||
list->head = NULL;
|
||||
list->tail = NULL;
|
||||
return list;
|
||||
}
|
||||
|
||||
// Creates a new node
|
||||
Node *create_node(uintptr_t data)
|
||||
{
|
||||
Node *new_node = (Node *)malloc(sizeof(Node));
|
||||
if (!new_node)
|
||||
{
|
||||
fprintf(stderr, "Memory allocation failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
new_node->data = data;
|
||||
new_node->prev = NULL;
|
||||
new_node->next = NULL;
|
||||
return new_node;
|
||||
}
|
||||
|
||||
// Adds a value to the front of the list
|
||||
void push_front(DoublyLinkedList *list, uintptr_t data)
|
||||
{
|
||||
Node *new_node = create_node(data);
|
||||
if (list->head == NULL)
|
||||
{
|
||||
list->head = new_node;
|
||||
list->tail = new_node;
|
||||
}
|
||||
else
|
||||
{
|
||||
new_node->next = list->head;
|
||||
list->head->prev = new_node;
|
||||
list->head = new_node;
|
||||
}
|
||||
list->size++;
|
||||
}
|
||||
|
||||
// Adds a value to the end of the list
|
||||
void push_back(DoublyLinkedList *list, uintptr_t data)
|
||||
{
|
||||
Node *new_node = create_node(data);
|
||||
if (list->tail == NULL)
|
||||
{
|
||||
list->head = new_node;
|
||||
list->tail = new_node;
|
||||
}
|
||||
else
|
||||
{
|
||||
new_node->prev = list->tail;
|
||||
list->tail->next = new_node;
|
||||
list->tail = new_node;
|
||||
}
|
||||
list->size++;
|
||||
}
|
||||
|
||||
// Removes a value from the front of the list
|
||||
uintptr_t pop_front(DoublyLinkedList *list)
|
||||
{
|
||||
if (list->head == NULL)
|
||||
{
|
||||
fprintf(stderr, "List is empty\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
Node *temp = list->head;
|
||||
uintptr_t data = temp->data;
|
||||
list->head = list->head->next;
|
||||
if (list->head != NULL)
|
||||
{
|
||||
list->head->prev = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
list->tail = NULL;
|
||||
}
|
||||
free(temp);
|
||||
list->size--;
|
||||
return data;
|
||||
}
|
||||
|
||||
// Removes a value from the end of the list
|
||||
uintptr_t pop_back(DoublyLinkedList *list)
|
||||
{
|
||||
if (list->tail == NULL)
|
||||
{
|
||||
fprintf(stderr, "List is empty\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
Node *temp = list->tail;
|
||||
uintptr_t data = temp->data;
|
||||
list->tail = list->tail->prev;
|
||||
if (list->tail != NULL)
|
||||
{
|
||||
list->tail->next = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
list->head = NULL;
|
||||
}
|
||||
free(temp);
|
||||
list->size--;
|
||||
return data;
|
||||
}
|
||||
|
||||
// Prints the list from front to back
|
||||
void print_list(DoublyLinkedList *list)
|
||||
{
|
||||
Node *current = list->head;
|
||||
printf("List: ");
|
||||
while (current != NULL)
|
||||
{
|
||||
printf("%lu ", current->data);
|
||||
current = current->next;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// Frees the entire list
|
||||
void free_list(DoublyLinkedList *list)
|
||||
{
|
||||
Node *current = list->head;
|
||||
while (current != NULL)
|
||||
{
|
||||
Node *next = current->next;
|
||||
free(current);
|
||||
current = next;
|
||||
}
|
||||
free(list);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
DoublyLinkedList *list = init_list();
|
||||
|
||||
push_front(list, 10);
|
||||
push_front(list, 20);
|
||||
push_back(list, 30);
|
||||
print_list(list);
|
||||
|
||||
printf("Popped from front: %lu\n", pop_front(list));
|
||||
print_list(list);
|
||||
|
||||
printf("Popped from back: %lu\n", pop_back(list));
|
||||
print_list(list);
|
||||
|
||||
free_list(list);
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user