-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedList.h
More file actions
180 lines (168 loc) · 3.04 KB
/
Copy pathlinkedList.h
File metadata and controls
180 lines (168 loc) · 3.04 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>
#include <string.h>
using namespace std;
class emp
{
private:
char name[20];
int age;
float salary;
public:
emp(char *name = "", int age = 0, int salary = 0.0)
{
strcpy(this->name,name);
this->age = age;
this->salary = salary;
}
friend ostream& operator << (ostream& o, emp &e);
};
ostream& operator << (ostream& o, emp &e)
{
cout<<e.name<<" "<<e.age<<" "<<e.salary<<endl;
return o;
}
template <class T>
class linkedList
{
private:
struct node
{
T data;
node* link;
} *p;
public:
linkedList()
{
p = NULL;
}
~linkedList()
{
node *t, *ptr;
ptr = p;
while(ptr != NULL)
{
t = ptr;
ptr = ptr->link;
delete t;
}
}
void addHead(T num)
{
node *r;
r = new node;
r->data = num;
r->link = p;
p = r;
cout<<"Inserted."<<endl;
cout<<p->data<<endl;
}
void insertAt(int, T);
void addTail(T);
void deleteAt(int);
void del(T);
void display()
{
node *ptr = p;
while(ptr != NULL)
{
cout<<ptr->data;
if(ptr->link != NULL)
cout<<"->";
ptr = ptr->link;
}
cout<<endl;
}
int count();
};
template <class T>
void linkedList<T>::insertAt(int c, T num)
{
node *prev, *ptr;
ptr = p;
int count = 1;
if(c<=0)
{
cout<<"Invalid Postition entered."<<endl;
return;
}
while (count != c && ptr != NULL)
{
count++;
prev = ptr;
ptr = ptr->link;
}
if(c == count && ptr == NULL)
addTail(num);
else if (c == count)
{
node *r;
r = new node;
r->data = num;
r->link = ptr;
prev->link = r;
}
else
cout<<"Position beyond range."<<endl;
}
template <class T>
void linkedList<T>::addTail(T num)
{
node *prev, *r, *ptr;
ptr = p;
while(ptr != NULL)
{
prev = ptr;
ptr = ptr->link;
}
r = new node;
r->data = num;
r->link = NULL;
prev->link = r;
cout<<"Tail Added"<<endl;
}
template <class T>
void linkedList<T>::del(T num)
{
node *r,*prev, *ptr;
ptr = p;
while (ptr != NULL && ptr->data != num)
{
prev = ptr;
ptr = ptr->link;
}
if(ptr==p)
{
p = ptr->link;
}
if (ptr == NULL)
{
cout<<"Data doesn't exist."<<endl;
}
else
{
prev->link = ptr->link;
delete ptr;
cout<<"Deleted"<<endl;
}
}
template <class T>
void linkedList<T>::deleteAt(int num)
{
int count = 1;
node *prev, *ptr;
ptr = p;
while (ptr->link != NULL && count!=num)
{
prev = ptr;
ptr = ptr->link;
count++;
}
if (count != num || ptr->link == NULL)
cout<<"Element does not exist at that position."<<endl;
else
{
prev->link = ptr->link;
delete ptr;
cout<<"Deleted"<<endl;
}
}