C_Algorithms 2.0.0
Documentation
Loading...
Searching...
No Matches
Data Structures | Typedefs | Functions
doublyLinkedList.h File Reference
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
Include dependency graph for doublyLinkedList.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  _Node_
 
struct  _List_
 

Typedefs

typedef struct _Node_ Node
 
typedef struct _List_ List
 

Functions

NodecreateNode (void)
 Allocates memory for a new node and initalizes it.
 
bool freeNode (Node *node)
 Frees the memory of a node.
 
ListcreateList (void)
 Allocates memory for a new list and initalizes it.
 
bool freeList (List *list)
 Frees the memory of a list and all nodes in it.
 
NodeappendNodeToEndOfList (List *list, Node *node, bool check_for_existing_node)
 Append a node to the end of a list.
 
ListappendListToEndOfList (List *list, List *list_to_append)
 
bool removeNodeFromList (List *list, Node *node)
 Removes a node from the list.
 
NoderemoveNodeWithValue (List *list, int data)
 Remove a node from a list with a specific value of playing_card_.
 
Noderemove_first (List *list)
 Remove first node in list.
 
bool checkElementInList (Node *current_node, Node *node_to_check)
 Checks if the adress of a node is already in a list (rekursive)
 
void printList (List *list)
 Print the whole List without trailing
.
 

Typedef Documentation

◆ List

typedef struct _List_ List

Definition at line 11 of file doublyLinkedList.h.

◆ Node

typedef struct _Node_ Node

Definition at line 10 of file doublyLinkedList.h.

Function Documentation

◆ appendListToEndOfList()

List * appendListToEndOfList ( List * list,
List * list_to_append )

Definition at line 139 of file doublyLinkedList.c.

140{
141 if (list->size_ == 0)
142 {
143 list->first_ = list_to_append->first_;
144 list->last_ = list_to_append->last_;
145 list->size_ = list_to_append->size_;
146 }
147 else
148 {
149 list->last_->next_ = list_to_append->first_; // Set next from last element in list to the new node
150 list_to_append->first_->previous_ = list->last_;
151 list->last_ = list_to_append->last_;
152 list->size_ = list->size_ + list_to_append->size_;
153 }
154 list_to_append->first_ = NULL;
155 list_to_append->last_ = NULL;
156 list_to_append->size_ = 0;
157 return list;
158}
Node * last_
Node * first_
Node * next_
Node * previous_

References _List_::first_, _List_::last_, _Node_::next_, _Node_::previous_, and _List_::size_.

◆ appendNodeToEndOfList()

Node * appendNodeToEndOfList ( List * list,
Node * node,
bool check_for_existing_node )

Append a node to the end of a list.

Parameters
listAddress of the list to append to
nodeAddress of the node
check_for_existing_nodetrue: same node can only be saved once | false: the same node can be saved multiple times
Returns
Returns the address of the appendend node

Definition at line 115 of file doublyLinkedList.c.

116{
117 // Element is already in the list
118 if ((check_for_existing_node ? checkElementInList(list->first_, node) : false))
119 {
120 return node;
121 }
122
123 if (list->size_ == 0)
124 {
125 list->first_ = node;
126 list->last_ = node;
127 list->size_++;
128 }
129 else
130 {
131 list->last_->next_ = node; // Set next from last element in list to the new node
132 node->previous_ = list->last_; // Set pointer to now second last element
133 list->last_ = node;
134 list->size_++;
135 }
136 return node;
137}
bool checkElementInList(Node *current_node, Node *node_to_check)
Checks if the adress of a node is already in a list (rekursive)

References checkElementInList(), _List_::first_, _List_::last_, _Node_::next_, _Node_::previous_, and _List_::size_.

Referenced by FunctionCall().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ checkElementInList()

bool checkElementInList ( Node * current_node,
Node * node_to_check )

Checks if the adress of a node is already in a list (rekursive)

Parameters
current_nodeAddress of the node to begin the check
node_to_checkThe address of the node to check
Returns
Returns true if the element is already in the list.

Definition at line 89 of file doublyLinkedList.c.

90{
91 // Check if list is empty
92 if (current_node == NULL)
93 {
94 return false;
95 }
96 // Check if the current node is the same as the one to check
97 if (current_node == node_to_check)
98 {
99 return true;
100 }
101 // Else go recursive into the function with the next element in the list
102 else
103 {
104 return checkElementInList(current_node->next_, node_to_check);
105 }
106 return false;
107}

References checkElementInList(), and _Node_::next_.

Referenced by appendNodeToEndOfList(), and checkElementInList().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ createList()

List * createList ( void )

Allocates memory for a new list and initalizes it.

Returns
Returns address of the created list

Definition at line 38 of file doublyLinkedList.c.

39{
40 List *list = (List *)malloc(sizeof(List));
41 if (list == NULL)
42 {
43 return NULL;
44 }
45
46 list->size_ = 0;
47 list->first_ = NULL;
48 list->last_ = NULL;
49
50 return list;
51}

References _List_::first_, _List_::last_, and _List_::size_.

Referenced by FunctionCall().

Here is the caller graph for this function:

◆ createNode()

Node * createNode ( void )

Allocates memory for a new node and initalizes it.

Returns
Returns address of the created node

Definition at line 6 of file doublyLinkedList.c.

7{
8 Node *node = (Node *)malloc(sizeof(Node));
9 if (node == NULL)
10 {
11 return NULL;
12 }
13
14 // Initialize node
15 node->next_ = NULL;
16 node->previous_ = NULL;
17 node->data_ = 0;
18
19 return node;
20}

References _Node_::data_, _Node_::next_, and _Node_::previous_.

Referenced by FunctionCall().

Here is the caller graph for this function:

◆ freeList()

bool freeList ( List * list)

Frees the memory of a list and all nodes in it.

Parameters
listAddress to a List struct
Returns
true if the list and all nodes has been freed / false if list was NULL

Definition at line 56 of file doublyLinkedList.c.

57{
58 if (list == NULL)
59 {
60 return false;
61 }
62
63 int list_size = list->size_;
64 if (list_size > 0)
65 {
66 Node *current_node;
67 for (int i = 0; i < list_size; i++)
68 {
69 current_node = list->last_;
70 if (!removeNodeFromList(list, current_node))
71 {
72 return false;
73 }
74 if (!freeNode(current_node))
75 {
76 return false;
77 }
78 }
79 }
80
81 free(list);
82 return true;
83}
bool removeNodeFromList(List *list, Node *node)
Removes a node from the list.
bool freeNode(Node *node)
Frees the memory of a node.

References freeNode(), _List_::last_, removeNodeFromList(), and _List_::size_.

Referenced by FunctionCall().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ freeNode()

bool freeNode ( Node * node)

Frees the memory of a node.

Parameters
nodeAddress to a Node struct
Returns
true if the node has been freed / false if node was NULL

Definition at line 25 of file doublyLinkedList.c.

26{
27 if (node == NULL)
28 {
29 return false;
30 }
31
32 free(node);
33 return true;
34}

Referenced by freeList().

Here is the caller graph for this function:

◆ printList()

void printList ( List * list)

Print the whole List without trailing
.

Parameters
listList to be printed

Definition at line 271 of file doublyLinkedList.c.

272{
273 Node *current_node = list->first_;
274 int count = 0;
275 while (current_node != NULL)
276 {
277 // Example print if data is an integer
278 printf("List element %d, value: %d\n", count, current_node->data_);
279 current_node = current_node->next_;
280 count ++;
281 }
282}

References _Node_::data_, _List_::first_, and _Node_::next_.

Referenced by FunctionCall().

Here is the caller graph for this function:

◆ remove_first()

Node * remove_first ( List * list)

Remove first node in list.

Parameters
list
Returns

Definition at line 255 of file doublyLinkedList.c.

256{
257 if (list->size_ == 0)
258 {
259 return NULL;
260 }
261
262 Node *removed = list->first_;
263 Node *second = list->first_->next_;
264 list->first_ = second;
265 list->size_--;
266 return removed;
267}

References _List_::first_, _Node_::next_, and _List_::size_.

◆ removeNodeFromList()

bool removeNodeFromList ( List * list,
Node * node )

Removes a node from the list.

Parameters
listAddress of the list
nodeAddress of node which should be removed
Returns
true: could be removed | false: could not be removed

Definition at line 164 of file doublyLinkedList.c.

165{
166 if (list->size_ == 0)
167 {
168 return NULL;
169 }
170
171 if (node->next_ == NULL && node->previous_ == NULL)
172 {
173 list->first_ = NULL;
174 list->last_ = NULL;
175 list->size_--;
176
177 return true;
178 }
179 else if (node->next_ == NULL) // Node is the last element in the list
180 {
181 list->last_ = node->previous_; // Set last list element to the one before this node
182 node->previous_->next_ = NULL; // Set the pointer from the second last node to this to NULL
183 list->size_--;
184
185 return true;
186 }
187 else if (node->previous_ == NULL) // Node is first element in list
188 {
189 list->first_ = node->next_; // Set first list element to the one after this node
190 node->next_->previous_ = NULL; // Set the pointer from the second node to this to NULL
191 list->size_--;
192
193 return true;
194 }
195 else
196 {
197 node->next_->previous_ = node->previous_;
198 node->previous_->next_ = node->next_;
199 list->size_--;
200
201 return true;
202 }
203 return false;
204}

References _List_::first_, _List_::last_, _Node_::next_, _Node_::previous_, and _List_::size_.

Referenced by freeList().

Here is the caller graph for this function:

◆ removeNodeWithValue()

Node * removeNodeWithValue ( List * list,
int data )

Remove a node from a list with a specific value of playing_card_.

Parameters
listList where the node should be removed
valueValue of playing_card_ that should be removed
Returns
Returns the address of the node that is removed from the list

Definition at line 210 of file doublyLinkedList.c.

211{
212 Node *current_node = list->first_;
213 while (current_node != NULL)
214 {
215 if (current_node->data_ == data)
216 {
217 if (current_node == list->first_)
218 {
219 list->first_ = current_node->next_;
220 }
221 else
222 {
223 current_node->previous_->next_ = current_node->next_;
224 }
225 if (current_node == list->last_)
226 {
227 list->last_ = current_node->previous_;
228 }
229 else
230 {
231 current_node->next_->previous_ = current_node->previous_;
232 }
233 if(list->first_ != NULL)
234 {
235 list->first_->previous_ = NULL;
236 }
237 if(list->last_ != NULL)
238 {
239 list->last_->next_ = NULL;
240 }
241
242 list->size_--;
243 current_node->previous_ = NULL;
244 current_node->next_ = NULL;
245 return current_node;
246 }
247 current_node = current_node->next_;
248 }
249 return NULL; // value not found
250}

References _Node_::data_, _List_::first_, _List_::last_, _Node_::next_, _Node_::previous_, and _List_::size_.