#include
using namespace std;
struct node {
int val;
node * next;
};
struct SLCL {
node *head, *tail;
void init() {
head = NULL;
tail = NULL;
}
void insertFirst(int x) {
node * new_node = new node;
new_node->val = x;
if(head == NULL) {
head = new_node;
head->next = head;
tail = head;
}
else {
new_node->next = head;
head = new_node;
tail->next = head;
}
}
void insertLast(int x) {
node * new_node = new node;
new_node->val = x;
if(head == NULL) {
head = new_node;
head->next = head;
tail = head;
}
else {
tail->next = new_node;
tail = new_node;
tail->next = head;
}
}
void deleteFirst() {
if(head != NULL) {
if(head->next == head) {
delete head;
head = NULL;
tail = NULL;
}
else {
node *tmp = head;
head = head->next;
tail->next = head;
delete tmp;
}
}
}
void deleteLast() {
if(head != NULL) {
if(head -> next == head) {
delete head;
head = NULL;
tail = NULL;
}
else {
node *tmp = head;
while(tmp->next != tail) {
tmp = tmp->next;
}
tail = tmp;
delete tmp->next;
tail->next = head;
}
}
}
void print() {
node *tmp = head;
while(tmp != tail) {
cout << tmp->val << "--> ";
tmp = tmp->next;
}
cout << tmp->val << endl;
}
};
int main() {
SLCL slcl;
slcl.init();
slcl.insertFirst(1);
slcl.insertFirst(2);
slcl.insertLast(3);
slcl.insertLast(4);
slcl.print();
slcl.deleteFirst();
slcl.print();
slcl.deleteLast();
slcl.print();
return 0;
}