-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinear List.cpp
82 lines (74 loc) · 1.37 KB
/
Linear List.cpp
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
#include <iostream>
using namespace std;
struct node {
int info;
node *next;
};
typedef node *list;
void insertend (list &l, int data) {
node *p, *q;
p = new node;
p -> info = data;
p -> next = nullptr;
if (l == nullptr) {
l = p;
}
else {
q = l;
while (q -> next != nullptr) {
q = q -> next;
}
q -> next = q;
}
}
void insertafterp (node *p, int data) {
if (p != nullptr) {
node *q = new node;
q -> info = data;
q -> next = p -> next;
p ->next = q;
}
}
void deleteafterp (node *p) {
if ((p != nullptr) && (p -> next != nullptr)) {
node *q = p -> next;
p -> next = q -> next;
delete q;
}
}
node *search (list l, int data) {
node *p;
for (p = l; p != nullptr; p = p -> next) {
if (p -> info == data) {
return p;
}
}
return nullptr;
}
void concat (list &l1, list l2) {
node *p;
if (l2 == nullptr) {
return;
}
if (l1 == nullptr) {
l1 = l2;
}
else {
p = l1;
while (p -> next != nullptr) {
p = p -> next;
}
p -> next = l2;
}
}
void reverse (list &l) {
node *p, *q;
q = nullptr;
while (l != nullptr) {
p = l;
l = p -> next;
p -> next = q;
q = p;
}
l = q;
}