about summary refs log tree commit diff
path: root/linked_list.c
blob: 98b672b57b718ec42f672d18fed4cc655f038425 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>
#include <stdlib.h>

#include "linked_list.h"

Node *initialise_node()
{
	Node* newNode = malloc(sizeof(Node));
	
	newNode->next = NULL;
	newNode->prev = NULL;

	return newNode;
}

void free_node(Node* node)
{
	if(node)
		free(node);
	return;
}

LinkedList *initialise_linked_list()
{
	LinkedList* newList = malloc(sizeof(LinkedList));
	
	newList->head = NULL;
	newList->tail = NULL;

	return newList;
}

void free_linked_list(LinkedList* list)
{
	Node* element;

	while(list->head)
	{
		element = list->head->next;
		free_node(list->head);
		list->head = element;
	}
	free(list);
}

void append_linked_list(LinkedList* list, int x, int y)
{
	Node* newNode = initialise_node();
	
	newNode->x = x;
	newNode->y = y;

	newNode->prev = list->tail;

	if(list->tail)
		list->tail->next = newNode;
	list->tail = newNode;
	if(!list->head)
		list->head = newNode;
}

void remove_tail_linked_list(LinkedList* list)
{
	Node* element;

	if(!list->tail)
		return;
	element = list->tail->prev;
	free_node(list->tail);
	list->tail = element;
	if(list->tail)
		list->tail->next = NULL;
	else
		list->head = NULL;
}