summaryrefslogtreecommitdiff
path: root/src/01/02a/singly_linked_list.c
blob: e251b6826add28247f20906ddc237a1bbf1da25f (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
#include "singly_linked_list.h"
#include <stdio.h>
#include <stdlib.h>

/**
 * The equivalent of a constructor to initalize a linked list.
 *
 * @param data The initial data to seed the linked list
 * @return Returns a new Node
 */
Node *initialize(int data) {
  Node *node = malloc(sizeof(Node));
  node->data = data;
  node->next = NULL;
  return node;
}

/**
 * Add a node to the tail of the linked list
 *
 * @param head The head of the linked list
 * @param data The data to add to the linked list
 */
Node *add(Node *head, int data) {
  Node *tail;
  Node *tmp = head;

  while(tmp) {
    if (!tmp->next)
      break;
    tmp = tmp->next;
  }
  tail = tmp;
  tail->next = initialize(data);
  return tail->next;
}

/**
 * Gets a specific node by index starting from index 0.
 *
 * @param self The Node to start from
 * @param index The index of the node to return
 * @return The node at the specific index
 */
Node *get(Node *self, int index) {
  if (!self || index < 0) return NULL;

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

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

/**
 * Swaps nodes in a linked list that are in positions
 * x and y.
 *
 * @param head The head of the linked list
 * @param x The node in position x
 * @param y The node in position y
 */
void swap(Node **head, int x, int y) {
  int count = size(*head);

  if (x == y) return;
  if (x >= count) return;
  if (y >= count) return;

  Node *xp = get(*head, x - 1);
  Node *yp = get(*head, y - 1);
  Node *xc = get(*head, x);
  Node *yc = get(*head, y);

  if (x == 0)
    *head = yc;
  else
    xp->next = yc;

  if (y == 0)
    *head = xc;
  else
    yp->next = xc;

  Node *tmp = yc->next;
  yc->next = xc->next;
  xc->next = tmp;
}

/**
 * A helper method used to print a visual representation
 * of a linked list
 *
 * @param node The head of the linked list
 */
void inspect(Node *node) {
  if (!node) return;

  printf("*******\n");
  while (node) {
    printf("\t%d\n", node->data);
    node = node->next;
  }
  printf("*******\n");
}