Designing a C + + Universal Linked list: implementing polymorphic bidirectional functions

Source: Internet
Author: User
Tags object object
Before using C to implement the universal linked list, the data domain of the linked list from the specific type to the void* pointer, the use of C to achieve the strength is the pointer assignment and also to achieve universal printing

Link: c Implementation of the common bidirectional chain performance in C + + re-implementation, the idea is mostly consistent:

Instead of using the template class, the virtual function is used to achieve polymorphism, and the data field does not store any information about the type, but the pointer, which puts it in an abstract class, and is connected by the pointer.

The list has a pointer to the end of the head and a series of insert delete print operations, all written in the linked list class.

The list pointer type is a linked list node class, the node's pointer field and data field are pointer types, and the data field points to an abstract class object object.

The object class is an abstract class that, when it is necessary to store integer data in a linked list, implements a subclass of the integer to inherit the class, must implement the virtual function in the abstract class, take the printing function as an example, each subclass of a different data type must have its own printing function, and object does not care how to print.

Class Intobject:p ublic object{public:intobject (int d = 0):d ata (d) {}~intobject () {}void Print () const{cout << Data & lt;< "--";} Private:int data;};/ Class Strobject:p ublic object{public:strobject (char * STR) {if (str = = NULL) {data = new char[1];d ata[0] = ' + ';} Else{data = new Char[strlen (str) + 1];strcpy (data, str);}} ~strobject () {}void Print () const{cout << "\" "<< data <<" \ "" << "--";} Private:char * data;};/ Class Floatobject:p ublic object{public:floatobject ( Float d = 0):d ata (d) {}~floatobject () {}void Print () const{cout << data << "-";} Private:float data;};/ void Main () {list mylist;for (int i = 0; i < 5; ++i) { Intobject *pi = new Intobject (i); Mylist.push_back (pi);} Mylist.printlist (); char* arr[5] = {"Affd", "FDAs", "FDFSS", "ere", "Qret"};for (int i = 0; i < 5; i++) {strobject*ps = new Strobject (Arr[i]); Mylist.push_back (PS);} Mylist.printlist (); float brr[5] = {0.34, 54.32, 0.53, 43.2, 5.878};for (int i = 0; i < 5; i++) {floatobject*ps = new F Loatobject (Brr[i]); Mylist.push_back (PS);} Mylist.printlist ();}

Release of the linked list:

Class Object{public:object () {}virtual ~object ()//Because it is a virtual function, the destructor of the parent class is mobilized while the deconstruction of the child class is transferred {}////defines the interface universal print virtual void print () const = 0 };class List;class Listnode{friend class List;public:listnode () {data = Null;next = NULL After inheriting the pure virtual function subclass must be implemented) ListNode (Object *pobj) {data = Pobj;next = NULL;} ~listnode () {Delete data;next = NULL;} Private:object *data;listnode *next;}; Class List{public:list () {head = tail = new ListNode;} ~list () {ListNode *delp = head->next;while (delp!= tail) {Head->next = Delp->next;delete Delp;delp = head-> Next;} Delete Head;head = tail = NULL;} void Push_back (Object *pb)//tail interpolation {ListNode *s = new ListNode (PB); assert (s! = NULL); tail->next = S;tail = s;} void Printlist () const{listnode *p = Head->next;while (P! = NULL) {p->data->print ();p = P->next;} cout << "NULL" << Endl;} Private:listnode *head;listnode *tail;};/ Class Intobject:p ublic object{public:intobject (int d = 0 ):d ATA (d) {}~intobject () {CoUT << "Delete int" << Endl;} void Print () const{cout << data << "--";} Private:int data;};/ Class Strobject:p ublic object{public:strobject (char * STR) {if (str = = NULL) {data = new char[1];d ata[0] = ' + ';} Else{data = new Char[strlen (str) + 1];strcpy (data, str);}} ~strobject () {cout << "delete string" << endl;delete []data;data = NULL;} void Print () const{cout << "\" "<< data <<" \ "" << "--";} Private:char * data;};
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.