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

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

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

#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
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小蒋的学习笔记

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

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

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

打赏作者

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

抵扣说明:

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

余额充值