This repository has been archived by the owner on Dec 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircular.cpp
224 lines (223 loc) · 7.09 KB
/
Circular.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include<iostream>
using namespace std;
class node{
friend class list;
private:
node* back;
node* next;
int data;
public:
void setdata(int a){
data=a;
}
int getdata(){
return this->data;
}
};
class list{
private:
node* head;
node* tail;
node*current;
node*currrrr; //To store the actual previous state of the current pointer.
int index; //To store the position of where the counter was in an arry form.
int counter; //To store the size of the List. Used in back function to make index=counter
int space; //To store the spaces required to print out '^'
public:
list(){
head=NULL;
index=0;
counter=0;
space=0;
}
void insert (int a){
node* temp= new node;
if (head==NULL){
temp->setdata(a);
this->head=temp;
this->tail=temp;
temp->back=this->head;
temp->next=this->tail;
counter++;
}
else{
temp->setdata(a);
tail->next=temp;
temp->back=tail;
tail=temp;
tail->next=head;
head->back=tail;
counter++;
}
}
void print(){
current=head;
cout<<"[";
while(current!=tail){
cout<<current->data<<",";
current=current->next;
}
cout<<current->data<<"]";
}
//The following functions are only used for calculating the spaces required
//to print out ^ character to the selected number.
int Length_Of_Number(int num){//This calculates the length of a number
int count=0;
while(num>=10){
num=num/10;
++count;
}
return ++count;
}
int spaces(){//This calculates the number of spaces required.
current=head;
while(current!=currrrr){
space=space+Length_Of_Number(current->data);
current=current->next;
}
return space+index;
}
void print_position(){//This Prints out the '^' to point out to the currently selected number
currrrr=current;
print();
cout<<endl;
int comeone= spaces();
for(int i=0;i<=comeone;i++){
cout<<" ";
}
cout<<"^\n-----------------------"<<endl;
current=currrrr;
space=0;
}
//End of Calculating spaces Function
void forward(){
if(current==tail){
index=0;
current=current->next;
cout<<"----------------------\nPosition: "<<index;
cout<<"\nData: "<<current->getdata()<<endl;
}
else{
current=current->next;
index=index+1;
cout<<"----------------------\nPosition: "<<index;
cout<<"\nData: "<<current->getdata()<<endl;
}
print_position();
}
void back(){
if(current==head){
index=counter;
current=head->back;
index=index-1;
cout<<"----------------------\nPosition: "<<index;
cout<<"\nData: "<<current->getdata()<<endl;
}
else{
current=current->back;
index=index-1;
cout<<"----------------------\nPosition: "<<index;
cout<<"\nData: "<<current->getdata()<<endl;
}
print_position();
}
void del(){
if(tail==current){
tail=current->back;
index=0;
}
if(head==current)
head=current->next;
current->next->back=current->back;
current->back->next=current->next;
currrrr=current->next;
delete current;
current=currrrr;
}
void del(int pos){
current=head;
for(int i=1;i<=pos;i++)
current=current->next;
del();
}
bool isempty(){
if (head==NULL)
return true;
else
return false;
}
void Display_List_Options(){
abc:
cout<<"Select from the following options:\n1) Insert data.\n2) Change Currently Selected Data(Forward/Backwards)?\n3) Print the list\n4) Delete item\n5) Quit"<<endl;
int option;
cin>>option;
while(option!=5){
switch (option){
case 1:{
int num;
string str;
cout<<"Enter Numbers into the List. Type a character to stop:\n";
while(cin>>num){
insert(num);
cout<<"Printing The New List:\n";
print();
cout<<" +-->";
}
cin.clear();
getline(cin, str);
};
break;
case 2:{
if(isempty()){
cout<<"The List is Empty Broooooooo!"<<endl;
break;
}
cout<<"\nF = Forward\nB = Backwards.\nAny other character = exit\n";
char op='o';
while (op!='n'){
cin>>op;
if(op=='F'||op=='f')
forward();
else if(op=='B'||op=='b')
back();
}
break;
}
case 3:
if(!isempty())
print_position();
else
cout<<"The List is Empty Broooooooo!"<<endl;
break;
case 4:
if(isempty()){
cout<<"The List is Empty Broooooooo!"<<endl;
break;
}
char op;
cout<<"Delete from currently selected data(D) or from an index(I)?: ";
cin>>op;
if(op=='D'||op=='d')
del();
else if(op=='I'||op=='i'){
cout<<"Enter Index";
int i;
cin>>i;
del(i);
}
else
cout<<"Incorrent option. Try Again."<<endl;
break;
default:
cout<< "Your selection must be between 1 and 5!\n";
}
goto abc;
}
}
};
int main(){
list L;
cout<<"This is a circular list."<<endl;
L.Display_List_Options();
return 0;
}