-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.h
127 lines (115 loc) · 1.58 KB
/
list.h
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
117
118
119
120
121
122
123
124
125
126
127
using namespace std;
template<class R>
struct node{
R val;
node* next;
};
template<class T>
class list{
node<T>* first;
int size;
public:
list(){
first=NULL;
size=0;
}
int getsize()
{
return size;
}
node<T>* top_value()
{
return first;
}
void insert(T val)
{
node<T>* temp = new node<T>;
temp->val=val;
temp->next=NULL;
if(first==NULL)
{
first=temp;
size++;
}
else{
node<T>* p=first;
while(p->next!=NULL)
{
p=p->next;
}
p->next=temp;
size++;
}
}
void insert_at_head(T val)
{
node<T>* temp=new node<T>;
temp->val=val;
temp->next=first;
first=temp;
size++;
}
void remove() {
node<T>* p = first;
if (p == NULL) {
return;
}
node<T>* q = NULL;
if (p->next != NULL) {
while (p->next != NULL) {
q = p;
p = p->next;
}
if (q != NULL) {
q->next = NULL;
}
delete p;
} else {
delete p;
first = NULL;
}
size--;
}
void remove_at_head()
{
node<T>* p=first;
if(first!=NULL)
first=first->next;
if (p!=NULL)
delete p;
size--;
}
int search(T val)
{
int count=-1;
node<T>* p=first;
while(p!=NULL)
{
count++;
if(p->val==val)
return count;
p=p->next;
}
return count;
}
T operator[](T val)
{
node<T>* p=first;
for(int i=0;i<val;i++)
{
if(p!=NULL)
p=p->next;
}
return p->val;;
}
void display()
{
node<T>* p=first;
while(p!=NULL)
{
std::cout<<p->val<<" ";
p=p->next;
}
cout<<endl;
}
};