5、循环双链表

#include<stdio.h>
#include<assert.h>
#include<malloc.h>

typedef int ElemType;

typedef struct Node{
    ElemType data;
    struct Node* prior;
    struct Node* next; 
}Node,*PNode;

typedef struct DCList{
    PNode first;
    PNode last;
    int size;
}DCList;

Node* malloc_node(ElemType e){
    Node* node = (Node*)malloc(sizeof(Node));
    assert(node != NULL);
    node->next = node->prior = NULL;
    node->data = e;
    return node;
}

//初始化循环链表
void init_DCList(DCList* list){
    Node* node = malloc_node(-1);
    list->first = list->last = node;
    list->first->prior = list->last;
    list->last->next = list->first;
    list->size = 0;
} 

//尾插
void push_back(DCList* list,ElemType e){
    Node* s = malloc_node(e);
    
    s->next = list->last->next;
    s->next->prior = s;
    s->prior = list->last;
    list->last->next = s;
    list->last = s;
    list->size++;
} 

//头插
void push_front(DCList* list,ElemType e){
    Node* s = malloc_node(e);
    
    s->next = list->first->next;
    s->prior = list->first;
    list->first->next = s;
    s->next->prior = s;
    if(list->first == list->last){
        list->last = s;
    }
    list->size++;
}
//打印链表
void show_list(DCList* list){
    Node* p = list->first->next;
    while(p != list->first){
        printf("%d<=>",p->data);
        p = p->next;
    }
    printf("NULL .\n");
} 

//删除末尾节点
void pop_back(DCList* list){
    if(list->size == 0){
        printf("当前链表没有元素,不可删除.\n");
        return;
    }
    Node* p = list->last;
    list->last = list->last->prior;
    
    p->next->prior = p->prior;
    p->prior->next = p->next;
    free(p);
    list->size--;    
}

//删除首节点
void pop_front(DCList* list){
    if(list->size == 0){
        printf("当前链表没有元素,不可删除.\n");
        return;
    }
    Node* p = list->first->next;
    p->prior->next = p->next;
    p->next->prior = p->prior;
    if(p == list->last){
        list->last = list->first;
    } 
    free(p);
    list->size--;
}

//插入节点[默认链表已升序排序]
void insert_val(DCList* list,ElemType e){
    Node* s = malloc_node(e);
    Node* p = list->first;
    while(p->next != list->first && p->next->data < s->data)
        p = p->next;
    s->next = p->next;
    s->prior = p;
    s->next->prior = s;
    s->prior->next = s;
    //处理插入最后一个位置的情况
    if(p == list->last){
        list->last = s;
    } 
    list->size++; 
}

//查找节点
Node* find(DCList* list,ElemType key){
    Node* p = list->first->next;
    while(p != list->first){
        if(p->data == key){
            return p;
        }
        p = p->next;
    }
    return NULL;
} 

//返回链表长度
int length(DCList* list) {
    return list->size;
}

//按值删除
void delete_val(DCList* list,ElemType e){
    if(list->size == 0){
        return;
    }
    Node* p = find(list,e);
    if(p == NULL){
        printf("删除节点不存在,不可删除. \n");
        return;
    }
    p->next->prior = p->prior;
    p->prior->next = p->next;
    if(p == list->last){
        list->last = p->prior;
    }
    free(p);
    list->size--;
}

//排序
void sort(DCList* list){
    if(list->size <= 1){
        return;
    }
    Node* s = list->first->next;
    Node* q = s->next;
    
    list->last->next = NULL;
    list->last = s;
    list->last->next = list->first;
    list->first->prior = list->last;
    
    while(q !=     NULL){
        s = q;
        q = q->next;
        Node* p = list->first;
        while(p->next != list->first && p->next->data < s->data){
            p = p->next; 
        }
        s->next = p->next;
        s->prior = p;
        s->next->prior = s;
        s->prior->next = s;
        if(p == list->last){
            list->last = s;
        }
    }
}

//逆置链表
void reverse(DCList* list){
    if(list->size <= 1)
        return;
    Node* s = list->first->next;
    Node* q = s->next;
    
    list->last->next = NULL;
    list->last = s;
    list->last->next = list->first;
    list->first->prior = list->last;
    
    while(q != NULL){
        s = q;
        q = q->next;
        
        s->next = list->first->next;
        s->prior = list->first;
        s->next->prior = s;
        s->prior->next = s;
    } 
}

//清空链表
void clear(DCList* list){
    if(list->size == 0){
        return;
    }
    Node* p = list->first->next;
    while(p != list->first){
        p->next->prior = p->prior;
        p->prior->next = p->next;
        free(p);
        p = list->first->next;
    }
    list->last = list->first;
    list->size = 0; 
} 

//销毁链表
void destroy(DCList* list){
    clear(list);
    free(list->first);
    list->first = list->last = NULL;
}

int main(){
    DCList list;
    init_DCList(&list);
    int select = 1;
    ElemType item = 0;
    while(select){
        printf("\n");
        printf("***********************************\n");
        printf("* [1] push_back   [2] push_front  *\n");        
        printf("* [3] show_list   [4] pop_back    *\n");
        printf("* [5] pop_front   [6] insert_val  *\n");
        printf("* [7] find        [8] length      *\n");
        printf("* [9] delete_val  [10] sort       *\n");
        printf("* [11] reverse    [12] clear      *\n");
        printf("* [13] destroy    [0] exit_system *\n");
        printf("***********************************\n");
        printf("请选择> ");
        scanf("%d",&select);
        if(!select){
            break;
        }
        PNode p = NULL;
        switch(select){
            case 1:
                printf("请输入要输入的数据(-1结束):>");
                while(scanf("%d",&item),item != -1){
                    push_back(&list,item);
                }
                break;
            case 2:
                printf("请输入要输入的数据(-1结束):>");
                while(scanf("%d",&item),item != -1){
                    push_front(&list,item);
                }
                break;
            case 3:
                show_list(&list);
                break;
            case 4:
                pop_back(&list);
                break;
            case 5:
                pop_front(&list);
                break;
            case 6:
                printf("请输入要插入的数据:>");
                scanf("%d",&item);
                insert_val(&list,item);
                break;
            case 7:
                printf("请输入要查找的数据:>");
                scanf("%d",&item);
                p = find(&list,item);
                if(p == NULL){
                    printf("查找的数据不存在.\n");
                }
                break;
            case 8:
                printf("该链表长度为: %d.\n",length(&list));
                break;
            case 9:
                printf("请输入要删除的数据:>");
                scanf("%d",&item);
                delete_val(&list,item);
                break;    
            case 10:
                sort(&list);
                break;
            case 11:
                reverse(&list);
                break;
            case 12:
                clear(&list);
                break;
            case 13:
                destroy(&list);
                break; 
        }    
    }
    destroy(&list);
    return 0;
}

 

posted @ 2024-09-16 15:44  颜欢兮  阅读(3)  评论(0编辑  收藏  举报