-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink list reveresed.cpp
100 lines (91 loc) · 1.72 KB
/
link list reveresed.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
using namespace std;
struct node {int data ;
node *next;
} ;
class link
{
node *start ;
public:
link();
void input(int i=0);
void output();
~link();
void reve() ;
};
link::link()
{
start=nullptr ;
}
void link::input(int i)
{
node*temp =new node ;
temp->data=i ;
temp->next=NULL ;
if(start!=NULL)
{
temp->next=start ;
start =temp ;
}
else
{
temp->next=NULL;
start=temp ;
}
}
void link::output()
{
node *temp;
temp=start;
while(temp!=NULL)
{
cout<<temp->data << " ";
temp=temp->next ;
}
cout<<endl ;
}
link::~link()
{
while(start!=NULL)
{
node *temp =start ;
start=start->next ;
delete temp ;
}
cout<<"Done";
}
void link::reve()
{
cout<<"Reversed\n" ;
node *prev = NULL ;
node *p ;
p=start ;
while(p!=NULL)
{
node *temp=p->next;
p->next = prev;
prev = p;
p=temp ;
}
while(prev!=NULL)
{
cout<<prev->data<<" ";
prev=prev->next ;
}
cout<<endl ;
}
int main() {
link l;
l.input(4);
l.input(8) ;
l.input(7) ;
l.input(9) ;
l.input(1) ;
l.input(0) ;
l.input(2) ;
l.input(5) ;
l.input(3) ;
l.output();
l.reve() ;
return 0;
}