鱼C论坛

 找回密码
 立即注册
查看: 2454|回复: 1

[技术交流] linux 环境下双向循环链表简单定义及使用

[复制链接]
发表于 2015-12-2 16:11:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
刚刚开始学习 希望多多交流

初来乍到 :ton:

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

typedef int DataType;
typedef struct Node
{
        DataType data;
        struct Node *next;             //指向后继结点的指针域
        struct Node *prior;            //指向前驱结点的指针域
}DLNode;

void ListIntiate(DLNode **head)    //双向循环链表初始化   
{
        *head = (DLNode *)malloc(sizeof(DLNode));  
        (*head)->prior = *head;        //构成前驱指针循环链表
        (*head)->next = *head;         //构成后继指针循环链表
}

int ListInsert(DLNode *head,int i,DataType x) //插入数据元素
{
        DLNode *p,*q;
        int j;
        p = head->next;
        j = 0;
       
        while(p != head && j < i)     //遍历到i结点
        {
                p = p->next;
                j ++;
        }
        if(j != i)
        {
                printf("ListInsert error..\n");
                return 0;
        }

        q = (DLNode *)malloc(sizeof(DLNode));
        q->data = x;                //插入数据元素x

        q->prior = p->prior;        //插入结点的前驱结点变成原结点p的前驱结点
        p->prior->next = q;         //原结点p的前驱结点的后继结点变为插入结点q
        q->next = p;
        p->prior = q;
        return 1;
}

int ListDelete(DLNode *head,int i,DataType *x)  //删除数据元素
{
        DLNode *p;
        int j = 0;
        p = head->next;

        while(p != head && j < i)
        {
                p = p->next;
                j ++;
        }
        if(j != i)
        {
                printf("ListDelete input i is error..\n");
                return 0;
        }
        *x = p->data;
        p->prior->next = p->next;
        p->next->prior = p->prior;


        free(p);           //记得释放动态内存空间
        return 1;
}

int ListLength(DLNode *head)   //求链表长度
{
        DLNode *p;
        int length = 0;
        p = head;

        while(p->next != head)
        {
                p = p->next;
                length ++;
        }
        return length;
}

int ListGet(DLNode *head,int i,DataType *x)   //取链表数据元素
{
        DLNode *p;
    p = head->next;
        int j = 0;
        while(p != head && j<i)
        {
                p = p->next;
                j ++;
        }
        if(j != i)
        {
                printf("ListGet input i is error...\n");
                return 0;
        }
        *x = p->data;
        return 1;
}

void ListDestroy(DLNode **head)    //销毁链表
{
        DLNode *p,*q;
        p = *head;

        while(p->next != *head)
        {
                q = p;
                p = p->next;
                free(q);
        }
        *head = NULL;   //记得头结点置NULL
}

int main()          //一个小程序 调用上述函数 实现双向循环链表简单功能
{
        DLNode *head;
        int i,x;

        ListIntiate(&head);         //初始化
        for(i = 0; i < 10 ;i ++)
        {
                ListInsert(head,i,i+1);  //插入数据1到10
        }
        ListDelete(head,4,&x);       //删除第5个元素
        for(i = 0; i < ListLength(head); i++)
        {
                ListGet(head,i,&x);      //取数据元素
                printf("%d\t",x);        //打印
        }
        printf("\n");
        ListDestroy(&head);          //销毁链表

        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-7-1 15:27:58 | 显示全部楼层
谢谢分享
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-29 00:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表