-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_Single_Linked_List.cpp
More file actions
88 lines (81 loc) · 1.54 KB
/
01_Single_Linked_List.cpp
File metadata and controls
88 lines (81 loc) · 1.54 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
#include <stdio.h>
#include <stdlib.h>
struct Node {
int value;
Node *next;
}*head, *tail;
Node *createNewNode(int value) {
Node *newNode = (Node*)malloc(sizeof(Node));
newNode->value = value;
newNode->next = NULL;
return newNode;
}
void pushHead(int value) {
Node *temp = createNewNode(value);
if(!head) {
head = tail = temp;
} else {
temp->next = head;
head = temp;
}
}
void pushTail(int value) {
Node *temp = createNewNode(value);
if(!head) {
head = tail = temp;
} else {
tail->next = temp;
tail = temp;
}
}
void popHead() {
if(!head) {
return;
} else if(head == tail) {
free(head);
head = tail = NULL;
} else {
Node *temp = head->next;
head->next = NULL;
free(head);
head = temp;
}
}
void popTail() {
if(!head) {
return;
} else if(head == tail) {
free(head);
head = tail = NULL;
} else {
Node *curr = head;
while(curr->next != tail) {
curr = curr->next;
}
curr->next = NULL;
free(tail);
tail = curr;
}
}
void printLL() {
Node *temp = head;
printf("Linked List: \n");
while(temp != NULL) {
printf("%d -> ", temp->value);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
pushHead(12);
pushHead(11);
printLL();
pushTail(13);
pushTail(14);
printLL();
popHead();
printLL();
popTail();
printLL();
return 0;
}