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

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值