循环链表和双向链表—课上课后练

第一关:单循环链表基本操作

单向循环链表的创建,插入,输出,销毁,删除。

#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;

}int main()
{
  struct node *head = createRlist();
  int num , data;
  scanf("%d",&num);
  for(int i=0;i<num;i++)
  {
      scanf("%d",&data);
      head = insertOrder(head, data);
  }
     printRlist(head);
     scanf("%d",&data);
     printf("\n%d ",deleteData(head,data));

    printf("\n%d ",destroyRlist(head));


  return 1;
}

第2关:双向链表基本操作

双向链表的基本操作:创建,插入,打印,销毁

#include <stdio.h>
#include <stdlib.h>
struct node
{//链表结点类型,包含一个存放整型数据的 data 成员,和指向前驱和后继结点的指针
    int data ;
    struct node *llink, *rlink ;
};

struct Hnode
{
    //双向链表头结点,有两个指针成员,分别指向双向链表的第一个结点和最后一个结点
    struct node *head, *tail;
};
//第二关代码
struct Hnode *createDlist()
{//函数功能:创建一个带头结点的双向链表,tail指向尾结点;head指针指向第一个结点,返回值是指向头结点的指针
  struct Hnode* head = (struct Hnode*)malloc(sizeof(struct Hnode));
  head->head = NULL;
  head->tail = NULL;
  return head;
}

void insertDlist(struct Hnode *list, int insData)
{
 //在双向链表的表头插入数据
    if(list->head==NULL){
        struct node* node = (struct node*)malloc(sizeof(struct node));
        node->data = insData;
        node->llink = NULL;
        node->rlink = NULL;
        list->head = node;
        list->tail = node;
    }else{
        struct node* node = (struct node*)malloc(sizeof(struct node));
        node->data = insData;
        node->rlink = list->head;
        list->head->llink = node;
        list->head = node;
    }
}
void printDlist(struct Hnode *list)
{
 //输出双向循环链表中各数据元素的值,每输出一个数据元素换行
 struct node* p = list->head;
 while(p!=list->tail){
  printf("%d\n",p->data);
     p = p->rlink;
 }
 printf("%d\n",p->data);
}
int deleteData(struct Hnode  *list, int delData)
{
    //在双向链表中删除值为delData的第一个结点,若删除成功返回1,否则返回0
    struct node* p = list->head;
    while(p->data!=delData&&p!=list->tail){
        p = p->rlink;
    }
    if(p->data!=delData) return 0;
    else{
        if(p==list->head){
            list->head = p->rlink;
            free(p);
        }else if(p==list->tail){
            list->tail = p->llink;
            free(p);
        }else{
            p->llink->rlink = p->rlink;
            p->rlink->llink = p->llink;
            free(p);
        }
        return 1;
    }
}
int destroyDlist(struct Hnode *list)
{//释放双向链表占用的存储空间,释放所有结点,返回释放的结点数,不含list本身
    struct node* p = list->head;
    int count =  0;
    while(p!=list->tail){
        struct node*q = p;
        p=p->rlink;
        free(q);
        count++;
    }

    free(p);
    count++;
    list->head=NULL;
    list->tail=NULL;

    return count;
}
int main()
{
  struct Hnode *head = createDlist();
  int num , data;
  scanf("%d",&num);
  for(int i=0;i<num;i++)
  {
     scanf("%d",&data);
     insertDlist(head, data);

  }
  printDlist(head);
  scanf("%d",&data);
  printf("\n%d ",deleteData(head,data));
  printf("\n%d ",destroyDlist(head));


  return 1;
}

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
循环链表是一种将单链表尾节点的指针域置为起始节点的地址,从而形成循环连接的链表结构。这样,从链表中的任意一个节点出发,都可以遍历到链表中的所有节点。单循环链表的结构图如下所示: 双向链表是一种可以从两个方向进行遍历的链表结构,每个节点除了保存下一个节点的地址外,还保存了上一个节点的地址。双向链表的一个特点是可以利用中间的一个节点推出下一个节点和上一个节点。双向链表的结构图如下所示: 对于一个循环链表来说,不论是单向循环链表还是双向循环链表,首节点和末节点都被连接在一起。这种方式在单向和双向链表中都可以实现。要转换一个循环链表,可以选择开始于任意一个节点然后沿着列表的任一方向直到返回开始的节点。另一种方法是将循环链表的指针指向NULL来打破循环。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [数据结构——单向循环链表&双向循环链表](https://blog.csdn.net/qq_56668869/article/details/126498355)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [C语言单循环链表的表示与实现实例详解](https://download.csdn.net/download/weixin_38684892/14870959)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小蒋的学习笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值