数据结构学习之 带有头节点的循环链表

本文详细介绍了循环链表的基本操作,包括初始化、插入、查找、删除、获取长度、判空、清除、销毁和打印等。并通过C语言实现,提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#pragma once
#include <stdbool.h>
//带头节点的循环链表
//循环链表尾节点的next指向头节点
//Clist为一条链表,CNode *一个节点的地址

typedef struct CNode
{
    int data; //数据
    struct CNode *next; //下一个节点的地址
}CNode,*CList;   //CList = CNode *

//初始化
void InitList(CList plist);

//头插法
bool Insert_head(CList plist, int val);

//尾插法
bool Insert_tail(CList plist, int val);

//在pos下标插入数据val
bool Insert_pos(CList plist, int pos, int val);

//查找,找到返回节点地址,没有找到返回NULL
CNode *Search(CList plist, int key);

//删除第一个key对应的节点
bool Delete(CList plist, int key);

//删除第一个数据节点,并通过rtval获得删除的值
bool Delete_head(CList plist, int *rtval);

//删除最后一个数据节点,并通过rtval获得删除的值
bool Delete_tail(CList plist, int *rtval);

//获取长度,统计数据节点的个数
int GetLength(CList plist);

//判空
bool IsEmpty(CList plist);

//清除所有数据
void Clear(CList plist);

//销毁所有节点
void Destory(CList plist);

//打印
void Show(CList plist);

 

#include <stdio.h>
#include "clist.h"
#include <assert.h>
#include <stdlib.h>
//带头节点的循环链表
//循环链表尾节点的next指向头节点
//Clist为一条链表,CNode *一个节点的地址

/*
typedef struct CNode
{
    int data; //数据
    struct Node *next;  //下一个节点的地址
}Node,*List;   //CList = CNode *
*/

//初始化
void InitList(CList plist)
{
    assert(plist != NULL);
    if(plist == NULL)
    {
	return ;
    }

    plist->next = plist;  //循环链表:指向头节点
}

static CNode *BuyNode(int val)
{
    //生成新节点
    CNode *p = (CNode *)malloc(sizeof(CNode));
    p->data = val;

    return p;
}

//头插法
bool Insert_head(CList plist, int val)
{
    assert(plist != NULL);
    CNode *p = BuyNode(val);

    p->next = plist->next;
    plist->next = p;

    return true;
}

//尾插法
bool Insert_tail(CList plist, int val)
{
    CNode *p = BuyNode(val);
    assert(plist != NULL);
    if(p == NULL)
    {
	return false;
    }
    CNode *q;
    for(q = plist; q->next != NULL; q = q->next)
    ;
    //将q插入到p的后面
    q->next = p->next;
    p->next = q;

    return true;
}

//在pos下标插入数据val
bool Insert_pos(CList plist, int pos, int val)
{
    assert(plist != NULL);
    if(pos < 0 || pos > GetLength(plist))
    {
	return false;
    }
    CNode *p = plist;
    for( int i = 0; i < pos; i++)
    {
	p = p->next;
    }
    CNode *q = BuyNode(val);
    q->next = p->next;
    p->next = q;

    return true;
}

//查找,找到返回节点地址,没有找到返回NULL
CNode *Search(CList plist, int key)
{
    assert(plist != NULL);
    for(CNode *p = plist->next; p != plist; p = p->next)
    {
	if(p->data == key)
	{
	    return p;
	}
    }

    return NULL;
}

//查找前趋
static CNode *SearchPri(CList plist, int key)
{
    assert(plist != NULL);
    for(CNode *p = plist; p->next != plist; p = p->next)
    {
	if(p->next->data == key)
	{
	    return p;
	}
    }

    return NULL;
}

//删除第一个key对应的节点
bool Delete(CList plist, int key)
{
    assert(plist != NULL);
    CNode *p = SearchPri(plist, key);
    if(p == NULL)
    {
	return false;
    }
    CNode *q = p->next; //q为要删除的节点
    p->next = q->next;
    free(q);

    return true;
}

//删除第一个数据节点,并通过rtval获得删除的值
bool Delete_head(CList plist, int *rtval)
{
    assert(plist != NULL);
    if(IsEmpty(plist))
    {
	return false;
    }
    if(rtval != NULL)
    {
	*rtval = plist->next->data;
    }
    CNode *p = plist->next;
    plist->next = p->next;
    free(p);

    return true;
}

//删除最后一个数据节点,并通过rtval获得删除的值
bool Delete_tail(CList plist, int *rtval)
{
    assert(plist != NULL);
    if(IsEmpty(plist))
    {
	return false;
    }
    CNode *p;
    for(p = plist; p->next->next != plist; p = p->next)
    ;
    free(p->next);
    p->next = plist;

    return true;
}

//获取长度,统计数据节点的个数
int GetLength(CList plist)
{
    //检查每一个节点,如果当前节点不是头节点,则length++
    int count = 0;
    for(CNode *p = plist->next; p != plist; p = p->next)
    {
	count++;
    }
    /*
    CNode *p;
    p = plist->next; //当前节点
    while(p != plist)
    {
	p = p->next; //检查当前节点的下一个节点
	count++;
    }
    */

    return count;
}

//判空
bool IsEmpty(CList plist)
{
    //只需要要判断当前节点的下一个节点是否是头节点
    if(plist->next == plist)
    {
	return true;
    }

    return false;
}

//清除所有数据
void Clear(CList plist)
{
    Destory(plist);
}

//销毁所有节点
void Destory(CList plist)
{
    CNode *p;
    while(plist->next != plist)
    {
	p = plist->next;
	plist->next = p->next;
	free(p);
    }
}

//打印
void Show(CList plist)
{
    for(CNode *p = plist->next; p != plist; p = p->next)
    {
	printf("%d ", p->data);
	
    }
    printf("\n");
}

 

#include <stdio.h>
#include "clist.h"

int main()
{
    CNode list;
    InitList(&list);
    for(int i = 0; i < 10; i++)
    {
	Insert_head(&list,i);
    }
    Show(&list);
    
    Insert_pos(&list,0,18);
    Insert_pos(&list,11,20);
    Show(&list);

    Delete(&list,18);
    Show(&list);

//    Delete_head(&list,1);
    Show(&list);
 //   Delete_tail(&list,20);
    Show(&list);

    Clear(&list);

    return 0;
}

 

### 回答1: 建立带有头节点的链表,可以使用以下步骤: 1. 定义链表节点的结构体,包含数据域和指向下一个节点的指针域。 2. 定义头节点头节点不存储任何数据,只是作为链表的起点,其指针域指向第一个节点。 3. 动态申请节点空间,将数据存储到节点中,将节点插入到链表中。 4. 遍历链表,访问节点数据。 下面是一个简单的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 typedef struct node { int data; struct node *next; } Node; int main() { // 定义头节点 Node *head = (Node*)malloc(sizeof(Node)); head->next = NULL; // 插入节点 for (int i = 0; i < 5; i++) { Node *p = (Node*)malloc(sizeof(Node)); p->data = i; p->next = head->next; head->next = p; } // 遍历链表 Node *p = head->next; while (p != NULL) { printf("%d ", p->data); p = p->next; } printf("\n"); // 释放节点空间 p = head; while (p != NULL) { Node *q = p->next; free(p); p = q; } return 0; } ``` 在该示例代码中,我们定义了一个 `Node` 结构体表示链表节点,其中 `data` 表示数据域,`next` 表示指针域。然后定义了头节点 `head`,并将其指针域设置为 `NULL`。接下来,使用 `for` 循环向链表中插入 5 个节点,每个节点的数据域为 `i`,指向下一个节点的指针域为当前头节点的指针域。最后遍历链表,输出每个节点的数据域。最后释放节点空间。 ### 回答2: 建立带有头节点的链表是一种常见的链表数据结构的实现方式。头节点是位于链表首部的一个特殊节点,它不存储任何数据,只用于表示链表的起始位置。下面是一个简单的方法来建立带有头节点的链表: 1. 首先,我们需要定义链表节点的结构,通常包括两个部分:数据域和指针域。数据域用于存储节点的数据,指针域用于指向下一个节点。 2. 创建头节点,使用一个特定值来初始化它的数据域,一般为空或者默认值。然后将指针域指向空。 3. 创建其他节点,按照需要依次添加到链表中。对于每个节点,先创建一个新的节点对象,然后将它的数据域填充为要存储的数据值。接下来,将指针域指向链表中的下一个节点。 4. 将最后一个节点的指针域指向空,表示链表结束。 通过上述步骤,我们就成功地建立了一个带有头节点的链表。可以通过遍历链表的方式,依次访问链表中的每个节点,并对节点中的数据进行操作。 带有头节点的链表相对于不带头节点的链表具有以下几个优点: 1. 可以处理空链表的情况,头节点作为链表的起点可以防止链表为空时的异常情况。 2. 方便插入和删除操作,头节点的存在使得在链表的任意位置插入或删除节点时,不需要对链表是否为空进行额外判断。 3. 简化链表的遍历操作,头节点作为链表起始点,可以直接从头节点开始遍历整个链表。 因此,在实际应用中,使用带有头节点的链表更为普遍。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值