C单向循环链表存储数据

单向循环链表

#include <stdio.h>
#include <stdlib.h>

typedef struct SinglyCycleList
{
    int data;
    struct SinglyCycleList *next;
}Node,*pNode;

//创建链表
void CreateSinglyCycleList(pNode *head)
{
    (*head) = (pNode)malloc(sizeof(Node));
    if((*head) == NULL)
    {
        printf("内存分配失败\r\n");
    }
    (*head)->data = 10;
    (*head)->next = (*head);
}

//创建节点
pNode CreateNode(int data)
{
    pNode newnode = (pNode)malloc(sizeof(Node));
    newnode->data = data;
    newnode->next = NULL;
    return newnode;
}

//获取最后一个节点
pNode GetLastNode(pNode head)
{
    pNode ptr = NULL;
    ptr = head->next;
    while(ptr != head)
    {
        ptr = ptr->next;
    }
    return ptr;
}

//获取链表长度
int GetListLength(pNode head)
{
    int i = 0;
    pNode ptr = NULL;
    ptr = head->next;
    while(ptr != head)
    {
        i++;
        ptr = ptr->next;
    }
    return i;
}

//找到某一数据的上一个节点位置
pNode FindPosNode(pNode head,int data)
{
    int i,index = 0;
    pNode ptmp = head;
    pNode ptr = head->next;
    while(ptr != head)
    {
        if(ptr->data == data)
            break;
        ptr = ptr->next;
        index++;
    }
    if(index == GetListLength(head))
    {
        printf("Not find node data!!!\r\n");
        return NULL;
    }
    for(i=0; i<index; i++)
        ptmp = ptmp->next;
    return ptmp;
}

//找到某一下标节点
int FindIndexNode(pNode head,int index)
{
    int i = 0;
    pNode ptr = head->next;
    while(ptr != head)
    {
        if(i == index)
            break;
        i++;
        ptr = ptr->next;
    }
    if((head->next == head) || i== GetListLength(head))
        return 0;
    else
        return i;
}

//头部插入节点
void InsertHeadNode(pNode head, int data)
{
    pNode newnode = CreateNode(data);
    pNode ptr = head->next;
    head->next = newnode;
    newnode->next = ptr;
}

//尾部插入节点
void InsertTailNode(pNode head, int data)
{
    pNode newnode = CreateNode(data);
    pNode ptr = head;
    while(ptr->next != head)
    {
        ptr = ptr->next;
    }
    ptr->next = newnode;
    newnode->next = head;
}

//在任意位置插入节点
void InsertPosNode(pNode pos, int data)
{
    pNode ptr = NULL;
    pNode newnode = CreateNode(data);
    ptr = pos->next;
    pos->next = newnode;
    newnode->next = ptr;
}

//循环存储数据
void SaveDataToList(pNode head, int *buf, int len)
{
    int i;
    pNode ptr = head;
    for(i=0; i<len; i++)
    {
        if(ptr == head)
            ptr = ptr->next;
        ptr->data = buf[i];
        ptr = ptr->next;
    }
}
//删除某一数据的节点
void DeleteNode(pNode head, int data)
{
    int i,index = 0;
    pNode ptmp = head;
    pNode ptr = head->next;
    while(ptr != head)
    {
        if(ptr->data == data)
            break;
        ptr = ptr->next;
        index++;
    }
    if(index >= GetListLength(head))
        printf("Not this data node!!!\n");
    else
    {
        for(i=0; i<index; i++)
        ptmp = ptmp->next;
        ptmp->next = ptr->next;
        free(ptr);
    }
}

//删除下标节点
void DeleteIndexNode(pNode head, int index)
{
    int i;
    pNode ptmp = head;
    pNode ptr = NULL;
    if(ptmp->next == head)
        return ;
    if(index >= 0 && index < GetListLength(head))
    {
        for(i=0; i<index; i++)
        ptmp = ptmp->next;
        ptr = ptmp->next;
        ptmp->next = ptr->next;
        free(ptr);
    }
    else
        printf("Not this node index!!!\r\n");
}

//顺序遍历链表
void TraverseList(pNode head)
{
    pNode ptr = NULL;
    ptr = head->next;
    while(ptr != head)
    {
        printf("%d ",ptr->data);
        ptr = ptr->next;
    }
    printf("\n");
}


int main()
{
    int i;
    int buf[15];
    pNode Head;
    pNode Pos;
    CreateSinglyCycleList(&Head);
    for(i=1; i<=10; i++)
        InsertTailNode(Head,0);
    TraverseList(Head);
    for(i=0; i<15; i++)
        buf[i] = i+1;
    SaveDataToList(Head, buf, 15);
    TraverseList(Head);
    InsertHeadNode(Head,13);
    TraverseList(Head);
    DeleteNode(Head,8);
    TraverseList(Head);
    DeleteIndexNode(Head,9);
    TraverseList(Head);
    printf("List len: %d\r\n",GetListLength(Head));
    Pos = FindPosNode(Head,9);
    InsertPosNode(Pos,99);
    TraverseList(Head);
    printf("index 0: %d\r\n",FindIndexNode(Head,5));
    DeleteIndexNode(Head,FindIndexNode(Head,5));
    TraverseList(Head);
    return 0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值