-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSLL.cpp
More file actions
103 lines (93 loc) · 1.82 KB
/
SLL.cpp
File metadata and controls
103 lines (93 loc) · 1.82 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
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <algorithm>
#define endl '\n'
#define null NULL
using namespace std;
struct Node {
int key;
struct Node * next;
};
struct Node * head = NULL;
struct Node * ptr;
void addNode (int key){
struct Node * temp = (struct Node *)malloc(sizeof(struct Node));
temp ->key = key;
temp->next = null;
if (!head) {
head = temp;
ptr = head;
}
else {
ptr->next = temp;
ptr = temp;
}
}
void popNode(int key){
Node * temp = head;
Node * prev = NULL;
while (temp){
if (temp->key == key){
if (!prev) {
head = temp->next;
free(temp);
}
else if (temp->next == NULL){
prev->next = NULL;
free(temp);
} else {
prev->next = temp->next;
free(temp);
}
}
prev = temp;
temp = temp->next;
}
}
void printNode(struct Node *root){
while (root != NULL){
cout << root->key << " ";
root = root->next;
}
cout <<endl;
}
void reverse(){
Node * prev = NULL;
Node * curr = head;
Node * next;
while (curr){
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
head = prev;
}
void reverseDouble(){
Node *temp = NULL;
Node *curr = head;
while(curr){
temp = curr->prev;
curr->prev = curr->next;
curr->next = temp;
temp = curr;
curr = curr->prev;
}
head = temp;
}
int main(){
for (int i = 0;i<20;i++){
addNode(i);
}
popNode(0);
popNode(7);
popNode(18);
popNode(19);
printNode(head);
reverse();
printNode(head);
}