"C + + version of data structure" of the implementation of the single-linked list (lead node and tail nodes)

Source: Internet
Author: User

The structure of the implemented single-linked list is as follows:


Header file: SList.h

#include <iostream> #include <cassert>using namespace Std;typedef enum{false,true}status;template<   Class Type>class List;template<class Type>class listnode{friend class list<type>; The friend class can access the members of the class Private:type data; ListNode *next;public:listnode ():d ATA (Type ()), Next (NULL) {}listnode (type D, listnode<type> *n = NULL):d ata (d), Next (n) {}void SetData (Type d) {data = D;} Type GetData () const{return data;} ~listnode () {}};template<class type>class list{private:listnode<type> *first;//single-linked list head pointer ListNode<Type  > *last;   The tail pointer of the single-linked list size_t size; Single-linked list length protected:/*listnode<type> * _buynode (const Type &x,ListNode<Type> *_narg = NULL)//---> Pending resolution {listnode<type> *s = new listnode<type> (X,_NARG);} */listnode<type> * _buynode (const Type &x) {listnode<type> *s = new listnode<type> (x); assert (s! = NULL); return s;} Public:list () {first = last = _buynode (0); size = 0;} ~list () {destory ();} Status Push_back (CONST Type &x) {listnode<type> *s = _buynode (x); last->next = S;last = S;size++;return TRUE;} void Show_list () {listnode<type> *s = First->next;while (s! = NULL) {cout << s->data << "; s = S->next;} cout << "NULL" << Endl;} Status Push_front (const Type &x) {listnode<type> *s = _buynode (x); s->next = First->next;first->next = S;if (size = = 0) last = S;size++;return TRUE;} Status Pop_back () {if (first = = last)//size = = 0{cout << "Single-linked list is empty, unable to tail delete" << Endl;return FALSE;} listnode<type> *s = First;while (s->next! = last) {s = s->next;} Delete Last;s->next = Null;last = S;size--;return TRUE;} Status Pop_front () {if (first = = last)//size = = 0{cout << "Single-linked list is empty, cannot header delete" << Endl;return FALSE;} listnode<type> *s = First->next;first->next = S->next;delete s;if (size = = 1) last = First;size--;return TRU E;} listnode<type>* Find (const Type &x)//Returns the address of the element being looked at: Trouble (Delete_val,next,prio) {listnode<type> *s = First->next;while (s! = NULL && S->data! = x) {s = s->next;} return s;} /*listnode<type>* Find (const Type &x)//Returns the previous address of the element being looked up {listnode<type> *s = First;while (s->next! = NULL &&s->next->data = x) {s = s->next;} if (S->next = = NULL) return Null:elsereturn s;} */////method One//status delete_val (const Type &AMP;X)//General method//{//listnode<type> *s = find (x);//if (s = = NULL)//{//cout & lt;< "The element does not exist, cannot be deleted" << endl;//return false;//}//if (s = = First->next)//pop_front ();//else if (s = = last)//pop_ Back ();//else//{//listnode<type> *p = First;//while (p->next! = s)//search for the previous node of the found element//{//p = p->next;//}//p-& Gt;next = S->next;//delete S;//size--;//}//return true;//}//method Two status Delete_val (const Type &x) {listnode< Type> *s = Find (x), if (s = = NULL) {cout << "the element does not exist, cannot be deleted" << Endl;return FALSE;} if (s = = last) pop_back ();//---------------> Tail node does not have a node behind it, it cannot be overwritten with the value of the latter node, so else{listnode<type> *p is defined separately = S-&GT;next;s->data = p->data;//The value of the back node overrides its value S->next = p->next;//Deletes the subsequent node delete p;size--;} return TRUE;} listnode<type>* Next (const Type &x) {listnode<type> *s = Find (x), if (s = = NULL) {cout << "the element does not exist" < < Endl;return NULL;} if (s = = last) {cout << "This element has no successor" << Endl;return NULL;} Else{return S->next;}} listnode<type>* prio (const Type &x) {listnode<type> *s = Find (x), if (s = = NULL) {cout << "the element does not exist" < < Endl;return NULL;} if (s = = First->next) {cout << "this element has no precursor" << Endl;return NULL;} else{listnode<type> *p = First;while (p->next! = s) {p = P->next;} return p;}} Single-linked list reverse: Disconnects the list from the first element (the first node becomes the last node)//The value of the node after the list, followed by the head interpolation (remember to delete the node after the value header) void Reserve () {if (size = = 0 | | size = = 1) return; listnode<type> *s = first->next; listnode<type> *p = S->next;last = S;last->next = null;//The first node becomes the last node while (P! = NULL) {s = p;p = p->next;/ /push_front (s->data);//delete S;s->next = first->next;first->next = S;}} Insert by value: First, then look for the previous position where you want to insert the element, and insert the element status Insert_val (const Type &x) {sort () after that position; listnode<type> *p = _buynode (x); listnode<type> *s = First;while (s->next! = NULL && S->next->data < P->data)//Find the previous node of the inserted element { s = s->next;} if (S->next = = NULL)//At this time, the interpolated element is the largest, the end plug can be (not found to insert the previous position) push_back (x); else{p->next = S->next;s->next = P;size ++;} return TRUE;} Sorting ideas: Disconnect the list from the first node (the first becomes the last node)//Insert the following node sequentially by value (note that after the value is inserted, the node is freed) void sort () {if (size = = 0 | | size = = 1) return; listnode<type> *s = first->next;  listnode<type> *p = S->next;last = S;              -------> First node becomes the last node Last->next = Null;while (P! = NULL)//-------> Break followed by value insert {s = p;        Save the node to be inserted p = p->next;           Prepare for the next insertion insert_val (s->data);//insert Delete s; Release nodes to prevent memory leaks}}size_t lenth () {return size;} /*void Clear () {listnode<type> *s = first; Listnode<type> *p;while (size--)//p always points to the first node (always delete the first node, delete size times) {p = S->next;s-> next = P->next;delete p;} Size = 0;last = fisrt;} */void Clear () {listnode<type> *s = first->next;//p always points to the first node (always delete the first node, delete size times) while (s! = NULL) {first-> Next = S->next;delete s;s = first->next;} Size = 0;last = First;} void Destory () {clear ();d elete First;first = last = NULL;}};
test file: Main.cpp

#include "SList.h" int main () {list<int> mylist;int item;int N;int select = 1; Listnode<int> *p;while (Select) {cout << "*************************************** *" << endl;cout   << "*[1" push_back [2] Push_front * "<< endl;cout <<" *[3] show_list [4] Pop_back               * "<< endl;cout <<" *[5] pop_front [6] insert_val * "<< endl;cout <<" *[7] Lenth [8] Find * "<< endl;cout <<" *[9 "merge [ten] delete_val*" << endl;cout &LT;&L T [*[11] sort [[] reserve * "<< endl;cout <<" *[13] Next [] clear * "&LT;&L T Endl;cout << "*[15] prio [0] quit_system*" << endl;cout << "Please select:>"; Cin >> Select; Switch (SELECT) {case 1:cout << "Please enter the element to be inserted (-1 end):>"; while (CIN >> item, Item! =-1) {Mylist.push_back (item) ;} Break;case 2:cout << "Please enter the element to be inserted (-1 end):>"; while (Cin >&Gt Item, Item! =-1) {Mylist.push_front (item);} Break;case 3:mylist.show_list (); break;case 4:mylist.pop_back (); break;case 5:mylist.pop_front (); Break;case 6:cout << "Please enter the element to insert:"; CIN >> Item;mylist.insert_val (item); Break;case 7:cout << "Length:" << Mylist.lenth () << endl;break;case 8:cout << "Please enter the element to find:"; Cin >> item;if (Mylist.find (item)) cout <& Lt "It's found" << endl;elsecout << "It's not exist" << endl;break;case 9:cout << "Please enter a location to delete:"; Cin &G T;> N;//mylist.delete_pos (N,item); break;case 10:cout << "Please enter the element to be deleted:"; Cin >> Item;mylist.delete_val ( Item); Break;case 11:mylist.sort (); break;case 12:mylist.reserve (); Break;case 13:cout << "Please enter the element to find successor:"; CIN > > item;p = Mylist.next (item), if (P! = NULL) cout << p->getdata () << endl;break;case 14:mylist.clear (); BR Eak;case 15:cout << "Please enter the element to find the precursor:"; cin >> item;p = Mylist.prio (item), if (P! = NULL) cout << p->getdata () << EndL;break;default:break;}} System ("pause"); return 0;}

Summary:

The difficulty is also the most interesting place: sorting (sort) and the implementation of single-linked list inverse (reverse)

The realization of the two has the same similarities;

Sort : Disconnects the original list from the first node, divides it into two, the first node and the head knot become a single linked list, and then inserts the remaining nodes sequentially by value

Linked list reverse: The original list is disconnected from the first node, divided into two, the first node and the head nodes become a separate list, and then the remaining nodes in turn head interpolation


Head insertion and head deletion with note points:

Push_front (): When the first node is inserted (the tail pointer points to that node)

Pop_front (): When the last node is deleted (the tail pointer points to the head node)


The Find () function:

Returns the address of the element you are looking for: (delete by value-Google interview questions)

1: Find again, position the previous element, and delete the element you want to delete

2: The element p that follows the element you want to delete overrides the element that you want to delete, and then remove P


Returns the address of the previous element of the element being looked at (deleting Delete_val () by value is simple)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"C + + version of data structure" of the implementation of the single-linked list (lead node and tail nodes)

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.