Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- 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.insertLast(105);
- slcl.insertLast(108);
- slcl.insertLast(109);
- slcl.insertLast(150);
- slcl.insertLast(240);
- slcl.insertLast(202);
- slcl.insertLast(300);
- node *tmp = slcl.head;
- while(tmp->val != 150) {
- tmp = tmp->next;
- }
- slcl.tail->next = tmp;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement