-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSequence.cpp
164 lines (142 loc) · 2.93 KB
/
Sequence.cpp
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
#include<iostream>
#include<string>
using namespace std;
struct Node{
private:
Node* pre;
Node* next;
int data;
public:
Node(){
pre= nullptr;
next= nullptr;
data=0;
}
friend class Iterator;
friend class Sequence;
};
class Iterator{
Node* node;
public:
Iterator(){
node= nullptr;
}
Iterator(Node* n) {
node = n;
}
Iterator* operator++(){
node=node->next;
return this;
}
Iterator* operator--(){
node=node->pre;
return this;
}
friend class Sequence;
};
class Sequence{
private:
Node* head;
Node* tail;
int size;
public:
Sequence(){
head=new Node;
tail=new Node;
head->next=tail;
tail->pre=head;
size=0;
}
Node* begin(){
return head->next;
}
Node* end(){
return tail;
}
void insert(Iterator &p,int e){
Node* newNode=new Node;
newNode->data=e;
p.node->pre->next=newNode;
newNode->pre=p.node->pre;
newNode->next=p.node;
p.node->pre=newNode;
size++;
}
void erase(Iterator &p){
if(size==0){
cout<<"Empty"<<endl;
}
else{
Node* deleteNode=p.node;
deleteNode->pre->next=deleteNode->next;
deleteNode->next->pre=deleteNode->pre;
p.node=begin();
delete deleteNode;
size--;
}
}
void print(){
if(size==0)
cout<<"Empty"<<endl;
else{
Node* curNode=head->next;
for(int i=0;i<size;i++){
cout<<curNode->data<<" ";
curNode=curNode->next;
}
cout<<endl;
}
}
void find(int e){
if(size==0)
cout<<"Empty"<<endl;
else{
Node* curNode=head->next;
int index=0;
for(int i=0;i<size;i++){
if(curNode->data==e){
cout<<index<<endl;
break;
}
else{
curNode=curNode->next;
index++;
}
}
if(index==size)
cout<<-1<<endl;
}
}
};
int main(){
int m;
cin>>m;
Sequence seq;
Iterator p = Iterator(seq.begin());
for(int i=0;i<m;i++){
string opr;
cin>>opr;
if(opr=="begin")
p=seq.begin();
else if(opr=="end")
p=seq.end();
else if(opr=="insert"){
int a;
cin>>a;
seq.insert(p,a);
}
else if(opr=="erase")
seq.erase(p);
else if (opr=="++")
++p;
else if (opr=="--")
--p;
else if (opr=="print")
seq.print();
else if (opr=="find") {
int a;
cin >> a;
seq.find(a);
}
}
}