#include <bits/stdc++.h>
using namespace std;
class LinkedCircleList {
private:
struct node {
int val;
node *next;
node() {}
node(int x, node *next) {
this->val = x;
this->next = next;
}
};
int size;
node *head;
public:
LinkedCircleList() {
this->size = 0;
head = new node;
head->next = head;
}
~LinkedCircleList() {
while (head!=head->next) {
delTail();
}
delete head;
}
void insert(int index, int x) {
if (index<0 || index>size) {
cout << "the index is invalid!" << endl;
return;
}
node *p = head;
for (int i=0; i<index; ++i) p = p->next;
node *q = new node(x, p->next);
p->next = q;
size ++;
}
void insertHead(int x) {insert(0, x);}
void insertTail(int x) {insert(size, x);}
int del(int index) {
if (index<0 || index>=size) {
cout << "the index is invalid!" << endl;
return -1;
}
if (head->next == head) {
cout << "the LinkedCircleList is null!" << endl;
return -1;
}
node *p = head;
for (int i=0; i<index; ++i) p = p->next;
node *del = p->next;
p->next = del->next;
int res = del->val;
delete del;
size --;
return res;
}
int delTail() {del(size-1);}
int delHead() {del(0);}
int get(int index) {
if (index < 0 || index>=size) {
cout << "the index is invalid!" << endl;
return -1;
}
if (head->next == head) {
cout << "the LinkedCircleList is null!" << endl;
return -1;
}
node *p = head->next;
for (int i=0; i<index; ++i) p = p->next;
return p->val;
}
int getHead() {get(0);}
int getTail() {get(size-1);}
void show() {
node *p = head->next;
while (p!=head) {
cout << p->val << " ";
p = p->next;
}
cout << endl;
}
bool isEmpty() {return head->next == head;}
};
int main() {
int a[] = {1};
int n = sizeof(a)/sizeof(int);
LinkedCircleList list = LinkedCircleList();
for (int i=0; i<n; ++i) list.insertTail(a[i]);
list.show();
cout << list.getHead() << endl;
cout << list.getTail() << endl;
return 0;
}