Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct node {
- int info;
- node *next;
- };
- struct SinglyLinkedCircularList {
- node * head;
- node * tail;
- void init() {
- head = NULL;
- tail = NULL;
- }
- void insertFront(int x) {
- node * new_node = new node;
- new_node->info = x;
- new_node->next = NULL;
- if(head == NULL) {
- head = new_node;
- tail = head;
- tail->next = head;
- }
- else {
- new_node->next = head;
- head = new_node;
- tail->next = head;
- }
- }
- void insertBack(int x) {
- node * new_node = new node;
- new_node->info = x;
- new_node->next = NULL;
- if(head == NULL) {
- head = new_node;
- tail = head;
- tail->next = head;
- }
- else {
- tail->next = new_node;
- tail = new_node;
- tail->next = head;
- }
- }
- void deleteFront() {
- if(head != NULL) {
- if(head->next == head) {
- delete head;
- head = NULL;
- tail = NULL;
- }
- else {
- node * tmp = head;
- head = head->next;
- delete tmp;
- tail->next = head;
- }
- }
- }
- void deleteBack() {
- if(head != NULL) {
- if(head->next == head) {
- delete head;
- head = NULL;
- tail = NULL;
- }
- else {
- node * tmp = head;
- while(tmp->next != tail) {
- tmp = tmp->next;
- }
- delete tail;
- tail = tmp;
- tail->next = head;
- }
- }
- }
- void deleteNode(int x) {
- if(head != NULL) {
- if(head->info == x) {
- deleteFront();
- }
- else {
- node * tmp = head;
- node * prev = NULL;
- while(tmp != NULL && tmp->info != x) {
- prev = tmp;
- tmp = tmp->next;
- }
- if(tmp == tail) {
- deleteBack();
- }
- else {
- prev->next = tmp->next;
- delete tmp;
- }
- }
- }
- }
- void deleteNode(node * x) {
- if(head != NULL) {
- if(head == x) {
- deleteFront();
- }
- else {
- node * tmp = head;
- node * prev = NULL;
- while(tmp != NULL && tmp != x) {
- prev = tmp;
- tmp = tmp->next;
- }
- if(tmp == tail) {
- deleteBack();
- }
- else {
- prev->next = tmp->next;
- delete tmp;
- }
- }
- }
- }
- void print() {
- node * tmp = head;
- for(int i = 0; i < 10; i++) {
- cout << tmp->info << " --> ";
- tmp = tmp->next;
- }
- cout << endl;
- }
- };
- void popravi(SinglyLinkedCircularList slcl) {
- node * losh = slcl.tail->next;
- SinglyLinkedCircularList prva;
- prva.init();
- SinglyLinkedCircularList vtora;
- vtora.init();
- node * tmp = slcl.head;
- while(tmp != NULL and tmp != losh) {
- prva.insertBack(tmp->info);
- tmp = tmp->next;
- }
- while(losh != slcl.tail) {
- vtora.insertBack(losh->info);
- losh = losh->next;
- }
- vtora.insertBack(slcl.tail->info);
- prva.print();
- vtora.print();
- }
- int main() {
- SinglyLinkedCircularList slcl;
- slcl.init();
- int n;
- cin >> n;
- for(int i = 0; i < n; i++) {
- int x;
- cin >> x;
- slcl.insertBack(x);
- }
- slcl.tail->next = slcl.head->next->next->next->next->next->next;
- cout << slcl.tail->next->info << endl;;
- popravi(slcl);
- return 0;
- }
- // 108 105 110 107 101 100 108 111 111 112
Add Comment
Please, Sign In to add comment