Repository navigation
Expand file tree
/
Copy path7_9_reverse_list.cpp
More file actions
110 lines (104 loc) · 2.26 KB
/
Copy path7_9_reverse_list.cpp
File metadata and controls
110 lines (104 loc) · 2.26 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
#include <iostream>
using namespace std;
template <class T>
class LinkedList{
struct node{
T x;
node *next;
};
public:
LinkedList(){
head = NULL;
curr = NULL;
}
node* getHead(){
return head;
}
node* getCurrent(){
return curr;
}
void addNode(struct node* b){
b->next = head;
head = b;
if(curr == NULL)
curr = head;
}
void addValue(T a){
node *n = new node;
n->x = a;
n->next = head;
head = n;
if(curr == NULL)
curr = head;
}
void rewind(){
curr = head;
}
void next(){
if( curr != NULL )
curr = curr->next;
}
int getValue(){
if( curr != NULL )
return curr->x;
return 0;
}
int hasValue(){
return ( curr != NULL ? true : false );
}
void reverseList(){
rewind();
node *ahead, *temp;
ahead = head->next;
head->next = NULL;
while(ahead->next!=NULL)
{
temp = ahead->next;
ahead->next = curr;
curr = ahead;
ahead = temp;
}
ahead->next = curr;
head = ahead;
}
private:
node *head;
node *curr;
};
int main(){
LinkedList<int> L;
L.addValue(12);
L.addValue(11);
L.addValue(10);
L.addValue(9);
L.addValue(8);
L.addValue(7);
L.addValue(6);
L.addValue(5);
L.addValue(4);
L.addValue(3);
L.addValue(2);
L.addValue(1);
L.addValue(0);
L.rewind();
while(L.hasValue())
{
cout << "BEFORE----Value: " << L.getValue();
if(L.getValue()/10 < 1)
cout << " ";
cout << " Pointer: " << L.getCurrent() << endl;
L.next();
}
cout << "\n----------------------------------------------------\n" << endl;
L.reverseList();
L.rewind();
while(L.hasValue())
{
cout << "AFTER-----Value: " << L.getValue();
if(L.getValue()/10 < 1)
cout << " ";
cout << " Pointer: " << L.getCurrent() << endl;
L.next();
}
return 0;
}