Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct node {
- int info;
- node * next;
- node * prev;
- };
- struct DLCL {
- node * head, * tail;
- void init() {
- head = NULL;
- tail = NULL;
- }
- void insertFront(int x) {
- node * new_node = new node;
- new_node->info = x;
- new_node->next = NULL;
- new_node->prev = NULL;
- if(head == NULL) {
- head = new_node;
- tail = head;
- head->next = tail;
- tail->prev = head;
- }
- else {
- new_node->next = head;
- head->prev = new_node;
- head = new_node;
- tail->next = head;
- head->prev = tail;
- }
- }
- void insertEnd(int x) {
- node * new_node = new node;
- new_node->info = x;
- new_node->next = NULL;
- new_node->prev = NULL;
- if(head == NULL) {
- head = new_node;
- tail = head;
- head->prev = tail;
- tail->next = head;
- }
- else {
- tail->next = new_node;
- new_node->prev = tail;
- tail = new_node;
- tail->next = head;
- head->prev = tail;
- }
- }
- void deleteFront() {
- if(head != NULL) {
- if(head->next == head) {
- delete head;
- head = NULL;
- tail = NULL;
- }
- else {
- head = head->next;
- delete head->prev;
- head->prev = tail;
- tail->next = head;
- }
- }
- }
- void deleteEnd() {
- if(head != NULL) {
- if(head->next == head) {
- delete head;
- head = NULL;
- tail = NULL;
- }
- else {
- tail = tail->prev;
- delete tail->next;
- tail->next = head;
- head->prev = tail;
- }
- }
- }
- void deleteNode(int x) {
- if(head != NULL) {
- if(head->info == x) {
- deleteFront();
- }
- else {
- node * tmp = head;
- while(tmp->info != x) {
- tmp = tmp->next;
- }
- if(tmp->next == head) {
- deleteEnd();
- }
- else {
- tmp->prev->next = tmp->next;
- tmp->next->prev = tmp->prev;
- delete tmp;
- }
- }
- }
- }
- void deleteNode(node * x) {
- if(head != NULL) {
- if(head == x) {
- deleteFront();
- }
- else {
- node * tmp = head;
- while(tmp != x) {
- tmp = tmp->next;
- }
- if(tmp->next == head) {
- deleteEnd();
- }
- else {
- tmp->prev->next = tmp->next;
- tmp->next->prev = tmp->prev;
- delete tmp;
- }
- }
- }
- }
- void inserAfter(int x, node * after) {
- node * new_node = new node;
- new_node->info = x;
- new_node->next = NULL;
- new_node->prev = NULL;
- if(after != NULL) {
- if(after == head) {
- insertFront(x);
- }
- else if(after == tail) {
- insertEnd(x);
- }
- else {
- new_node->next = after->next;
- new_node->prev = after;
- after->next = new_node;
- new_node->next->prev = new_node;
- }
- }
- }
- void print() {
- if(head != NULL) {
- node * tmp = head;
- while(tmp != tail) {
- cout << tmp->info << "-->";
- tmp = tmp->next;
- }
- cout << tmp->info<<"-->";
- tmp = tmp->next;
- while(tmp != tail) {
- cout << tmp->info << "-->";
- tmp = tmp->next;
- }
- cout << tmp->info << endl;
- while(tmp != head) {
- cout << tmp->info << "<--";
- tmp = tmp->prev;
- }
- cout << tmp->info << "<--";
- tmp = tmp->prev;
- while(tmp != head) {
- cout << tmp->info << "<--";
- tmp = tmp->prev;
- }
- cout << tmp->info << endl;
- }
- }
- };
- int main() {
- DLCL dll;
- dll.init();
- dll.insertFront(1);
- dll.insertFront(2);
- dll.insertFront(3);
- dll.insertEnd(4);
- dll.insertEnd(5);
- dll.insertEnd(6);
- dll.print();
- node * tmp = dll.head->next;
- dll.inserAfter(7, tmp);
- dll.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement