"Data Structure" in C + + implementation of the double-linked list of various operations (including header deletion, tail delete, insert, reverse, destroy, empty, etc.)

Source: Internet
Author: User

"Data Structure" with C + + implementation of the various operations of the double-linked list (including header deletion, tail deletion, insertion, reverse order, destruction, emptying, etc.)//header file #ifndef _list_h#define _list_h#include<iostream>using namespace Std;template<class type>class dlist;template<class Type>class listnode{friend class DList< Type>;p ublic:listnode ():d ATA (Type ()), Next (null), Prio (null) {}listnode (type D, listnode<type> *n = NULL, listnode<type> *m=null):d ata (d), Next (N), Prio (m) {}~listnode () {}private:type data; Listnode<type> *next; Listnode<type> *prio;}; Template<class Type>class dlist{public:dlist () {first = last = Buynode ();} ~dlist () {Destroy ();} Public:void push_back (const Type &x)//tail plug ' {listnode<type> *s = Buynode (x); last->next = S;S-&GT;PR IO = Last;last = s;first->data++;} void Push_front (const Type &x)//head interpolation ' {if (First->data = = 0) {push_back (x); return;} listnode<type> *s = Buynode (x); s->next = First->next;first->next->prio = S;first->next = s;s-> Prio = First;last = s;for (int i = 0; i < first->data; i++) {last = Last->next;} first->data++;} void Pop_back ()//tail-delete ' {if (First->data = = 0) return; listnode<type> *p = Last;last = Last->prio;last->next = Null;delete p;first->data--;} void Pop_front ()//Header delete ' {if (First->data = = 0) {return;} else{listnode<type> *s = first->next;if (First->data = = 1) {Delete S;last = First;last->next = NULL;} Else{first->next = S->next;s->next->prio = First;delete s;} first->data--;}} void Insert_val (const Type &x)//By value Insert ' {listnode <Type> *p = Buynode (x); ListNode <Type> *s = first;if (First->data = = 0) {push_back (x); return;} while (s->next->data<x&&s->next->next! = NULL) {s = s->next;} if (S->next->next = = NULL) {if (s->next->data>x) {p->next = S->next;s->next->prio = p;s-> Next = P;p->prio = s;first->data++;} Elsepush_back (x);} Else{p->next = S->next;s->next->prio = P;s-> next = P;p->prio = s;first->data++;}} void Show_list ()//display ' {if (First->data = = 0) {cout << ' NULL ' << Endl;return;} listnode<type> *q = first->next;while (q! = NULL) {cout << q->data << ", q = Q->next;} cout << "NULL first->data=" << first->data << "Last->data=" <<last->data<< " last->next= "<< last->next <<" "<< Endl;} void sort ()//Sort ' {if (First->data = = 0) {return;} listnode<type> *p = first->next; listnode<type> *q = P->next;p->next = Null;last = P;first->data = 1; Listnode<type> *r;while (q! = NULL) {insert_val (q->data); r = Q;q = Q->next;delete r;}} listnode<type>* Find (const Type &x)//Find ' {listnode<type> *s;for (s = first->next; s! = NU LL; s = s->next) {if (S->data = = x) {return s;}} return NULL;} int length ()//Seek length ' {return (First->data);} void Delete_val (const Type &x)//By value delete ' {if (First->data = = 0) {cout << "no number found:" << Endl;return;} Else{listnode<type> *p = Find (x), if (p = = NULL) {cout << "no number found:" << Endl;return;} listnode<type> *q = First;while (q->next! = p) {q = Q->next;} if (Q->next->next = = NULL) {pop_back ();} Else{q->next = P->next;p->next->prio = Q;delete p;first->data--;}}} void clear ()//clear ' {if (First->data = = 0) return;while (first->next! = NULL) {pop_back ();}} void Resver ()//reverse order ' {if (First->data = = 0) {return;} listnode<type> *p = first->next; listnode<type> *q = P->next;p->next = Null;last = P;first->data = 1; Listnode<type> *r;while (q! = NULL) {Push_front (q->data); r = Q;q = Q->next;delete r;}} void Quit_system (int &a)//exit ' {clear (); a = 0;} void Destroy ()//Destroy ' {clear ();d elete first;} protected:listnode<type>* Buynode (Type x = Type ()) {listnode<type> *p = new listnode<type> (x); return p;} Private:listnode<type> *first; Listnode<type> *last;};/ /main function #endif#include "DList.h" void Main () {dlist<int> mylist;int select = 1;int item;//int pos;while (select) {cout << "**************************************" << endl;cout << "* [1] show_list [2] push_front *" &lt ;< endl;cout << "* [3] push_back [4] Pop_front *" << endl;cout << "* [5] pop_back [6] I            Nsert_val * "<< endl;cout <<" * [7] Find [8] delete_val * "<< endl;cout <<" * [9]length [[] Clear * "<< endl;cout <<" * [one] quit_system [] Destroy * "<< endl;cout <& Lt "* [] resver [] sort *" << endl;cout << "**************************************" << End L;cout << "Please select:>"; Cin >> Select;switch (SELECT) {case 1:mylist.show_list (); Break;case 2:cout << " Please enter a value to insert (-1 end):> ";" while (CIN >> item, Item! =-1) {Mylist.push_front (item);} Break;case 3:cout << "Please enter the value to insert (-1 end):>"; while (CIN >> item, Item! =-1) {Mylist.push_back (item);} Break;case 4:mylist.pop_front (); Break;case 5:mylist.pop_back (); Break;case 6:cout << "Please enter the value to insert:>"; Cin >> Item;mylist.insert_val (Item); Break;case 7:cout << "Please enter the number to find:"; Cin >> item;cout << "The number of addresses is:"; cout << Mylist.find (Item) < < endl;break;case 8:cout << "Please enter the value to delete:>"; Cin >> Item;mylist.delete_val (Item); Break;case 9:cout < < "This sequential table length is:" << mylist.length () << endl;break;case 10:mylist.clear (); Break;case 11:mylist.quit_system ( Select); Break;case 12:mylist.destroy (); case 13:mylist.resver (); break;case 14:mylist.sort (); break;default:break;}} return;}  

"Data Structure" in C + + implementation of the double-linked list of various operations (including header deletion, tail delete, insert, reverse, destroy, empty, etc.)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.