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

Source: Internet
Author: User

"Data Structure" with C + + implementation of a single cycle linked list of various operations (including header delete, tail delete, insert, reverse, destroy, empty, etc.)//header file #ifndef _cdlist_h#define _cdlist_h#include<iostream> Using namespace Std;template<class type>class cdlist;template<class Type>class Listnode{friend class Cdlist<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 cdlist{public:cdlist () {first = last = Buynode (); last->next = First;first->prio = Last;} ~cdlist () {Destroy ();} Public:void push_back (const Type &x)//tail plug ' {listnode<type> *s = Buynode (x); last->next = S;S-&GT;PR IO = Last;s->next = First;first->prio = S;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;first->data++;} void Pop_back ()//tail-delete ' {if (First->data = = 0) return; listnode<type> *p = Last;last = Last->prio;last->next = First;first->prio = Last;delete p;first-> data--;} void Pop_front ()//Header delete ' {if (First->data = = 0) {return;} else{listnode<type> *s = first->next;if (First->data = = 1) {last = First;last->next = First;first->prio = Last;} 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! = first) {s = s->next;} if (S->next->next = = first) {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! = first) {cout << q->data << ", q = Q->next;} cout << "NULL first->data=" << first->data << "Last->next->data=" << last->next- >data << "Last->data=" << last->data << "First->prio->data=" << first->prio- >data << "" << Endl;} void sort ()//Sort ' {if (First->data = = 0) {return;} listnode<type> *p = first->next; listnode<type> *q = P->next;p->next = First;first->prio = P;last = P;first->data = 1; Listnode<type> *r;while (q! = first) {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! = first; 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 = = first) {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! = first) {Pop_back ();}} void Resver ()//reverse order ' {if (First->data = = 0) {return;} listnode<type> *p = first->next; listnode<type> *q = P->next;p->next = First;first->prio = P;last = P;first->data = 1; Listnode<type> *r;while (q! = first) {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;}; #endif//Main function # include "CDList.h" void Main () {cdlist<int> mylist;int select = 1;int item;while (select) {cout << "**************************************" << endl;cout << "* [1] show_list [2] Push_front *" << end  L;cout << "* [3] push_back [4] Pop_front *" << endl;cout << "* [5] pop_back [6] Insert_val           * "<< endl;cout <<" * [7] Find [8] delete_val * "<< endl;cout <<" * [9]length [[] Clear * "<< endl;cout <<" * [one] quit_system [] Destroy * "<< endl;cout <<" * [1 3] resver [+] Sort * "<< endl;cOut << "**************************************" << endl;cout << "Please select:>"; Cin >> Select; Switch (SELECT) {case 1:mylist.show_list (); Break;case 2:cout << "Please enter the 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;} &Lt;img src= "http://img.blog.csdn.net/20150531224751379?watermark/2/text/ ahr0cdovl2jsb2cuy3nkbi5uzxqvzg91zg91d2exmjm0/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/ Center "alt=" "/>

"Data Structure" in C + + to implement the various operations of the double-loop list (including header delete, 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.