One: Cause
(1) to take out their own implementation of the list at the beginning of the first time with the C + + class to implement list or first write a project to use the list class, in and now achieve the list contrast, the mood is extremely complex;
(2) explained a little, I have been naïve, is still relatively naïve, but I believe that as long as the original dream of the people, will eventually go to maturity;
(3) More detailed comparison of the whole list , see STL List class source code implementation
(4) Once again thank oneself can adhere to the original dream, I believe you also have a similar dream, do not envy others talent, other people's success can not be copied, other people's glory will not repeat, only our own down-to-earth walk every step, sow our own dream seeds, stride forward.
Two: The detailed code is as follows: The following is the code and some personal experience, welcome everybody pointing ~
(1) Simple implementation of Linklist class
struct node{int data; Node *next;}; Class linkedlist{public:linkedlist (const int &n);//N list length LinkedList (const LinkedList &aplist) ; ~linkedlist (); void display (); void Insert_before (const int &KEY,CONST int &toinsert); void del (const int &todelete); node* find_key (const int &key); void Distroy (); int GetLength (); Private:node *head; Node *cur;};/ /class After the same as the struct, you must add a semicolon (;) linkedlist::linkedlist (const int &n) {head = new Node; Head->data = -1;//This element is an identifier Head->next = null;//init head Node *p = head;//p in the left of cur int i; cout << "Please enter chain list:" << Endl; for (i=0;i<n;i++) {cur = new Node; CIN >> cur->data; Cur->next = NULL; P->next = cur; p = cur; }}linkedlist::~linkedlist () {cout << "* * *"; Distroy ();} void LinkedList::d isplay () {if (head = = NULL) { cout << "This list does not exist! "<< Endl; Return } cur = head->next; if (cur = = null) {cout << "This list is empty! "<< Endl; Return } while (cur! = NULL) {cout << cur->data << ""; Cur = cur->next; } cout << endl;//One more space for}void LinkedList::d el (const int &todelete) {if (head = = NULL) {cout < < "This list does not exist! "<< Endl; Return } Node *p = head; Cur = head->next; if (cur = = null) {cout << "This list is empty, delete failed! "<< Endl; Return } while (Cur!=null && cur->data!=todelete) {p = cur; Cur = cur->next; if (cur = = NULL) cout << "This element does not exist, delete failed!" "<< endl;//Of course this list is empty else {cout <<" successfully deleted element: "<< cur->data << Endl; P->next = cur->next; Delete cur; }}void linkedlist::insert_before (const int &key,const int &toinsert) {if (HEAD = = NULL) {cout << "This list does not exist! "<< Endl; Return } Node *p,*tmp; p = head; Cur = head->next; if (cur = = null) {cout << "This list is empty, insert failed! "<< endl;//of course it is also possible that this list is empty return; } while (Cur!=null && cur->data!=key) {p = cur; Cur = cur->next; if (cur = = NULL) cout << key << ": This element does not exist, insert failed! "<< Endl; else {tmp = new Node; Tmp->data = Toinsert; Tmp->next = NULL; P->next = tmp; Tmp->next = cur; cout << "Insert element successfully:" << toinsert << Endl; }}node* linkedlist::find_key (const int &key) {if (head = = NULL) {cout << "This list does not exist! "<< Endl; return NULL; } cur = head->next; if (cur = = null) {cout << "This list is empty, find failed! "<< Endl; return head; } while (Cur!=null && cur->data!=key) cur = cur->next; if (cur = = NULL) {cout << key << "This element is not in the list, the lookup failed!" "<< Endl; return head; } else {cout << "successfully found element:" << cur->data << Endl; return cur; }}void LinkedList::d Istroy () {if (head = = NULL) {cout << "This list does not exist! "<< Endl; Return } cur = head->next; if (cur = = NULL) {cout << "This list is empty and cannot be deleted again!" "<< Endl; Delete head;//destroys head node head = null;//is used for subsequent judgment return; } while (cur! = NULL) {Head->next = cur->next; Delete cur; Cur = head->next; } if (head! = NULL) {delete head; head = NULL; }}int linkedlist::getlength () {int cp = 0; if (head = = NULL) {cout << "This list does not exist!" "<< Endl; return CP; } cur = head->next; if (cur = = null) {cout << "This list is empty! "<< Endl; return CP; } while (cur! = NULL) {CP + +; Cur = cur->next; } return CP;}
(2) test of main function
#include <iostream>using namespace std;//main function starts, main () int main () {int len,todelete,key,toinsert,tofind;//can of course Some variables agree to be named Opt_key Node *tmp; int CP; cout << "Please enter the length of the list to be established:" << Endl; Cin >> Len; LinkedList *list = new LinkedList (len);//New object, pointer to List->display ();//List.display List of//linkedlist () = LinkedList (len); List.display ();//annotation statements are also true, but must be changed to list.xxx; And the new object (on the heap) must be deleted manually, otherwise the destructor is not called, the object is not new, the destructor is called automatically cout << "Please enter the element you want to delete:" << Endl; Cin >> Todelete; List->del (Todelete); List->display (); cout << "Please enter the element you are looking for:" << Endl; Cin >> Tofind; TMP = List->find_key (tofind); cout << Returns the result of main () after finding: "<< tmp->data << Endl; List->display (); cout << "Please enter the element to be inserted and its right element:" << Endl; CIN >> Toinsert >> key; List->insert_before (Key,toinsert); List->display (); cout << "list length before destruction:"; CoUT << list->getlength () << Endl; List->distroy (); CP = List->getlength (); cout << "The length of the linked list after destruction:"; cout << CP << Endl; List->~linkedlist ();//The destructor is not automatically called,//Otherwise, the textbook is clearly said that the destructor can be automatically called, but there is a premise: the object's lifetime is about to end when// Called when the function ends (when the stack memory is about to be destroyed), the object in the heap memory is called by the delete list when the delete occurs; return 0;}
Three: Experiences and experiences
(1) in C + +, when a normal object leaves its scope, its destructor is automatically called, destroying the object, freeing up the memory it occupies, and not having a memory leak.
(2) The new command can dynamically allocate a certain amount of space to an object in memory and return the first address of the allocated space, and if the object is not destroyed by delete before the end of the program run, release the
Memory leaks are also occurring in the space used.
(3) See oneself above write of two piece of experience and experience, oneself also Puchi smile, again see oneself write code--main function and Linklist class unexpectedly together; it seems that I was really naïve a lot;
(4) Look at the code now written, completely different, but I still thank the original I, then I chose the road, maturity is based on the naïve.
(5) The last excerpt of a sentence, and share with you: when your talent can not afford your ambition, only calm down to study hard! Even if fate is destined to be a soy sauce, also want to hit a bottle with other people not the same soy sauce!
C + + Implementation List Class (First use Class)--new object requires delete