-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlinkedlist.c
98 lines (90 loc) · 2.63 KB
/
linkedlist.c
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "linkedlist.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
struct node *head = NULL;
struct node *current = NULL;
/* Function: isEmpty
Arguments: None
Return Value: int
Purpose: Returns integer value representing whether
the circular doubly linked list is empty or not
*/
int isEmpty()
{
return head == NULL;
}
/* Function: insertHead
Arguments: char identifier[], int x_solution, int y_solution
Return Value: void
Purpose: Instantiates a new node struct, populates it with the
properties passed to the function, and inserts the
node in the head position of the circular doubly linked list
*/
void insertHead(char identifier[5], int x_solution, int y_solution)
{
struct node *new_node = (struct node *)malloc(sizeof(struct node));
strncpy(new_node->identifier, identifier, 4);
new_node->x_solution = x_solution;
new_node->y_solution = y_solution;
if (isEmpty()) {
head = new_node;
head->next = head;
head->last = head;
} else {
// Insert new node in head's previous position and assign
// new node as new head
new_node->next = head;
head->last->next = new_node;
new_node->last = head->last;
head->last = new_node;
head = new_node;
}
}
/* Function: deleteNode
Arguments: struct node *to_delete
Return Value: void
Purpose: Takes a pointer to the node that should
be deleted, and removes it from the
circular doubly linked list
*/
void deleteNode(struct node *to_delete)
{
if (to_delete == head && to_delete->next == NULL) {
free(head);
} else if (to_delete == head && to_delete->next->next == head) {
head = to_delete->next;
head->next = NULL;
head->last = NULL;
free(to_delete);
} else if (to_delete == head) {
head = to_delete->next;
to_delete->last->next = to_delete->next;
to_delete->next->last = to_delete->last;
free(to_delete);
} else {
to_delete->last->next = to_delete->next;
to_delete->next->last = to_delete->last;
free(to_delete);
}
}
/* Function: printList
Arguments: None
Return Value: void
Purpose: Displays the contents of the nodes
currently present in the circular
doubly linked list
*/
void printList()
{
struct node *iter = head;
printf("(Identifier: %s, Solution x value: %d, Solution y value: %d\n) ",
iter->identifier, iter->x_solution, iter->y_solution);
while (iter->next != head) {
printf
("(Identifier: %s, Solution x value: %d, Solution y value: %d\n) ",
iter->identifier, iter->x_solution, iter->y_solution);
iter = iter->next;
}
}