#include<stdio.h>
#include<stdlib.h>
typedef struct dnode{
int data;
struct dnode *prior,*next;
}DNode;
int length(DNode *Dl){
int length = 0;
while(Dl->next != NULL){
Dl = Dl->next;
length++;
}
return length;
}
bool lengthOver(DNode *Dlist,int flag){
int len = length(Dlist);
if(flag > len){
printf("请删除正确的节点!");
return false;
}else{
return true;
}
}
bool InitDlist(DNode* &Dl){
Dl = (DNode*)malloc(sizeof(DNode));
if(Dl == NULL){
return false;
}
Dl->next = NULL;
Dl->prior = NULL;
return true;
}
void InsertDlist(DNode *Dl,int data){
DNode *p;
InitDlist(p);
p->data = data;
p->next = Dl->next;
if( Dl->next != NULL ){
Dl->next->prior = p;
}
p->prior = Dl;
Dl->next = p;
}
bool DelDlist(DNode *Dl,int flag){
DNode *p;
InitDlist(p);
if(!lengthOver(Dl,flag)){
return false;
}
for(int i = 0;i<flag-1;i++){
Dl = Dl->next;
}
p = Dl->next;
Dl->next = p->next;
p->next->prior = Dl;
free(p);
return true;
}
bool UpdateDlist(DNode *Dlist,int data,int flag){
if(!lengthOver(Dlist,flag)){
return false;
}
for(int i = 0; i < flag;i++ ){
Dlist = Dlist->next;
}
Dlist->data = data;
return true;
}
int main(){
DNode *Dlist;
InitDlist(Dlist);
InsertDlist(Dlist,1);
InsertDlist(Dlist,2);
InsertDlist(Dlist,3);
InsertDlist(Dlist,4);
DelDlist(Dlist,2);
UpdateDlist(Dlist,99,2);
while(Dlist->next != NULL){
Dlist = Dlist->next;
printf("%d",Dlist->data);
}
printf("\n");
while(Dlist->prior != NULL){
printf("%d",Dlist->data);
Dlist = Dlist->prior;
}
}
数据结构--双链表的基本操作
最新推荐文章于 2022-05-03 09:05:29 发布