Skip to content

Instantly share code, notes, and snippets.

@ArnonEilat
Last active November 10, 2023 21:52
Show Gist options
  • Save ArnonEilat/4470948 to your computer and use it in GitHub Desktop.
Save ArnonEilat/4470948 to your computer and use it in GitHub Desktop.
Very simple singly linked list implementation in C with usage example.
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int info;
} DATA;
typedef struct node {
DATA data;
struct node* next;
} NODE;
void init(NODE** head) {
*head = NULL;
}
void print_list(NODE* head) {
NODE * temp;
for (temp = head; temp; temp = temp->next)
printf("%5d", temp->data.info);
}
NODE* add(NODE* node, DATA data) {
NODE* temp = (NODE*) malloc(sizeof (NODE));
if (temp == NULL) {
exit(0); // no memory available
}
temp->data = data;
temp->next = node;
node = temp;
return node;
}
void add_at(NODE* node, DATA data) {
NODE* temp = (NODE*) malloc(sizeof (NODE));
if (temp == NULL) {
exit(EXIT_FAILURE); // no memory available
}
temp->data = data;
temp->next = node->next;
node->next = temp;
}
void remove_node(NODE* head) {
NODE* temp = (NODE*) malloc(sizeof (NODE));
if (temp == NULL) {
exit(EXIT_FAILURE); // no memory available
}
temp = head->next;
head->next = head->next->next;
free(temp);
}
NODE * reverse_rec(NODE * ptr, NODE * previous) {
NODE * temp;
if (ptr->next == NULL) {
ptr->next = previous;
return ptr;
} else {
temp = reverse_rec(ptr->next, ptr);
ptr->next = previous;
return temp;
}
}
NODE * reverse(NODE * node) {
NODE * temp;
NODE * previous = NULL;
while (node != NULL) {
temp = node->next;
node->next = previous;
previous = node;
node = temp;
}
return previous;
}
NODE *free_list(NODE *head) {
NODE *tmpPtr = head;
NODE *followPtr;
while (tmpPtr != NULL) {
followPtr = tmpPtr;
tmpPtr = tmpPtr->next;
free(followPtr);
}
return NULL;
}
NODE *sort_list(NODE *head) {
NODE *tmpPtr = head, *tmpNxt = head->next;
DATA tmp;
while (tmpNxt != NULL) {
while (tmpNxt != tmpPtr) {
if (tmpNxt->data.info < tmpPtr->data.info) {
tmp = tmpPtr->data;
tmpPtr->data = tmpNxt->data;
tmpNxt->data = tmp;
}
tmpPtr = tmpPtr->next;
}
tmpPtr = head;
tmpNxt = tmpNxt->next;
}
return tmpPtr;
}
int main() {
int i;
NODE* head;
NODE* node;
DATA element;
printf("Add Elements to List:\n");
init(&head);
for (i = 53; i <= 63; i++) {
element.info = i;
printf("Add Element %2d To The List.\n", element.info);
head = add(head, element);
}
printf("\nPrint The List:\n");
print_list(head);
printf("\nRemove Element From The List:\n");
node = head->next->next;
remove_node(node);
printf("\nAdd Element To The List:\n");
node = head->next->next->next;
element.info = 2000;
add_at(node, element);
head = reverse(head); // Revers The List
head = sort_list(head); // Sort The List
head = free_list(head);
return (EXIT_SUCCESS);
}
@Dadp00l
Copy link

Dadp00l commented Sep 23, 2022

Simple

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment