栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

数据结构之SWUSTOJ955: 单链表上查找算法的实现

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构之SWUSTOJ955: 单链表上查找算法的实现

题目:

代码:
#include
#include
typedef struct SList
{
    int data;
    struct SList* next;
}SL;
void SListInit(SL** ps)
{
    *ps = (SL*)malloc(sizeof(SL));
    (*ps)->next = NULL;
}//建立一个哨兵位头结点
void SListCreate(SL** ps, int n)
{
    SL* cur = *ps;
    int x = 0;
    while (n)
    {
        scanf("%d", &x);
        getchar();//吸收输入的空格
        SL* newnode = (SL*)malloc(sizeof(SL*));
        newnode->data = x;
        cur->next = newnode;
        cur = newnode;
        n--;
    }
    cur->next = NULL;
}
void SListFind(SL** ps, int i)
{
    SL* cur = *ps;
    int j = 1;
    while (cur)
    {
        if (j == i)
            break;//找到j等于i的节点就跳出循环
        j++;
        cur = cur->next;
    }
    if (j == i)
        printf("ok");
    else
        printf("error");
}
int main()
{
    SL* plist = NULL;
    SListInit(&plist);
    int n = 0;
    scanf("%d", &n);
    getchar();
    SListCreate(&plist,n);
    int i = 0;
    scanf("%d", &i);
    getchar();
    SListFind(&plist, i);
    return 0;

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/779652.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号