C ++; two-way cyclic linked list

Source: Internet
Author: User

C ++; two-way cyclic linked list

# Pragma once // header file using namespace std; typedef int DataType; class ListNode {friend class DoubleList; private: ype _ data; ListNode * _ next; ListNode * _ prev; public: listNode () ;}; class DoubleList {private: ListNode * _ pHead; ListNode * _ pTail; public: DoubleList (); DoubleList (const DoubleList & list );~ DoubleList (); void PushBack (const DataType x); void PopBack (); void Insert (ListNode * pos, const DataType x); void Erase (ListNode * pos ); listNode * Find (const DataType x); void Reverse (); void GetData (); void Destroy (); void operator = (const DoubleList & list );}; # include <iostream> // function file # include "doubleList. h "# include <assert. h> using namespace std; ListNode: ListNode (): _ next (NULL), _ prev (NULL) {} Doubl EList: DoubleList (): _ pHead (new ListNode), _ pTail (_ pHead) {_ pHead-> _ next = _ pHead; _ pHead-> _ prev = _ pHead;} DoubleList: DoubleList (const DoubleList & list) {_ pHead = new ListNode; ListNode * head = _ pHead; _ pTail = _ pHead; ListNode * tmp = list. _ pHead-> _ next; while (tmp! = List. _ pHead) {head-> _ next = new ListNode; head-> _ next-> _ data = tmp-> _ data; _ pTail = head-> _ next; _ pTail-> _ prev = head; _ pTail-> _ next = _ pHead; _ pHead-> _ prev = _ pTail; head = head-> _ next; tmp = tmp-> _ next;} DoubleList ::~ DoubleList () {while (_ pTail! = _ PHead) {ListNode * del = _ pTail; _ pTail = _ pTail-> _ prev; delete del;} delete _ pTail;} void DoubleList :: pushBack (const DataType x) {ListNode * tmp = new ListNode; tmp-> _ prev = _ pTail; _ pTail-> _ next = tmp; tmp-> _ data = x; _ pTail = tmp; tmp-> _ next = _ pHead; _ pHead-> _ prev = tmp;} void DoubleList: PopBack () {ListNode * del = _ pTail; if (_ pTail! = _ PHead) {_ pTail = _ pTail-> _ prev; _ pTail-> _ next = _ pHead;} delete del;} void DoubleList: Insert (ListNode * pos, const DataType x) {assert (pos); ListNode * tmp = new ListNode; tmp-> _ prev = pos; tmp-> _ next = pos-> _ next; pos-> _ next = tmp; tmp-> _ next-> _ prev = tmp; tmp-> _ data = x; _ pTail = _ pHead-> _ prev ;} void DoubleList: Erase (ListNode * pos) {assert (pos); ListNode * del = pos; if (_ pTail! = _ PHead) {pos-> _ next-> _ prev = pos-> _ prev; pos-> _ prev-> _ next = pos-> _ next ;} delete del;} ListNode * DoubleList: Find (const DataType x) {ListNode * tmp = _ pHead-> _ next; while (tmp! = _ PHead) {if (tmp-> _ data = x) {return tmp;} tmp = tmp-> _ next;} return NULL;} void DoubleList :: getData () {ListNode * tmp = _ pHead-> _ next; while (tmp! = _ PHead) {cout <tmp-> _ data <"->"; tmp = tmp-> _ next;} cout <"NULL" <endl ;} void DoubleList: Reverse () {ListNode * tmp = _ pHead-> _ next; ListNode * newHead = _ pHead; do {newHead-> _ next = newHead-> _ prev; newHead-> _ prev = tmp; newHead = tmp; tmp = newHead-> _ next;} while (newHead! = _ PHead); _ pTail = _ pHead-> _ prev;} void DoubleList: Destroy () {this-> ~ DoubleList ();} void DoubleList: operator = (const DoubleList & list) {_ pHead = new ListNode; ListNode * head = _ pHead; _ pTail = _ pHead; listNode * tmp = list. _ pHead-> _ next; while (tmp! = List. _ pHead) {head-> _ next = new ListNode; head-> _ next-> _ data = tmp-> _ data; _ pTail = head-> _ next; _ pTail-> _ prev = head; _ pTail-> _ next = _ pHead; _ pHead-> _ prev = _ pTail; head = head-> _ next; tmp = tmp-> _ next; }}# include <iostream> // main function test file # include "doubleList. h "using namespace std; void test1 () {DoubleList s1; s1.PushBack (1); s1.PushBack (2); s1.PushBack (3); s1.PushBack (4); s1.GetData (); cout <(s1.Find (1) <endl; s1.Insert (s1.Find (4), 5); s1.GetData (); DoubleList s2; s2.GetData (); s1.Reverse (); s1.GetData (); s2 = s1; s2.GetData () ;}int main () {test1 (); return 0 ;}

 

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.