chatgpt has the spirit

This commit is contained in:
lda committed 2025-06-30 09:53:05 +07:00
1 parent fd814b8572
commit abbb01049d
2 files changed
+146

No files matched your search

+11
View File
@@ -0,0 +1,11 @@
{
"files.associations": {
"algorithm": "cpp",
"optional": "cpp",
"system_error": "cpp",
"type_traits": "cpp",
"xlocmon": "cpp",
"xtr1common": "cpp",
"xutility": "cpp"
}
}
+135
View File
@@ -0,0 +1,135 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
// Node structure for singly linked list
typedef struct Node
{
uintptr_t value; // Generic data pointer or integer value
struct Node *next;
} Node;
// Singly linked list structure
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
} SinglyLinkedList;
// Initializes a new singly linked list
SinglyLinkedList *create_list()
{
SinglyLinkedList *list = (SinglyLinkedList *)malloc(sizeof(SinglyLinkedList));
if (!list)
{
fprintf(stderr, "Memory allocation failed\n");
return NULL;
}
list->size = 0;
list->head = NULL;
list->tail = NULL;
return list;
}
// Creates 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");
return NULL;
}
new_node->value = value;
new_node->next = NULL;
return new_node;
}
// Adds a 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
}
if (list->tail == NULL)
{
list->head = new_node;
list->tail = new_node;
}
else
{
list->tail->next = new_node;
list->tail = new_node;
}
list->size++;
return 0; // Success
}
// Removes a value 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
}
Node *temp = list->head;
uintptr_t value = temp->value;
list->head = list->head->next;
if (list->head == NULL)
{
list->tail = NULL;
}
free(temp);
list->size--;
return value;
}
// Prints the list
void print_list(const SinglyLinkedList *list)
{
Node *current = list->head;
printf("List: ");
while (current != NULL)
{
printf("%lu ", current->value);
current = current->next;
}
printf("\n");
}
// Frees the entire list
void free_list(SinglyLinkedList *list)
{
Node *current = list->head;
while (current != NULL)
{
Node *next = current->next;
free(current);
current = next;
}
free(list);
}
int main()
{
SinglyLinkedList *list = create_list();
if (!list)
{
return EXIT_FAILURE;
}
append(list, 10);
append(list, 20);
append(list, 30);
print_list(list);
printf("Removed: %lu\n", remove_first(list));
print_list(list);
free_list(list);
return 0;
}