/*llist.cpp* Author:qiang xiao* time:2015-07-12*/#include<iostream>using namespacestd;classnode{ Public: intdata; Node*ptr; Node (intelem=0, node* node= NULL) { This->data= Elem; This->ptr=NULL;}};classllist{Private: Node*Head; Node*tail; intlength; Public: Llist (); ~llist (); BOOLAppend (node*); BOOLInsertint, node*); voidprint (); intGetLength () {return This-length;}}; Llist::llist () {Node* init=NewNode (4); This->head=NewNode (); This->tail=NewNode (); This->head=Init; This->tail=Init; This->length=0;} Llist::~llist () {DeleteHead; Deletetail;}BOOLLlist::insert (intPOS, node*node) { intI=0; Node* fence=NewNode (); Fence= This-Head; while(i<POS) {Fence= fence->ptr; I++; } node->ptr= fence->ptr; Fence->ptr=node; This->length++; return true;}BOOLLlist::append (node*node) { This->tail->ptr=node; This->tail=node; This->length++; return true;}voidllist::p rint () {Node* p= This->head->ptr; while(p) {cout<<p->data<<"\ t"; P= p->ptr; } cout<<Endl; Deletep;}intMain () {cout<<"\n******************begin test**********************\n"; Node* node1=NewNode (1); Node* node2=NewNode (2); Node* node3=NewNode (3); Node* node4=NewNode (4); Llist* list=Newllist (); cout<<"\n******************empty list**********************\n"; List-print (); List-append (Node1); List-append (Node2); List-append (NODE3); List-append (NODE4); cout<<"\n******************after append********************\n"; List-print (); cout<<"\ n"; Node* node5=NewNode (Ten); intpos=2; List-Insert (POS,NODE5); Node* node6=NewNode ( -); POS=4; List-Insert (POS,NODE6); cout<<"\n\n*****************after insert*******************\n"; List-print (); return 0;}
Console Display:
[Email protected]:~/c/datastructure$ g++ LList.cpp-o llist.o[email protected]-ubun:~/c/datastructure$. ******************begin test****************************************empty list************************* After append********************1 2 3 4 * * * * After insert*******************1 2 3 - 4 [email protected]
Writing this program is mainly to practice the use of the list. There are many areas in the code that need improvement, so please correct me.
Welcome to the Exchange!
C + + implementation of a simple linked list