双向循环链表

直接贴代码:

/**
带空表头结点的双向循环链表
                by 左鸢
                //请忽略我捉急的英文--
**/
#include<stdio.h>
#include<stdlib.h>

typedef int data_type;      /*双向链表结点数据域类型*/
typedef struct node{
    data_type data;
    struct node *llink;
    struct node *rlink;
}Node;                     /*双向链表数据结点数据结构*/

Node* createNode()
{
    Node* p;
    if((p=(Node*)malloc(sizeof(Node)))==0)
    {
        printf("no space left!\n");
    }
    return p;
}

Node* CreateList(Node* head,int number)
{
    int i;
    head=createNode();
    head->data=0;
    head->rlink=head->llink=head;//构造空表头结点

    Node *q,*p=head;//q作为遍历链表的结点,p用于记录链表位置
    for(i=0;i<number;i++)
    {
        q=createNode();
        printf("please input the %d data of the list:",i+1);
        scanf("%d",&q->data);
        //从左至右链接链表
        p->rlink=q;
        q->llink=p;

        p=q;//p前驱一个结点指向q

        //从右至左链接链表,将链表链接至头结点
        q->rlink=head;
        head->llink=q;
    }
    return head;
}

void destroyList(Node *head)
{
    if(head!=0)
    {
        Node *q,*p=head->rlink; /* p指向第一个结点 */

        while(p!=head) /* p没到表头 */
        {
            q=p->rlink;
            free(p);
            p=q;
        }
        free(head);
    }
}

Node* insert_Node(Node *head,data_type data)
{
    /*总是在表尾插入*/
    Node *p;
    p=createNode();
    p->data=data;

    //从左至右链接链表,head->llink是尾结点
    head->llink->rlink=p;
    p->rlink=head;
    //从右至左链接链表,将链表链接至头结点
    p->llink=head->llink;
    head->llink=p;
    return head;
}


//链表结点的查找,x为数据域
Node* search_Node(Node *head,data_type x)
{
    Node* p;
    int i=0;
    data_type y;
    for(p=head->rlink;p!=head;p=p->rlink)
    {
        y=p->data;
        i++;
        if(y==x)
        {
            printf("having found data!\nthe data is in the %d node\n",i);

            return p;
        }
    }
    printf("not find data!\n");
    return head;
}


Node* delete_Node(Node *head,data_type x)
{
    Node* p=search_Node(head,x);
    //从左至右链接链表
    if(p!=head)
    {
        p->llink->rlink=p->rlink;
        p->rlink->llink=p->llink;
        printf("delete successfully!\n");
    }
    return head;
}

void print(Node* head)
{
    Node *n;
    int i=0;
    if(head==0)
    {
        return;
    }
    for(n=head->rlink;n!=head;n=n->rlink)
    {
        i++;
        printf("the %d node's data is:%d \n",i,n->data);
    }
    printf("\n\n");
}


void menu()
{
    system("cls");
    printf("menu:\n\n");
    printf("----\t1.create list\n");
    printf("----\t2.insert node\n");
    printf("----\t3.delete node\n");
    printf("----\t4.find node\n");
    printf("----\t5.print list\n");
    printf("----\t0.exit\n");
    printf("please input your choice:");
}

int main()
{
    int number;
    Node *head=0;
    data_type data;

    int op;
    do
    {
        menu();
        scanf("%d",&op);
        switch(op)
        {
        case 1:
            {
                system("cls");
                printf("please input number:\n");
                scanf("%d",&number);
                head=CreateList(head,number);
                printf("create successfully\n");
                system("pause");
                break;
            }
        case 2:
            {
                system("cls");
                printf("please input the data you want to insert:");
                scanf("%d",&data);
                insert_Node(head,data);
                printf("insert successfully!\n");
                system("pause");
                break;
            }
        case 3:
            {
                system("cls");
                printf("please input the data you want to delete:");
                scanf("%d",&data);
                Node *n;
                head=delete_Node(head,data);
                system("pause");
                break;
            }
        case 4:
            {
                system("cls");
                printf("please input the data you want to search:");
                scanf("%d",&data);
                search_Node(head,data);
                system("pause");
                break;
            }
        case 5:
            {
                system("cls");
                print(head);
                system("pause");
                break;
            }
        defaul:break;
        }
    }while(op);

    destroyList(head);//销毁双向循环链表
    printf("destroy list successfully!\n");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值