双链表插入删除

#include 


#include 
#include 

using namespace std;

//带头指针的双链表

typedef  struct  DNode{
    int data;
    struct DNode *next,*prior;

}DNode, *DLinkList;

bool  init_DInitList( DLinkList &L){
    L = (DNode *)malloc(sizeof(DNode));
    if(L == NULL)return false;
    else {
        L->next = NULL;
        L->prior = NULL;
    }
    return true;
}

//在p节点之后插入s节点
bool insert_s(DNode *p,DNode *s){
    if(p==NULL || s==NULL){
        return false;
    }
    if(p->next){
        s->next  = p->next;
        p->next->prior = s;
        s->prior = p;
        p->next = s;
    }else{
        s->next  = p->next;
        s->prior = p;
        p->next =s;
    }
    return true;

}
// 删除p节点的后继节点

bool delete_pnext(DNode *p){
    if(p->next ==NULL || p ==NULL)return  false;
    DNode *q = p;

    q=p->next;
    if(q->next != NULL){ // 如果p的后继不是最后一个节点
        q->next->prior =p;
        p->next = q->next;
    }
    q->prior = NULL;
    q->next =NULL;
    free(q);


    return true;




}


int main(){
    DLinkList L;
    init_DInitList(L);




    return 0;
}

你可能感兴趣的:(ds,c++,开发语言)