双向循环链表

来源:互联网 发布:网络延长器价格 编辑:程序博客网 时间:2024/06/18 02:59

双向循环链表

双向链表其实就是普通的单链表的每个结点再加上一个指向前驱的指针
如果再将头尾链接起来,就构成了双向循环链表

示例代码

#include <stdio.h>typedef struct DuLNode{    int data;    struct DuLNode *prior, *next;}DuLNode;DuLNode* createNullList();         /**创建空链表的函数的声明**/int isNullList(DuLNode*);      /**判断是否链表是否为空的函数的声明(Y-1,N-0)**/int insertNode(DuLNode*, int, int); /**在双向链表某位置插入结点的函数的声明**/int delNode(DuLNode*, int, int*); /**删除双向链表某位置插入结点的函数的声明**/void print_L(DuLNode*);void print_hyphen(int);int main(void){    DuLNode *head;    head = createNullList();    if(head == NULL){        printf("链表初始化失败!\n");        exit(1);    }    printf("链表初始化成功!\n");    int end = 0;    int ope;    while(!end){        print_hyphen(15); printf("\n");        printf("请输入指令来执行操作\n");        print_hyphen(15); printf("\n");        printf("1、判断双向循环链表是否为空\n2、在链表某位置插入新结点\n3、删除链表某位置的结点\n4、退出\n");        print_hyphen(15); printf("\n");        printf("输入要使用的功能的序号: ");        scanf("%d", &ope);        switch(ope){            case 1:                if(isNullList(head))       /**判断是否链表是否为空的函数的调用(Y-1,N-0)**/                    printf("链表为空!\n");                else                    printf("链表不为空!\n");                break;            case 2:{                int inset, insnum;                printf("请输入要插入到链表的数据的位置: ");                scanf("%d", &inset);                printf("请输入要插入的数据: ");                scanf("%d", &insnum);                if(insertNode(head, inset, insnum))                    printf("插入成功!\n");                else                    printf("插入失败!\n");                print_L(head);                break;            }            case 3:{                int delset, delnum;                printf("请输入要删除的结点的位置: ");                scanf("%d", &delset);                if(delNode(head, delset, &delnum)){                    printf("删除成功!删除的数据为:%d\n", delnum);                }                else                    printf("删除失败!\n");                print_L(head);                break;            }            case 4:                printf("再见!\n");                end = 1;                break;            default:                printf("无此序号,请重新输入!\n");        }    }    return 0;}DuLNode* createNullList()         /**创建空双向循环链表的函数的定义**/{    DuLNode* head = NULL;    head=(DuLNode*)malloc(sizeof(DuLNode));    if(head!=NULL){        head->prior = head->next = head;        printf("Has create null list success!\n");    }    else        printf("Out of space!\n");    return head;}int isNullList(DuLNode *head)      /**判断双向循环链表是否为空的函数的定义(Y-1,N-0)**/{    return (head->prior == head && head->next == head);}int insertNode(DuLNode *head, int set, int n)    /**在某位置插入结点的函数的定义**/{    DuLNode *pnew = (DuLNode*)malloc(sizeof(DuLNode));    if(pnew == NULL){        printf("Out of space!\n");        return 0;    }    DuLNode *p;    p = head;    while(set--)        p = p->next;    p = p->prior;    pnew->data = n;    pnew->next = p->next;    pnew->prior = p;    p->next->prior = pnew;    p->next = pnew;    return 1;}int delNode(DuLNode *head, int set, int *n)  /**删除某位置的结点的函数的定义**/{    DuLNode *p = head, *pdel;    while(set--)        p = p->next;    pdel = p;    *n = pdel->data;    p = pdel->prior;    pdel->next->prior = p;    p->next = pdel->next;    free(pdel);    return 1;}void print_L(DuLNode *head){    printf("\n当前链表中的数据为:\n");    DuLNode *p = head->next;    while(p != head){        printf("%d ", p->data);        p = p->next;    }    printf("\n");}void print_hyphen(int n){    while(n--)        printf("-");}
原创粉丝点击