Description
Template <typename e>
Class Linkedlist{private://inner Class:linked-list node class node {public:e data; Node * NEXT; }; Node * FIRST; Public:linkedlist () {first = 0; } ~linkedlist () {while (first! = 0) {Removefirst (); }} E GetFirst () {return first->data; } bool IsEmpty () {return first = = 0; }//Implementation of the following 4 functions: LinkedList (const LinkedList & that); LinkedList & operator= (const LinkedList & that); void Removefirst (); void AddFirst (E data);}; Hint
The insertion of a linked list uses the head interpolation method, which simply submits the implementation of the template class function and does not need to submit the main function, as shown in the following code:
Template <typename e>void Linkedlist<e>::removefirst () {node * node = first; First = node->next; Delete node; The code is as follows:
Template <typename e>LinkedList<e>::linkedlist (ConstLinkedList &That ) {Node* current =0; Node* node =That.first; while(Node! =0) { if(Current = =0) Current= first =NewNode (); Else{ Current->next =NewNode (); Current= current->Next; } Current->data = node->data; Current->next =0; Node= node->Next; }}template<typename e>LinkedList<E>& Linkedlist<e>::operator= (ConstLinkedList &That ) {LinkedList<E>tmp (that); while(First! =0) Removefirst (); Node* current =0; Node* node =Tmp.first; while(Node! =0) { if(Current = =0) Current= first =NewNode (); Else{ Current->next =NewNode (); Current= current->Next; } Current->data = node->data; Current->next =0; Node= node->Next; } return* This;} Template<typename e>voidLinkedlist<e>:: Removefirst () {Node* node =First ; First= node->Next; Deletenode;} Template<typename e>voidLinkedlist<e>:: AddFirst (E data) {Node* Newfirst =NewNode (); Newfirst->data =data; Newfirst->next =First ; First=Newfirst;}
Test code:
Template <typename e>classlinkedlist{Private: //Inner class:linked-list node classNode { Public: E data; Node*Next; }; Node*First ; Public: LinkedList () { First=0; } ~LinkedList () { while(First! =0) {Removefirst (); }} E GetFirst () {returnFirst->data; } BOOLIsEmpty () {returnFirst = =0; }//TODO:LinkedList (ConstLinkedList &that ); LinkedList&operator= (ConstLinkedList &that ); voidRemovefirst (); voidAddFirst (E data);};/*template <typename e>linkedlist<e>::linkedlist (const linkedlist<e> & that) {}template <t Ypename e>linkedlist<e> & linkedlist<e>::operator= (const linkedlist<e> & that) {}templat E <typename e>void Linkedlist<e>::removefirst () {node * node = first; First = node->next; Delete node; Template <typename e>void Linkedlist<e>::addfirst (E data) {}*///#include "source.cpp"#include<iostream>using namespacestd; LinkedList<Double>Read () {LinkedList<Double>list; for(inti =0; I <Ten; ++i) {Doublevalue; CIN>>value; List.addfirst (value); } returnlist;}voidRemoveandprintall (linkedlist<Double>list) { while(!List.isEmpty ()) {cout<< List.getfirst () <<Endl; List.removefirst (); }}intMain () {LinkedList<Double> list =read (); LinkedList<Double>List2; List2=list; Removeandprintall (LIST2);}
Sicily 1000. LinkedList