forked from gods-mack/Workspace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.cpp
More file actions
130 lines (102 loc) · 1.51 KB
/
list.cpp
File metadata and controls
130 lines (102 loc) · 1.51 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
111
112
113
114
115
116
#include<iostream>
using namespace std;
struct node
{ int data;
node *next;
};
node *h;
class list
{ node *t;
public:
list() { t=NULL; h=NULL; }
void add(int);
void add_front(int);
void add_back(int);
void add_after(int,int);
void del_front();
int reverse(node*);
void print();
};
void list::add(int x)
{
node *n=new node;
n->data=x;
if(h==NULL)
{ h=n; t=n; }
else
{
t->next=n;
t= n;
}
}
void list::add_front(int x)
{
node *n=new node;
n->data=x;
n->next=h;
h=n;
}
void list::add_back(int x)
{
node *n=new node;
n->data=x;
t->next=n;
t=n;
}
void list::add_after(int srch,int item)
{ node *tmp; tmp=h;
while(tmp!=NULL)
{
if(tmp->data==srch)
{ node *n=new node;
n->data=item;
n->next=tmp->next;
tmp->next=n;
}
tmp=tmp->next;
}
}
void list::del_front()
{
node *tmp;
tmp=h;
h=h->next;
delete tmp;
}
int list::reverse(struct node *p)
{
if(p->next==NULL)
{ h=p;
return p->data;
}
reverse(p->next);
struct node *q=p->next;
q->next=p;
p->next=NULL;
}
//Do something
void list::print()
{
node *tmp=new node;
tmp=h;
while(tmp!=NULL)
{ cout<<tmp->data<< " ";
tmp=tmp->next;
}
}
int main()
{
list a;
a.add(4);
a.add(45);
a.add(7);
a.add_front(73);
a.add_back(67);
a.add(3);
a.add_back(56);
a.add(26736);
//a.add_after(56,100);
//a.del_front();
a.reverse(h);
a.print();
}