Design and implementation of linked list class (smart pointers)

Source: Internet
Author: User

recently read "C++primer" when found the smart pointer this thing, it is easy to use, with the habit of later than the new and delete code to a lot of simple

/**** Chain List class design and implementation, default construction, copy construction, insert, delete, get length, display linked list data ****/
#include <memory>//for shared_ptr
using std::tr1:: shared_ptr;//be sure to introduce namespaces, Share_ptr defined here
struct Linknode
{
    shared_ptr <linkNode> next;
    int data;
};

Class LinkedList
{
private:
    int length;
    shared_ptr <linkNode> head;
Public:
    LinkedList ();
    LinkedList (const linkedlist& otherlist);

    void inserttolist (int ins);
    Delete 1 nodes or multiple node
    void Deleatfromlistbeginhead () from the beginning node;
    void deleatfromlistbeginhead (int num);

    void Show ();
    int getlemgth ();
    Shared_ptr<linknode> GetHead ();
    ~linkedlist ();

};

The type of int used in the data field, and then change this to a template class before sending it

/**** Chain List class design and implementation, default construction, copy construction, insert, delete, get length, display linked list data ****/#include "stdafx.h" #include "linkedList.h" #include <memory>//
For shared_ptr #include <iostream> using std::tr1::make_shared; Construction Linkedlist::linkedlist (): Length (0) {head = NULL;}
    Default linkedlist::linkedlist (const linkedlist& otherlist) {Auto con = otherlist.head;
        while (Con) {inserttolist (con->data);
    con = con->next; }//change//insret date into list void linkedlist::inserttolist (int ins) {Auto node = make_shared<linknode> (
    );
    Node->data = INS;
    Node->next = head;
    Head = node;
++length; }//deleat date from list begin head void LinkedList::d eleatfromlistbeginhead () {if (head) {head = Head->n
        Ext
        --length;
    Std::cout << "successfully deleted" << Std::endl;
    else {std::cout << "This linkedlist is empty" << Std::endl; } void LinkedList::d eleatfromlistbeginhead (int num) {if (num >= length) {for (int i = 0; i < Num. ++i) {head = Head->n
            Ext
        --length;
    } std::cout << "successfully deleted" << Std::endl;
    } else{std::cout << "This data is invalid" << Std::endl;
    }//shows void Linkedlist::show () {shared_ptr<linknode> node = head;
        while (node) {std::cout << head->data<< "->";
    node = node->next;

} if (head) std::cout << "list are end" << Std::endl; 
int Linkedlist::getlemgth () {return length;} shared_ptr<linknode> Linkedlist::gethead () {return this->head;}
 Linkedlist::~linkedlist () {}

Memory request when using shared_ptr it is best to use make_shared The return value of,make_shared<int> () is a pointer to a shared_ptr type Shared_ PTR and new can also be used in conjunction with shared_ptr <int> p (new int (1024)) to create a smart pointer in direct initialization (NOTE: You cannot use shared_ptr <int> p = new Int ( 1024), because it is wrong to convert the built-in pointer implicitly to the smart pointer , if we have already created a shared_ptr<int> p named P, you can pass the Shared_ The Reset method brought by the PTR pointer is assigned a value of p, such as p.reset (new int (1024)); it's okay.

Although built-in pointers and smart pointers can be converted, they are not recommended for mixing, which can cause a lot of unnecessary trouble

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.