RTM Project

This commit is contained in:
2024-05-07 22:02:08 +05:30
commit f7d7a53ade
20 changed files with 2033 additions and 0 deletions

25
DLL/CMakeLists.txt Normal file
View File

@@ -0,0 +1,25 @@
set( DLL_SOURCE
"dll.c")
set( DLL_HEADER
"dll.h")
set( TEST_EXE
"main.c")
add_library( ${DLL_NAME} STATIC
${DLL_SOURCE}
${DLL_HEADER})
if( DLL_TESTING )
add_executable( testDll
${TEST_EXE})
target_include_directories( testDll PUBLIC
"./")
endif()
target_include_directories( ${DLL_NAME} PUBLIC
"./" #for files on same path as this CMakeLists.txt file.
)

71
DLL/dll.c Normal file
View File

@@ -0,0 +1,71 @@
#include <stdio.h>
#include <stdlib.h>
#include "dll.h"
/*
* Initialize an empty list in which the head
* and the tail are the same, i.e. they both
* point to dummy node.
*/
dll_t* init_dll() {
dll_t* dll = calloc(1, sizeof(dll_t));
dll_node_t* node = calloc(1, sizeof(dll_node_t));
node->next = node;
node->prev = node;
dll->head = node;
dll->tail = node;
return dll;
}
/*
* Add new node with data to back of list.
*/
void append(dll_t* dll, void* data) {
dll_node_t* head = dll->head;
dll_node_t* node = calloc(1, sizeof(dll_node_t));
node->data = data;
node->next = dll->head;
node->prev = dll->tail;
dll->head->prev = node;
dll->tail->next = node;
dll->tail = node;
if (dll->head != head) {
printf("Head of the list is compromised.\n");
}
}
/*
* Delete a node from the lists.
*/
void del(dll_t* dll, dll_node_t* node) {
node->prev->next = node->next;
node->next->prev = node->prev;
if (node == dll->tail) {
// need to update the tail node if we're deleting it.
dll->tail = node->prev;
}
free(node);
}
/*
* Delete all nodes from the list.
*/
void deinit_dll(dll_t* dll) {
dll_node_t* node = dll->head->next;
while (node != dll->head) {
dll_node_t* next = node->next;
del(dll, node);
node = next;
}
free(dll);
}

47
DLL/dll.h Normal file
View File

@@ -0,0 +1,47 @@
#ifndef DLL_H
#define DLL_H
/*
* Data Structure definations for
* generic cicular doubly linked
* list.
*/
typedef struct dll_node_ {
void* data;
struct dll_node_* next;
struct dll_node_* prev;
}dll_node_t;
typedef struct dll_ {
dll_node_t* head;
dll_node_t* tail;
}dll_t;
/*
* DLL API'S
*/
dll_t* init_dll();
void append(dll_t* dll, void* data);
void del(dll_t* dll, dll_node_t* node);
void deinit_dll(dll_t* dll);
#define ITERATE_DLL( dll, data_ptr, struct_type ) { \
dll_node_t* c_ptr = dll->head->next; \
while( c_ptr != dll->head ){ \
struct_type* data_ptr = (struct_type* )c_ptr->data; \
c_ptr = c_ptr->next;
#define ITERATE_DLL_END() }
#endif

65
DLL/main.c Normal file
View File

@@ -0,0 +1,65 @@
#include <stdio.h>
#include <stdlib.h>
#include "dll.h"
dll_node_t* find(dll_t* dll, int n) {
dll_node_t* node = dll->head->next;
while (node != dll->head) {
if (*((int*)node->data) == n) {
return node;
}
node = node->next;
}
return node;
}
void print(dll_t* dll) {
printf("Printing...\n");
dll_node_t* node = dll->head->next;
while (node != dll->head) {
printf("%i\n", *((int*)node->data));
node = node->next;
}
}
void reverse_print(dll_t* dll) {
printf("Reverse printing...\n");
dll_node_t* node = dll->tail;
while (node != dll->head) {
printf("%i\n", *((int*)node->data));
node = node->prev;
}
}
int main() {
dll_t* dll = init_dll();
int i;
for (i = 0; i < 5; i++) {
int* n = malloc(sizeof(int));
*n = i;
append(dll, n);
}
//print(dll);
reverse_print(dll);
for (i = 0; i < 5; i++) {
if (find(dll, i) != dll->head) {
printf("Correct\n");
}
}
del(dll, find(dll, 3));
del(dll, find(dll, 0));
del(dll, find(dll, 4));
print(dll);
reverse_print(dll);
deinit_dll(dll);
print(dll);
reverse_print(dll);
exit(0);
}