-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseSLL.cpp
More file actions
88 lines (78 loc) · 1.36 KB
/
reverseSLL.cpp
File metadata and controls
88 lines (78 loc) · 1.36 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
//reverse a singly linked list
#include <iostream>
using namespace std;
struct node
{
int data;
node *link;
} * p;
class linkedlist
{
public:
linkedlist()
{
p = NULL;
}
void append(int num)
{
node *temp, *r;
temp = new node;
temp->data = num;
temp->link = NULL;
if (p == NULL)
p = temp;
else
{
r = p;
while (r->link != NULL)
r = r->link;
r->link = temp;
}
}
void reverse()
{
node *current = p;
node *link = NULL, *prev = NULL;
while (current != NULL)
{
link = current->link;
current->link = prev;
prev = current;
current = link;
}
p = prev;
}
void display()
{
node *temp = p;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->link;
}
cout << endl;
}
~linkedlist()
{
node *temp;
while (temp != NULL)
{
temp = p->link;
delete p;
p = temp;
}
}
};
int main()
{
linkedlist l;
l.append(60);
l.append(50);
l.append(40);
l.append(30);
l.append(20);
l.append(10);
l.display();
l.reverse();
l.display();
}