C 数据结构中双链表基本操作

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/dcrmg/article/details/83756372

双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>


//定义双链表结点
typedef struct dNode{
    int data;
    struct dNode* pre;
    struct dNode* next;
}dnode;

//创建双链表
dnode* create_dnode(const int length){
    dnode* head = (dnode*)malloc(sizeof(dnode));
    dnode *p, *s;
    p = head;
    for(int i=0;i<length;i++){
        s = (dnode*)malloc(sizeof(dnode));
        s->data=i;                                                                                                                                                                                                                                                                                                                                                                                                                                        
        p->next = s;
        s->pre = p;
        p = s;
    }
    p->next = NULL;
    head = head->next;
    head->pre = NULL;
    return head;
}

//打印双链表
void print_dnode(dnode* dNode){
    dnode* head = dNode;
    while(head!=NULL){
        printf("%d\n",head->data);
        head = head->next;
    }   
}

//逆序打印双链表
void print_dnode_reverse(dnode* dNode){
    dnode* p = dNode->next;
    while(p->next!=NULL){
        p=p->next;
    }
    while(p!=NULL){
        printf("%d\n",p->data);
        p=p->pre;
    }
}

//双链表测长(递归)
int length_dnode(dnode* dNode){
    if(dNode->next == NULL){
        return 1;
    }
    return 1+length_dnode(dNode->next);
}

//双链表测长(非递归)
int length_dnode_(dnode* dNode){
    int length = 0;
    for(dnode* p=dNode;p!=NULL;p=p->next){
        length+=1;
    }
    return length;
}

//双链表头部插入
dnode* insert_head(dnode* dNode, int insert_data){
    dnode* p = (dnode*)malloc(sizeof(dnode));
    p->data = insert_data;
    p ->next = dNode;
    dNode->pre = p;
    p->pre = NULL;
    return p;
}

//双链表尾部插入
dnode* insert_end(dnode* dNode, int insert_data){
    dnode* p;
    dnode* end = (dnode*)malloc(sizeof(dnode));
    for(p=dNode;p->next!=NULL;p=p->next);
    end->data = insert_data;
    end->next = NULL;
    end->pre = p;
    p->next = end;
    return dNode;   
}

//双链表删除指定数据
dnode* delete_key(dnode* dNode, int key){
    dnode* p;
    for(p=dNode;p->data!=key&&p!=NULL;p=p->next);    
    if(p==NULL){
        return dNode;
    }
    else{
        p->pre->next = p->next;
        free(p);
    }
    return dNode;
}

//双链表按排序插入数据
dnode* insert_key_sort(dnode* dNode, const int key){
    dnode* p = dNode;
    dnode* new_dnode = (dnode*)malloc(sizeof(dnode));
    while(p->data<=key&&p->next!=NULL){
        p= p->next;
    }
    if(p->next==NULL){
        new_dnode->data = key;
        new_dnode->next = NULL;
        new_dnode->pre = p;
        p->next = new_dnode;       
    }
    else{
        new_dnode->data = key;
        new_dnode->next = p;
        new_dnode->pre = p->pre;
        p->pre->next = new_dnode;        
    }
    return dNode;
}

//销毁双链表
void destroy_dnode(dnode* dNode){
    dnode* p = dNode->next;
    while(p->next!=NULL){
        dNode->next = p->next;
        free(p);
        p = dNode->next;
    }
    free(dNode);    
}

void main(){
    dnode* dNode = create_dnode(10);
    print_dnode(dNode);   
    print_dnode_reverse(dNode);
    dNode = insert_end(dNode,99);
    dNode = insert_key_sort(dNode,11);
    print_dnode(dNode);  
    return ;
}            

                                                                                                                                                                                                             

猜你喜欢

转载自blog.csdn.net/dcrmg/article/details/83756372