"Data Structure" in C + + implementation of a single cycle linked list of various operations (including header delete, tail delete, insert, reverse, destroy, empty, etc.)

Source: Internet
Author: User

Header file #ifndef _list_h#define _list_h#include<iostream>using namespace Std;template<class Type>class CList ; Template<class Type>class Listnode{friend class clist<type>;p ublic:listnode ():d ATA (Type ()), Next (NULL) {}listnode (Type D, listnode<type> *n = NULL):d ata (d), Next (n) {}~listnode () {}private:type data; Listnode<type> *next;}; Template<class Type>class clist{public:clist () {first = last = Buynode (); last->next = First;} ~clist () {Destroy ();} Public:void push_back (const Type &x)//tail plug ' {listnode<type> *s = Buynode (x); last->next = S;last = S ; last->next = first;first->data++;} void Push_front (const Type &x)//head plug ' {listnode<type> *s = Buynode (x); s->next = first->next;first- >next = S;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> *s = first;for (int i = 1; I < first->data; i++) {s = s->next;} Delete S->next;s->next = First;last = s;first->data--;} void Pop_front ()//Header delete ' {if (First->data = = 0) {return;} if (First->data = = 1) {pop_back ();} else{listnode<type> *s = First->next;first->next = S->next;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 = p;first->data+ +;} Elsepush_back (x);} Else{p->next = S->next;s->next = p;first->data++;}} void Show_list ()//display ' {listnode<type> *q = First->next;while (q!=first) {cout << q->data << ", q = Q->next;} cout << "NULL first->data=" << first->data << "firSt= "<<first<<" 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 = First;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! = Fi Rst 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;d elete 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;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 "CList.h" void Main () {clist<int> mylist;int select = 1;int item;//int pos;while (select) {cout & lt;< "**************************************" << endl;cout << "* [1] show_list [2] Push_front *" < < Endl;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 <<" * [[] resver [] sort * "<< endl;cout <<" ************************************** "<< Endl ; 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 address of this number 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 a single cycle linked list of various operations (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.