| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 517 人关注过本帖
标题:哪位高手帮我看看这个程序错在哪?
只看楼主 加入收藏
一只小妖
Rank: 2
等 级:论坛游民
帖 子:7
专家分:13
注 册:2012-2-27
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:1 
哪位高手帮我看看这个程序错在哪?
我需要把链表程序的将按位置插入和删除改为按给定元素进行查找后插入和删除,我想要的是查找元素后向后插入一个元素,或者按元素查找后删除元素,但是不能实现,哪错了?红色为要改的地方
//实验一参考程序如下: //
#include <iostream>
using namespace std;   
#define max 100               
typedef struct SeqList
{
 int elem[max];
 int length;
}sqlist;

sqlist L;   //定义顺序表L


sqlist CreatList_Sq(sqlist L)
 {//创建顺序表
 int i;
 printf("请输入线性表的长度n:\n");
 scanf("%d",&L.length);
 printf("\n");
 printf("please input %d numbers:\n",L.length);
 for (i=1;i<=L.length;i++)
    scanf("%d",&L.elem[i]);
  printf("\n");
return(L);
}

void display(sqlist L)
{//输出显示函数
 int i;
 printf("线性表为:\n");
   for (i=1;i<=L.length;i++)
      printf("%d ",L.elem[i]);
   printf("\n");
}

sqlist ListInsert_Sq(sqlist L,int i,int e)
{//在顺序表L的第i个位置插入新元素e
  int j;
  for(i=0;i<=L.length;i++)
     if(L.length==i)
     break;
  if (i<1||i>L.length)
     printf("error");
    if (L.length>=max)
        printf("overflow");
    else
     {
       for (j=L.length;j>=i;j--)
         L.elem[j-1]=L.elem[j];
       L.elem[i]=e;
       L.length++;
       printf("success\n");
  };
   return(L);
}

sqlist ListDelete_Sq(sqlist l, int i)
{//删除线性表L中第i个元素
 int j;
 for(i=0;i<=L.length;i++)
     if(L.length==i)
  break;
 if (i<1 ||i>L.length)
   printf("I is error\n");
 else
   {
    for (j=i+1;j<=L.length;++j)
      L.elem[j-1]=L.elem[j];
    L.length--;
    printf("success\n");
   };
return(L);
}

void main()
{
  int num,delnum,f=1;
  int p;
  int no;
  /*clrscr();*/
  while(f)
  { printf("请输入0或1、2、3来选择操作:\n0退出\n1顺序表创建\n2顺序表元素插入\n3顺序表元素删除\n");
    scanf("%d",&no);
    switch(no)
        {
    case 1:    L=CreatList_Sq(L);
                   display(L);
                   break;
        case 2:    printf("please point to the position you want to:\n");
                   scanf("%d",&p);
                   printf("Input the inserted number:\n");
                   scanf("%d",&num);
                   printf("\n");
                  L=ListInsert_Sq(L,p,num);
                   display(L);
                   break;
        case 3:    printf("Input the deleted the position of the number:");
                   scanf("%d",&delnum);
                   printf("\n");
                   L=ListDelete_Sq(L,delnum);
                   display(L);
                   break;
        case 0:    break;
        }
printf("do you want to continue ?\nno.1:continue\nno.0:exit\n");
scanf("%d",&f);
  }
}
搜索更多相关主题的帖子: 线性表 include 100 元素 链表程序 
2012-04-02 22:24
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6814
专家分:42393
注 册:2010-12-16
收藏
得分:20 
有几个逻辑错误
1 如果输入的字数大于100的时候 ,程序就有非法内存范围了
2
sqlist ListInsert_Sq(sqlist L,int i,int e)
{//在顺序表L的第i个位置插入新元素e
  int j;
  for(i=0;i<=L.length;i++)
     if(L.length==i)
     break;

……
   return(L);
}

红色的部分你到底想干什么?这个没有必要的

比如要插入的位置是i = 10 的时候,
直接 if (i < 0 || i >= max)就行了,没有必要红色的部分。。 你细细想想这个逻辑。。。

3 数组下标从0开始好点 , 你的下标从1 开始了,浪费了一个空间 (当然你也可以这么用 ,只是不太好吧)

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2012-04-08 20:29
快速回复:哪位高手帮我看看这个程序错在哪?
数据加载中...
 
   



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

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