-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list.c
87 lines (77 loc) · 2.33 KB
/
linked_list.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "linked_list.h"
// Creates a new node:
node createNode(){
node temp;
temp = (node)malloc(sizeof(struct LinkedList));
temp->next = NULL;
return temp;
}
// Adds the new node at the end of the linked list:
node insertNode(node head, int value){
node temp, p; // Declare two nodes (pointers to 'LinkedList' objects), temp and p
temp = createNode(); // Create temp
temp->data = value; // Set its data equal to the function's argument
if (head == NULL){ // If the linked list is empty...
head = temp; // ...temp becomes the head of the list
}
else{ // If the linked list is not empty...
p = head; // ...make p point to the head
while (p->next != NULL){ // While p is not pointing to the last node...
p = p->next; // ...make p point to the next node until it reaches the last node
}
p->next = temp; // When p points to the last node of the list and add temp after it
}
return head;
}
// Removes the first node of the linked list:
node removeNode(node head){
node temp; // Declare (pointer to) node, temp
if (head == NULL){
printf("It's empty!\n");
}
else{
temp = head->next; // Make temp point to the node after the head
free(head); // Delete current head
head = temp; // The node after the old head becomes the new head
}
return head;
}
// Checks if the linked list is empty:
bool isEmpty(node head){
if (head == NULL)
return true;
else
return false;
}
// Prints all nodes of the linked list:
void printLinkedList(node head){
node temp; // Deaclare (pointer to) node, temp
if (head == NULL)
printf("There's nothing here!\n");
else{
temp = head; // Make temp point to the current head
while (temp != NULL){ // While temp is pointing to some node...
printf("%d ", temp->data); //...print that node's data and...
temp = temp->next; //...make temp point to the node next to it
}
printf("\n");
}
}
// Returns the size of linked list
int calculateSize(node head){
node temp;
int size = 0;
if (head == NULL)
return 0;
else{
temp = head;
while (temp != NULL){
size++;
temp = temp->next;
}
return size;
}
}