Sicily 1000. LinkedList

Source: Internet
Author: User

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

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.