using namespace std;
typedef struct DNode{
int data;
struct DNode* prior,*next;
}DNode,*DLinklist;
//建立双链表
void BuildDLinklist(DLinklist &L){
L =(DLinklist)malloc(sizeof(DNode));
L->next=NULL;
L->prior=NULL;
DNode *p = L;
int x;
cout<<"输入双链表的每一项,输入-1结束输入:";
cin>>x;
while (x!=-1) {
DNode *s = new DNode;
s->next = p->next;
s->prior = p;
p->next = s;
s->data = x;
p = s;
cin>>x;
}
}
//按序号查找
DNode *GetElmeById(DLinklist L,int i){
int j=1;
DNode *p = L->next;
if (i==0) {
return L;
}
if (i<1) {
return NULL;
}
while (p&&j<i) {
p=p->next;
j++;
}
return p;
}
//插入数据
void InsertDLinklist(DLinklist &L,int i,int x){
DNode *p = GetElmeById(L, i-1);
DNode *s = new DNode;
s->next=p->next;
p->next->prior=s;
s->prior=p;
p->next=s;
s->data=x;
}
//按序号删除
void DelateById(DLinklist &L,int i){
DNode *p=GetElmeById(L, i-1);
DNode *q=GetElmeById(L, i);
p->next = q->next;
q->next->prior = p;
free(q);
}
//打印输出双链表
void PrintfDLinklist(DLinklist L){
DNode *p = L->next;
while (p!=NULL) {
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
|