summaryrefslogtreecommitdiff
path: root/src/01/02b/doubly_linked_list.c
blob: 86fb1cee0c5b943c1e550509f164671b3451a15d (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "doubly_linked_list.h"
#include <stdio.h>
#include <stdlib.h>

/**
 * The equivalent of a constructor
 * to initialize a new node in a doubly linked list.
 *
 * @param data The data to initialize the head node with.
 * @return new Node bound to the data specified
 */
Node *initialize(int data) {
  Node *node = malloc(sizeof(Node));
  node->data = data;
  node->next = NULL;
  node->prev = NULL;
  return node;
}

/**
 * Returns the tail of the list.
 *
 * @param head The head of the linked list
 * @return The tail of the linked list
 */
Node *tail(Node *head) {
  Node *tmp = head;
  while (tmp) {
    if (!tmp->next)
      break;
    tmp = tmp->next;
  }
  return tmp;
}

/**
 * Adds data to the tail of a doubly linked list.
 *
 * @param head The head of the doubly linked list
 * @param data The data to append to the list
 * @return Returns the new node.
 */
Node *add(Node *head, int data) {
  Node *t = tail(head);
  t->next = initialize(data);
  t->next->prev = t;
  return t->next;
}

/**
 * Returns a specific node in the list by index starting from 0.
 *
 * @param from The node to start scanning from.
 * @param index The zero based index of the node to retrieve.
 * @return The node at the specified index.
 */
Node *get(Node *from, int index) {
  if (!from || index < 0)
    return NULL;

  while (index > 0 && from) {
    from = from->next;
    index--;
  }
  return from;
}

/**
 * Returns the number of items in the linked list.
 *
 * @param head The head of the linked list
 * @return The number of items in the list.
 */
static int size(Node *head) {
  int i = 0;
  for (Node *tmp = head; tmp && tmp != NULL; tmp = tmp->next)
    i++;
  return i;
}

/**
 * Assigns the next pointer of a node.
 *
 * @param self The node struct to alter.
 * @param other The other node to point to.
 */
static void assign_next(Node *self, Node *other) {
  if (self)
    self->next = other;

  if (other)
    other->prev = self;
}

/**
 * Assigns the previous pointer of a node.
 *
 * @param self The node struct to alter.
 * @param other The other node to point to.
 */
static void assign_prev(Node *self, Node *other) {
  if (self)
    self->prev = other;

  if (other)
    other->next = self;
}

/**
 * Swaps two different nodes in the same linked list.
 *
 * @param x A node to swap
 * @param y Another node to swap
 */
void swap(Node *x, Node *y) {
  if (x == y)
    return;
  if (!x || !y)
    return;
  if (x->prev == y && y->next == x)
    return swap(y, x);

  Node *xp = x->prev, *xn = x->next, *yp = y->prev, *yn = y->next;

  // if adjacent
  if (x->next == y && y->prev == x) {
    assign_next(x, yn);
    assign_prev(x, y);
    assign_prev(y, xp);
  } else {
    assign_prev(x, yp);
    assign_next(x, yn);
    assign_prev(y, xp);
    assign_next(y, xn);
  }
}

/**
 * Prints the previous, data and next pointers for a node
 *
 * @param node The node to print
 */
static void print(Node *node) {
  if (node->prev && node->next)
    printf("(%d<%d>%d)", node->prev->data, node->data, node->next->data);
  else if (node->next)
    printf("(nil<%d>%d)", node->data, node->next->data);
  else
    printf("(%d<%d>nil)", node->prev->data, node->data);
  printf(" ");
}

/**
 * Iterates through each node in a linked list.
 *
 * @param self The node to start iterating from.
 * @param block The callback function to invoke on each node.
 */
void each(Node *self, Visitor block) {
  Node *tmp = self;

  while (tmp) {
    block(tmp);
    tmp = tmp->next;
  }
}

/**
 * Renders a visual output of a full linked list.
 *
 * @param node The head of the linked list
 */
void inspect(Node *node) {
  printf("[ ");
  each(node, &print);
  printf("]\n");
}