ArchiSys/TP/TP1/rendu/linked_list.c

125 lines
2.5 KiB
C
Raw Normal View History

2024-03-26 17:06:15 +01:00
// linked list
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "linked_list.h"
2024-03-29 22:46:30 +01:00
linkedList* createLinkedList() {
linkedList *list = (linkedList*)malloc(sizeof(linkedList));
2024-03-26 17:45:20 +01:00
list->head = NULL;
return list;
2024-03-26 17:06:15 +01:00
}
//insert link at the first location
2024-03-29 22:46:30 +01:00
void insertFirst(linkedList *list, int key, void *data) {
2024-03-26 17:45:20 +01:00
//create a link
2024-03-29 22:46:30 +01:00
node *link = (node*) malloc(sizeof(node));
2024-03-26 17:06:15 +01:00
2024-03-26 17:45:20 +01:00
link->key = key;
link->data = data;
2024-03-26 17:06:15 +01:00
2024-03-26 17:45:20 +01:00
//point it to old first node
link->next = list->head;
2024-03-26 17:06:15 +01:00
2024-03-26 17:45:20 +01:00
//point first to new first node
list->head = link;
2024-03-26 17:06:15 +01:00
}
//delete first item
2024-03-29 22:46:30 +01:00
node* deleteFirst(linkedList *list) {
2024-03-26 17:45:20 +01:00
if (list->head == NULL) {
return NULL;
}
2024-03-26 17:06:15 +01:00
2024-03-26 17:45:20 +01:00
//save reference to first link
2024-03-29 22:46:30 +01:00
node *tempLink = list->head;
2024-03-26 17:06:15 +01:00
2024-03-26 17:45:20 +01:00
//mark next to first link as first
list->head = list->head->next;
2024-03-26 17:06:15 +01:00
2024-03-26 17:45:20 +01:00
//return the deleted link
return tempLink;
2024-03-26 17:06:15 +01:00
}
//find a link with given key
2024-03-29 22:46:30 +01:00
node* find(linkedList *list, int key) {
2024-03-26 17:06:15 +01:00
2024-03-26 17:45:20 +01:00
//start from the first link
2024-03-29 22:46:30 +01:00
node* current = list->head;
2024-03-26 17:06:15 +01:00
2024-03-26 17:45:20 +01:00
//if list is empty
if(list->head == NULL) {
return NULL;
}
2024-03-26 17:06:15 +01:00
2024-03-26 17:45:20 +01:00
//navigate through list
while(current->key != key) {
2024-03-26 17:06:15 +01:00
2024-03-26 17:45:20 +01:00
//if it is last node
if(current->next == NULL) {
return NULL;
} else {
//go to next link
current = current->next;
}
}
2024-03-26 17:06:15 +01:00
2024-03-26 17:45:20 +01:00
//if data found, return the current Link
return current;
2024-03-26 17:06:15 +01:00
}
//delete a link with given key
2024-03-29 22:46:30 +01:00
node* deleteElement(linkedList *list, int key) {
2024-03-26 17:06:15 +01:00
2024-03-26 17:45:20 +01:00
//start from the first link
2024-03-29 22:46:30 +01:00
node* current = list->head;
node* previous = NULL;
2024-03-26 17:06:15 +01:00
2024-03-26 17:45:20 +01:00
//if list is empty
if(list->head == NULL) {
return NULL;
}
//navigate through list
while(current->key != key) {
//if it is last node
if(current->next == NULL) {
return NULL;
} else {
//store reference to current link
previous = current;
//move to next link
current = current->next;
}
}
//found a match, update the link
if(current == list->head) {
//change first to point to next link
list->head = list->head->next;
} else {
//bypass the current link
previous->next = current->next;
}
2024-03-26 17:06:15 +01:00
2024-03-26 17:45:20 +01:00
return current;
2024-03-26 17:06:15 +01:00
}
//is list empty
2024-03-29 22:46:30 +01:00
bool isEmpty(linkedList *list) {
2024-03-26 17:45:20 +01:00
return list->head == NULL;
2024-03-26 17:06:15 +01:00
}
2024-03-29 22:46:30 +01:00
int length(linkedList *list) {
2024-03-26 17:45:20 +01:00
int length = 0;
2024-03-29 22:46:30 +01:00
node *current;
2024-03-26 17:06:15 +01:00
2024-03-26 17:45:20 +01:00
for(current = list->head; current != NULL; current = current->next) {
length++;
}
2024-03-26 17:06:15 +01:00
2024-03-26 17:45:20 +01:00
return length;
2024-03-26 17:06:15 +01:00
}