数据结构-循环链表

#include <stdio.h>
#include <stdlib.h>
struct node
{//链表结点类型,包含一个存放整型数据的 data 成员,和一个指向下一个结点的next成员
    int data ;
    struct node *next ;
};
 
//第一关代码
struct node *createRlist()
{//函数功能:创建一个有一个空循环链表,返回值为头指针
    struct node* head = (struct node*)malloc(sizeof(struct node));
    head->data = 0;
    head->next = head;
    return head;
}
 
struct node * insertOrder(struct node *list, int insData)
{
//在单向递增有序的循环链表(表头指针list)中插入数据元素insData,使之依然有序 。返回值是头指针
struct node* node = (struct node*)malloc(sizeof(struct node));
node->data = insData;
struct node* q = list;
while(q->next->data<insData&&q->next!=list){
    q=q->next;
}
node->next = q->next;
q->next = node;
 
return list;
}
 
void printRlist(struct node *list)
{
 //从链表第一个结点开始输出单向循环链表中各数据元素的值,每输出一个数据元素空一格
    struct node* p = list->next;
    while(p!=list){
        printf("%d ",p->data);
        p = p->next;
    }
 
}
 
int deleteData(struct node  *list, int delData)
{
    //在单向递增有序循环链表(表头指针list)中删除所有值为delData的结点,返回值为删除结点的个数
    int count = 0;
    struct node* p = list->next;
    struct node* q = list;
    while(p!=list){
        if(p->data==delData){
            struct node* k = p;
            p=p->next;
            q->next = p;
            free(k);
            count++;
            continue;
        }
        p = p->next;
        q = q->next;
    }
    return count;
}
int destroyRlist(struct node *list)
{
    //从第一个结点开始释放循环链表各结点占用的空间,返回值为最后一个结点的值
    struct node* p = list->next;
    while(p->next!=list){
        struct node* q = p;
        p = p->next;
        free(q);
    }
    int temp = p->data;
    free(p);
    free(list);
    return temp;
 
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值