-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedList.cpp
More file actions
76 lines (68 loc) · 1.15 KB
/
linkedList.cpp
File metadata and controls
76 lines (68 loc) · 1.15 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
//linkedList.cpp - linkedList class specification
linkedList::linkedList(){
l = NULL;
}
//enter a value in the front
void linkedList::enterFront(int x){
ptrType p = new node;
p->info = x;
p->next = l;
l = p;
}
//enter a value in the rear
void linkedList::enterRear(int x){
if (l == NULL){
enterFront(x);
return;
}
ptrType ptr = l;
while (ptr->next != NULL)
ptr = ptr->next;
ptrType p = new node;
p->info = x;
p->next = NULL;
ptr->next = p;
}
//delete front node
void linkedList::delFront(){
ptrType ptr = l;
l = l->next;
delete ptr;
}
//delete last node
void linkedList::delRear(){
ptrType ptr = l;
ptrType p = l;
while (ptr->next != NULL)
ptr = ptr->next;
while (p->next != ptr)
p = p->next;
delete ptr;
p->next = NULL;
}
//return count of node elements
int linkedList::getCount(){
int i = 0;
ptrType p = l;
while (p != NULL){
p = p->next;
i++;
}
return i;
}
//get the nthelement
int linkedList::nthElement(int n){
ptrType p = l;
for (int i = 1; i < n; i++)
p = p->next;
int x = p->info;
return x;
}
void linkedList::printList(){
ptrType p = l;
while (p != NULL){
cout<<p->info;
cout<<endl;
p = p->next;
}
}