-
Notifications
You must be signed in to change notification settings - Fork 0
/
single_linked_list.h
75 lines (67 loc) · 1.41 KB
/
single_linked_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
#ifndef SINGLE_LINKED_LIST_H
#define SINGLE_LINKED_LIST_H
#include <iostream>
class list_element{
public:
list_element(int n=0, list_element *next=nullptr):n(n),next(next){};
int n;
list_element *next;
};
class singlelist{
public:
singlelist(list_element *head=nullptr, list_element *cursor=nullptr):head(head),cursor(cursor){};
bool is_Empty(){
if (head==nullptr)
return 1;
else
return 0;
};
void insert_before(int n){
if (head==nullptr)
cursor=head=new list_element(n,head);
else
head=new list_element(n,head);
};
void insert_after(int n){
if (head==nullptr)
cursor=head=new list_element(n,head);
else {
while(cursor->next!=nullptr)
cursor=cursor->next;
cursor->next=new list_element(n,nullptr);
cursor=cursor->next;
}
};
list_element *FindPrevious(int n){
//single list should find the previous element
list_element *p=head;
while (p->next!=nullptr && p->next->n!=n)
p=p->next;
return p;
};
void Delete(int n){
if (head->n==n){
list_element *headtemp=head->next;
delete head;
head=headtemp;
}
else{
list_element *position=FindPrevious(n);
position->next=position->next->next;
delete position;
}
};
void disp(){
list_element *t=head;
std::cout<<"This is the list: "<<std::endl;
while(t!=nullptr){
std::cout<<t->n<<" ";
t=t->next;
}
std::cout<<std::endl;
}
private:
list_element *head;
list_element *cursor;
};
#endif