Repository navigation
Expand file tree
/
Copy pathlinkedlist.h
More file actions
169 lines (155 loc) · 4.38 KB
/
Copy pathlinkedlist.h
File metadata and controls
169 lines (155 loc) · 4.38 KB
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
#ifndef LINKEDLIST_JL
#define LINKEDLIST_JL
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
// Define how our node is structured.
typedef struct Node{
int data; // The value of the node that we need to store.
struct Node* next; // The pointer to the next node in the list.
} Node;
// Define how our (linear and dynamic) linked list is structured.
typedef struct{
Node* head; // The pointer to the first node in our list.
int size; // The number of elements (nodes) in our list.
} LinkedList;
/********************/
/* FUNCTIONS */
/********************/
// Set Up the Linked List. We start setting the head pointer value to null and size to 0.
void initLinkedList(LinkedList *linkedlist){
linkedlist->head = NULL;
linkedlist->size = 0;
}
// Create a New Node. Returns a newNode value.
Node* createNode(int value){
Node* newNode = (Node*)malloc(sizeof(Node));
if(!newNode){
return NULL;
}
newNode->data = value;
newNode->next = NULL;
return newNode;
}
// Insert at the Beginning of the List. Returns 1 if successful or 0 if the new node is not created.
int insertAtHead(LinkedList *linkedlist, int value){
Node* newNode = createNode(value);
if(!newNode){
return 0;
}
else{
newNode->next = linkedlist->head;
linkedlist->head = newNode;
linkedlist->size++;
return 1;
}
}
// Insert at the End of the List. Returns 1 if successful or 0 if the new node is not created.
int insertAtTail(LinkedList *linkedlist, int value){
Node* newNode = createNode(value);
if(!newNode){
return 0;
}
else{
if(linkedlist->head == NULL){
linkedlist->head = newNode;
}
else{
Node* temp = linkedlist->head;
while(temp->next != NULL){
temp = temp->next;
}
temp->next = newNode;
}
linkedlist->size++;
return 1;
}
}
// Delete a Node by its Position. Returns 1 if successful or 0 if the index is invalid.
int deleteAtIndex(LinkedList *linkedlist, int index){
if(index < 0 || index >= linkedlist->size || linkedlist->head == NULL){
return 0;
}
else{
Node* temp = linkedlist->head;
if(index == 0){
linkedlist->head = temp->next;
free(temp);
linkedlist->size--;
}
else{
Node *prev = NULL;
for(int i = 0; i < index; i++){
prev = temp;
temp = temp->next;
}
prev->next = temp->next;
free(temp);
linkedlist->size--;
}
return 1;
}
}
// Delete a Node by Value (First One is Deleted). Returns 1 if successful or 0 if the node is not deleted.
int deleteByValue(LinkedList *linkedlist, int value){
if(linkedlist->head == NULL){
return 0;
}
else{
Node *temp = linkedlist->head;
Node *prev = NULL;
while(temp != NULL){
if(temp->data == value){
if(prev == NULL){
linkedlist->head = temp->next;
}
else{
prev->next = temp->next;
}
free(temp);
linkedlist->size--;
return 1;
}
prev = temp;
temp = temp->next;
}
return 0;
}
}
// Search for a Value in the List. Returns 1 if found or 0 if it's not.
int searchList(LinkedList *linkedlist, int value){
Node* temp = linkedlist->head;
while(temp != NULL){
if(temp->data == value){
return 1;
}
temp = temp->next;
}
return 0;
}
// Print all Elements of the Linked List. Displays elements from first to last one.
void printLinkedList(LinkedList *linkedlist){
printf("[");
Node* temp = linkedlist->head;
while(temp != NULL){
printf("%d", temp->data);
if(temp->next != NULL){
printf(", ");
}
temp = temp->next;
}
printf("]\n");
}
// Free all the Nodes and Reset the List.
void freeList(LinkedList *linkedlist){
Node *temp = linkedlist->head;
while(temp != NULL){
Node *nextNode = temp->next;
free(temp);
temp = nextNode;
}
linkedlist->head = NULL;
linkedlist->size = 0;
}
#endif //LINKEDLIST_JL