数据结构单循环链表

本文介绍了一个简单的循环链表实现,包括初始化、插入、删除及搜索等基本操作,并提供了一个交互式菜单供用户进行链表操作。
#include <stdio.h>
#include <stdlib.h>
typedef struct CLinkLIst
{
    int data;
    struct CLinkLIst *next;
}node;

void ds_init(node **pNode)//初始化
{
    int item;
    node *temp;
    node *target;
    printf("输入节点的值,输入0完成初始化。\n");
    while (1)
    {
        scanf("%d",&item);
        fflush(stdin);//清空输入缓冲区,通常是为了确保不影响后面的数据读取(例如在读完一个字符串后紧接着又要读取一个字符,此时应该先执行fflush(stdin);)。
        if (item ==0)  
            return;

        if ((*pNode) == NULL)//循环链表只有一个节点
        {
            *pNode =(node*)malloc(sizeof(struct CLinkLIst)) ;
            if(!(*pNode))
                exit(0);
            (*pNode)->data = item;
            (*pNode)->next = *pNode;//循环链表,头尾相接
        }

        else
        {
            for (target = (*pNode);target->next != (*pNode);target = target->next)//找到next指向第一个结点的结点
                ;

            temp = (node *)malloc(sizeof(struct CLinkLIst));

            if(!temp)
                exit(0);
            temp->data = item;
            temp->next = *pNode;
            target->next = temp;
        }
    }
}

void ds_insert(node **pNode,int i)//插入结点
{
    node *temp;
    node *target;
    node *p;
    int item;
    int j = 1;
    printf("输入要插入结点的值:");
    scanf("%d",&item);

    if (i == 1)//新插入的结点作为第一个结点
    {
        temp = (node *)malloc(sizeof(struct CLinkLIst));
        if(!(*pNode))
            exit(0);
        temp->data = item;
        for (target = (*pNode);target->next != (*pNode);target = target->next)//找到最后一个结点
            ;
        temp->next = *pNode;
        target->next = temp;
        *pNode = temp;
    }
    else
    {
        target = *pNode;
        for (;j < i - 1;++j)
        {
            target = target->next;
        }
        temp = (node *)malloc(sizeof(struct CLinkLIst));
        if(!temp)
            exit(0);
        temp->data = item;
        p = target->next;
        target->next = temp;
        temp->next = p;
    }
}

void ds_delete(node **pNode,int i)//删除一个结点
{ 
    node *target;
    node *temp;
    int j = 1;
    if(i == 1)
    {
        for(target = *pNode;target->next != *pNode;target = target->next)
            ;
        temp = *pNode;
        *pNode = (*pNode)->next;
        target->next = *pNode;
        free(temp);
    }
    else
    {
        target = *pNode;
        for(;j < i - 1;++j)
        {
            target = target->next;
        }
        temp = target->next;
        target->next = temp->next;
        free(temp);
    }
}

int ds_search(node *pNode,int elem)//返回结点的位置
{
    node *target;
    int i = 1;
    for (target = pNode;target->data != elem && target->next != pNode;++i)//避免无限循环,则要满足条件target->next != pNode
    {
        target = target->next;
    }
    if (target->data == elem)//判断是不是要找的值,是就返回位置
    {
        return i;
    }
    else
        return 0;
}

void ds_printf(node *pNode)
{
    node *temp;
    temp = pNode;
    printf("循环链表中的元素:\n");
    do 
    {
        printf("%d\t",temp->data);
    } while ((temp = temp->next) != pNode);
    printf("\n");
}

int main()
{
    //node *p = NULL;
    //int i;
    //ds_init(&p);//初始化
    //ds_printf(p);
    //ds_insert(&p,1);
    //ds_insert(&p,2);
    //ds_insert(&p,3);
    //ds_printf(p);
    //i = ds_search(p,2);
    //printf("%d\n",i);
    //ds_delete(&p,3);
    //ds_printf(p);
    //printf("%d",ds_search(p,1));
    //return 0;

    node *p = NULL;
    char i = '1';
    int find;

    printf("\n1.初始化链表 \n2.插入结点 \n3.删除结点 \n4.返回结点位置 \n5.遍历链表  \n0.退出 \n请选择你的操作:\n");
    while(i != '0'){
        scanf("%c",&i);
        switch(i){
        case '1':
            ds_init(&p);
            printf("\n");
            ds_printf(p);
            break;

        case '2':
            printf("输入需要插入结点的位置:");
            scanf("%d", &find);
            ds_insert(&p,find);
            printf("在位置%d插入值后:\n", find) ;
            ds_printf(p);
            printf("\n");
            break;

        case '3':
            printf("输入需要删除的结点位置:");
            scanf("%d", &find);
            ds_delete(&p,find);
            printf("删除第%d个结点后:\n", find) ;
            ds_printf(p);
            printf("\n");
            break;

        case '4':
            printf("输入你要查找的值:");
            scanf("%d", &find);
            printf("元素%d所在位置:%d\n", find,ds_search(p,find));
            printf("\n");
            break;

        case '5':
            ds_printf(p);
            printf("\n");
            break;

        case 0:
            exit(0);
        }
    }

    return 0 ;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值