| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1490 人关注过本帖
标题:线性表中查询数据
只看楼主 加入收藏
WL2311296974
Rank: 1
来 自:安徽
等 级:新手上路
帖 子:37
专家分:7
注 册:2017-3-30
结帖率:90%
收藏
已结贴  问题点数:10 回复次数:9 
线性表中查询数据
各位大神,帮忙看看哪里错了!!!谢谢了!!!




#include "stdio.h"
#define maxsize 10
typedef struct
{
    int data[maxsize];
    int last;                //向线性表输入数据的个数
}List;
void Scanf_List(List*p)
{
    int i;
    for(i=1;i<=p->last;i++)
    {
        scanf("%d",&p->data[i]);
    }
}
void Printf_List(List*p)
{
    int i;
    for(i=1;i<=p->last;i++)
    {
        printf(" \n\n\n%d ",p->data[i]);
    }
    printf("\n");
}
void Locate(int i,int x,List*ptr)
{
    for(i=0;i<ptr->last;i++)
    {
        if(ptr->data[i]==x)
           {
               printf("%d\n",ptr->data[i]);
           }
           printf("No!!!!!!!!!!!!!!!");
    }
    ptr->data[i]=x;
    return;
}
main()
{
    List n;
    printf("请输入线性表的长度:\n");
    while(1)
    {
        scanf("%d",&n.last);
        if(n.last<maxsize)
        {
            break;
        }
        printf("输入的线性表长度必须小于%d,请重新输入\n",maxsize);
        
    }
    printf("请输入线性表的数据:\n");
    Scanf_List(&n);
    Locate_List(3,&n);
    Printf_List(&n);
}
搜索更多相关主题的帖子: 线性表 include 
2017-03-30 21:04
marlow
Rank: 6Rank: 6
等 级:侠之大者
威 望:2
帖 子:125
专家分:419
注 册:2016-7-18
收藏
得分:5 
改成这样,是不是可以?
程序代码:
#include "stdio.h"
#define maxsize 10
typedef struct
{
    int data[maxsize];
    int last;                //向线性表输入数据的个数 
}List;
void Scanf_List(List*p)
{
    int i;
    for(i=1;i<=p->last;i++)
    {
        scanf("%d",&p->data[i]);
    }
}
void Printf_List(List*p)
{
    int i;
    for(i=1;i<=p->last;i++)
    {
        printf(" \n\n\n%d ",p->data[i]);
    }
    printf("\n");
}
void Locate(int x,List*ptr)
{
    int i;
    
    for(i=0;i<ptr->last;i++)
    {
        if(ptr->data[i]==x)
           {
               printf("ptr->data[%d] = %d\n",i, ptr->data[i]);
               return;
           }
         continue;
    }
    return; 
}
main()
{
    List n;
    int data;
    printf("请输入线性表的长度:\n");
    while(1)
    {
        scanf("%d",&n.last);
        if(n.last<maxsize)
        {
            break;
        }
        printf("输入的线性表长度必须小于%d,请重新输入\n",maxsize); 
        
    }
    printf("请输入线性表的数据:\n");
    Scanf_List(&n);
    Printf_List(&n); 
    printf("请输入想要查询的数据:\n");
    scanf("%d", &data);
    Locate(data,&n);
}


[此贴子已经被作者于2017-3-31 08:11编辑过]


一切都在学习、尝试、摸索中
2017-03-31 08:08
WL2311296974
Rank: 1
来 自:安徽
等 级:新手上路
帖 子:37
专家分:7
注 册:2017-3-30
收藏
得分:0 
谢谢了!!!!!



但是还是不行,查询不到数据!

我的类不是你的泪
2017-03-31 14:23
renkejun1942
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:不是这样
等 级:贵宾
威 望:33
帖 子:1645
专家分:5297
注 册:2016-12-1
收藏
得分:0 
void Locate(int i,int x,List*ptr)


Locate_List(3,&n);

函数名、参数数量不匹配。
我找了很久都没找到locate_List这个函数,我看漏了?还是你没发上来?


程序代码:
void Scanf_List(List*p)
{
    int i;
    for(i=1;i<=p->last;i++)//这个i的初值怎么看怎么怪,当然可能是我不理解你的意图吧。
    {
        scanf("%d",&p->data[i]);
    }
}
void Printf_List(List*p)
{
    int i;
    for(i=1;i<=p->last;i++)//这个i的初值怎么看怎么怪,当然可能是我不理解你的意图吧。
    {
        printf(" \n\n\n%d ",p->data[i]);
    }
    printf("\n");
}




[此贴子已经被作者于2017-3-31 15:53编辑过]


09:30 05/21 种下琵琶种子,能种活么?等待中……
21:50 05/27 没有发芽。
20:51 05/28 没有发芽。
23:03 05/29 没有发芽。
23:30 06/09 我有预感,要发芽了。
2017-03-31 15:51
WL2311296974
Rank: 1
来 自:安徽
等 级:新手上路
帖 子:37
专家分:7
注 册:2017-3-30
收藏
得分:0 
void Locate(int i,int x,List*ptr)   有Locate 这个函数啊,是我把函数名写错了吗,是Locate_List还是Locate






i 初值是从1开始的,避免了输入0的尴尬情况

我的类不是你的泪
2017-03-31 16:29
renkejun1942
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:不是这样
等 级:贵宾
威 望:33
帖 子:1645
专家分:5297
注 册:2016-12-1
收藏
得分:0 
回复 5楼 WL2311296974
数组的下标是从0开始,从1开始,那么0号元素就是一个垃圾值,如果你要从1开始也不是不可以,但是你在哪个locate的函数里又是从0开始。

你要防止输入0,正确的做法不是判断是不是0吗?如果是0,要求重新输入。

如果代码是你自己写的,我想你还是回去重新整理一下思路吧。
如果是你抄的,那还是对比一下,是不是抄错了。

locate_List 和locate是两个截然不同的函数,至少从你的代码中是这样,你调用locate_list只有2个参数,而locate要求三个参数。

[此贴子已经被作者于2017-3-31 16:43编辑过]


09:30 05/21 种下琵琶种子,能种活么?等待中……
21:50 05/27 没有发芽。
20:51 05/28 没有发芽。
23:03 05/29 没有发芽。
23:30 06/09 我有预感,要发芽了。
2017-03-31 16:42
marlow
Rank: 6Rank: 6
等 级:侠之大者
威 望:2
帖 子:125
专家分:419
注 册:2016-7-18
收藏
得分:0 
回复 3楼 WL2311296974
没搞懂你要做什么样的查询。另外,你这个就不是链表,仅仅是定义一个结构体里面的数组

一切都在学习、尝试、摸索中
2017-03-31 16:42
亲琪琪
Rank: 2
等 级:论坛游民
帖 子:55
专家分:38
注 册:2016-3-12
收藏
得分:5 
回复 楼主 WL2311296974
#include "stdio.h"
#define maxsize 10
typedef struct
{
    int data[maxsize];
    int last;                //向线性表输入数据的个数
}List;
void Scanf_List(List*p)
{
    int i;
    for(i=0;i<p->last;i++)
    {
        scanf("%d",&p->data[i]);
    }
}
void Printf_List(List*p)
{
    int i;
    for(i=0;i<p->last;i++)
    {
        printf(" \n%d ",p->data[i]);
    }
    printf("\n");
}
void Locate_List(int x,List*ptr)
{
    int i;
    for(i=0;i<ptr->last;i++)
    {
        if(ptr->data[i]==x)
           {
               x=ptr->data[i];
               printf("查找的元素为:\n");
               printf("%d\n",x);
        }
    }
       if(x==0)
       {
           printf("NO!!!!!!!!!!!!!!!!!!!!!!");
       }
      
    return;
}
main()
{
    List n;
    int x;
    printf("请输入线性表的长度:\n");
    while(1)
    {
        scanf("%d",&n.last);
        if(n.last<maxsize)
        {
            break;
        }
        printf("输入的线性表长度必须小于%d,请重新输入\n",maxsize);
        
    }
    printf("请输入线性表的数据:\n");
    Scanf_List(&n);
    printf("请输入要查找的元素:\n");
    scanf("%d",&x);
    Locate_List(x,&n);
    Printf_List(&n);
}
你看看这个对不
2017-03-31 19:01
WL2311296974
Rank: 1
来 自:安徽
等 级:新手上路
帖 子:37
专家分:7
注 册:2017-3-30
收藏
得分:0 
可以,谢谢!!!







可以查到线性表中的第N个数据吗?

求解答!!!

我的类不是你的泪
2017-04-05 08:58
亲琪琪
Rank: 2
等 级:论坛游民
帖 子:55
专家分:38
注 册:2016-3-12
收藏
得分:0 
回复 9楼 WL2311296974
可以的  我给你改的就是查找N个元素
2017-04-06 19:53
快速回复:线性表中查询数据
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017650 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved