比单链表多了一个rear前节点
#include<bits/stdc++.h>
using namespace std;
typedef int datatype;
class Node{ //节点类
public:
Node *rear; //前节点
datatype data;
Node *next; //后节点
};
class dlLinkList{ //双向链表类
public:
dlLinkList();
~dlLinkList();
void CreatedlLinkList(int size);
int size;
Node *head;
};
dlLinkList::dlLinkList(){
head=new Node;
head->next=NULL;
head->data=0;
head->rear=NULL;
}
dlLinkList::~dlLinkList(){
delete head;
}
void dlLinkList::CreatedlLinkList(int size){
Node *ptemp=NULL;
Node *pnew=NULL;
this->size=size;
ptemp=this->head;
for(int i=0;i<size;++i){
cout<<"请输入第"<<i+1<<"个节点值:"<<endl;
cin>>pnew->data;
pnew->rear=ptemp; //新增前节点
ptemp->next=pnew;
ptemp=pnew;
}
cout<<"创建完成"<<endl;
}