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;
- node * prev;
- };
- struct DLL {
- node *head;
- void init() {
- head = NULL;
- }
- void insertBegin(int x) {
- node * new_node = new node;
- new_node->val = x;
- new_node->next = NULL;
- new_node->prev = NULL;
- if(head == NULL) {
- head = new_node;
- }
- else {
- new_node->next = head;
- head->prev = new_node;
- head = new_node;
- }
- }
- void insertEnd(int x) {
- node * new_node = new node;
- new_node->val = x;
- new_node->next = NULL;
- new_node->prev = NULL;
- if(head == NULL) {
- head = new_node;
- }
- else {
- node *tmp = head;
- while(tmp->next != NULL) {
- tmp = tmp->next;
- }
- tmp->next = new_node;
- new_node->prev = tmp;
- }
- }
- void insertAfterNode(int x, node *tmp) {
- if(tmp->next == NULL) {
- insertEnd(x);
- }
- else {
- node *new_node = new node;
- new_node->val = x;
- new_node->next = tmp->next;
- tmp->next->prev = new_node;
- new_node->prev = tmp;
- tmp->next = new_node;
- }
- }
- void insertBefore(int x, node *tmp) {
- if(tmp->prev == NULL) {
- insertBegin(x);
- }
- else {
- node *new_node = new node;
- new_node->val = x;
- new_node->prev = tmp->prev;
- tmp->prev->next = new_node;
- tmp->prev = new_node;
- new_node->next = tmp;
- }
- }
- void deleteBegin() {
- if(head != NULL) {
- if(head->next == NULL) {
- delete head;
- head = NULL;
- }
- else {
- node *tmp = head;
- head = head->next;
- head->prev = NULL;
- delete tmp;
- }
- }
- }
- void deleteEnd() {
- if(head != NULL) {
- if(head->next == NULL) {
- delete head;
- head = NULL;
- }
- else {
- node *tmp = head;
- while(tmp->next != NULL) {
- tmp = tmp->next;
- }
- tmp->prev->next = NULL;
- tmp->prev = NULL;
- delete tmp;
- }
- }
- }
- void deleteAll() {
- while(head != NULL) {
- deleteBegin();
- }
- }
- void print() {
- node * tmp = head, *last_node;
- while(tmp != NULL) {
- cout << tmp->val << " <--> ";
- last_node = tmp;
- tmp = tmp->next;
- }
- cout << endl;
- while(last_node != NULL) {
- cout << last_node->val << " <--> ";
- last_node = last_node->prev;
- }
- cout << endl;
- }
- };
- void sortiraj(DLL & dll) {
- node *tmp = dll.head;
- while(tmp != NULL) {
- node *tmp2 = tmp->next;
- while(tmp2 != NULL) {
- if(tmp->val > tmp2->val) {
- int x = tmp->val;
- tmp->val = tmp2->val;
- tmp2->val = x;
- }
- tmp2 = tmp2->next;
- }
- tmp = tmp->next;
- }
- }
- int main() {
- DLL dll;
- dll.init();
- int n;
- cin >> n;
- for(int i = 0; i < n; i++) {
- int x;
- cin >> x;
- dll.insertEnd(x);
- }
- sortiraj(dll);
- dll.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement