本文共 5093 字,大约阅读时间需要 16 分钟。
templateclass ChainNode{ friend Chain ;private: T data; ChainNode *next;};template class Chain { public: Chain(); ~Chain(); bool isEmpty()const{ return first->data == 0;} int length()const; bool find(int k,T& x)const; int search(const T &x)const; Chain & Delete(int k,T& x); Chain & insert(int k,T& x); void output(Chain & k);private: ChainNode *first; //define the first node of the chain which has no message};
templateChain ::Chain(){ first = new ChainNode(); if(first ==NULL) { cout<<"construct the linkedlist absortively!"< data = 0; first->next = NULL:}template Chain ::~Chain(){ ChainNode *temp = first; while(temp) { temp = first->next; delete first; first = temp; } cout<<"deconstruct the linkedlist triumphantly!!"<
生成具有的头结点的链表,在初始化方法中,需要将生成头结点,为其申请相关的内存空间,并指明各个相关信息的位置
对链表进行清空和对链表进行析构是一样的吗?如果是析构,不应该释放所有的空间吗?为什么你还保留一个头结点。
templateint Chain ::length()const{ return first->data;}/* description:find the element whose value is x copy the element to the a caution:the index of the element id from one in the view of user*/template bool Chain ::find(int k,T& x)const{ ChainNode *temp = first; int i = 0; //if temp is NULL ,temp->nex t is NULL; while(temp && i < k) { temp = temp->next; i ++; } if(temp || i < K) { return false; } x = temp->data; return true; }
if(temp){ }
/* description:search the index of x return:if return 0,x does not exist in the linkedlist*/templateint Chain ::search(const T& x)const{ ChainNode *temp = first->next; int i = 1; while(temp) { if(temp->data == x) { return i; } i ++; temp = temp->next; } if(temp) { cout<<"the element does not exist!"<
/* description:delete the element whose index is k,dan copy its value to x*/templateChain * Chain ::delete(int k,T& x){ ChainNode *temp = first; int i = 0 //if temp is NULL ,temp->nex t is NULL; while(temp && i < k - 1) { temp = temp->next; i ++; } if(!temp||(!temp->next || i < k - 1)) { cout<<"the element you want to delete does not exist!"< *temp2 = temp->next; temp->next = temp2->next; x = temp2->data delete temp2; //caution:decrease the length of the linkedlist first->data --; return *this;}
凡是调用对应的结点的相关的属性,前提是确保该对象是实际存在的,不为空
在链表中将对应结点移出逻辑结构的时候,就需要释放相关的空间
/* description:insert the x after the element whose index is k*/templateChain & Chain ::insert(int k,const T& x){ ChainNode *temp = first; int i = 0 //if temp is NULL ,temp->nex t is NULL; while(temp && i < k - 1) { temp = temp->next; i ++; } if(!temp|| i < k - 1)) { cout<<"the index you want to insert does not exist"< *temp2 = new ChainNode ; temp2->data = x; temp2->next = temp->next; temp->next = temp2; //caution:you should increase the length of the array first->length ++; return *this;}template void Chain ::output(){ //caution:you have refer the index of the next element of the first //so,you should judge if the element does exist if(isEmpty()) { return ; } ChainNode *temp = first->next; while(temp) { cout< date<<" "; temp = temp->next; }}
#include#include "linklist.h"using namespace std;int main(){ cout<<"insert the element!!"<
templateChain & Chain ::add(T& x){ insert(length()+1,x); return *this;}
转载地址:http://qwwvb.baihongyu.com/