| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 546 人关注过本帖
标题:为什么这个程序可以通过编译,却不能执行呢, 能给我点指点吗?
只看楼主 加入收藏
zc1992312
Rank: 2
等 级:论坛游民
帖 子:43
专家分:12
注 册:2013-3-20
结帖率:57.14%
收藏
已结贴  问题点数:20 回复次数:5 
为什么这个程序可以通过编译,却不能执行呢, 能给我点指点吗?
#include <stdio.h>
#include <stdlib.h>

#define FALSE -1
#define TURE  0
#define Max_size 30
#define LIST_INIT_SIZE 10
typedef int ElemType;

typedef struct
{
     ElemType pBase[Max_size];
    int nLength;
    int nSize;         
}SqList;

int InitList(SqList *pList)    /*线性表初始化*/
{
     pList = (SqList *) malloc(sizeof (SqList));
      pList -> nLength = 0;
     pList -> nSize = Max_size;
     return TURE;      
}

void DestroyList(SqList *pList)        /*销毁线性表*/
{
      free(pList);      
}

int ListLength(SqList *pList)        /*线性表的长度*/
{
      return (pList -> nLength);     
}

int ListEmpty(SqList *pList)        /*判断线性表是否为空*/
{
      if (pList -> nLength == 0)
          return FALSE;
     else
         return TURE;      
}

int ShowList(SqList *pList)        /*输出线性表*/
{
      int pos, n = 0;
      if (ListEmpty (pList))
          return FALSE;
     for (pos = 0; pos < pList -> nLength; pos++)
     {
        printf ("%d  ", pList -> pBase[pos]);
        n++;
        if (n % 5 == 0)
            printf ("\n");
     }
     
     printf ("\n");     
}

int GetElem(SqList *pList, int pos, int e)        /*求线性表中某个元素的值*/
{
      if (pos < 1 || pos > pList -> nLength)
          return FALSE;
     else
         e = pList -> pBase [pos - 1];
         
     return TURE;
         
}

int LocateElem(SqList *pList, int e)        /*查找元素e*/
{
      int pos;
      for (pos = 0; pos < pList -> nLength && pList -> pBase[pos] != e;)
           pos++;
          if (pos >= pList -> nLength)
              return FALSE;
          else
             return (pos + 1);      
}

int ListDelete(SqList *pList, int pos, int e)            /*删除数据元素*/
{
      int j;
     if (pos < 1 || pos > pList -> nLength)
         return FALSE;
     pos--;
     e = pList -> pBase[pos];
     
     for (j = pos; j < pList -> nLength - 1; j++)
     {
           pList -> pBase[j] = pList -> pBase[j + 1];      
     }
     
     pList -> nLength--;
     
     return TURE;     
}

int ListInsert(SqList *pList, int pos, int e)            /*插入数据元素*/
{
      int j;
     if (pos < 1 || pos > pList -> nLength)
         return FALSE;
     pos--;
     
     for (j = pList -> nLength; j > pos; j--)
     {
           pList -> pBase[j] = pList -> pBase[j - 1];      
     }
     
     pList -> pBase[pos]= e;
     pList -> nLength++;
     
     return TURE;
}

int ReadList(SqList *pList,int n)        /*从文件读入n(n > 10)个整数,填入顺序表*/
{
     int i;
     FILE * fp = fopen("list.txt","r");
     
     if (fp == NULL)
     {
           printf ("错误!无法读入文件!");
         exit(0);         
     }
     
     for(i = 0; i < n; i++)
        fscanf(fp,"%d ",&pList -> pBase[i]);

     pList -> nLength = n;
     fclose(fp);
     return TURE;
}

int SaveList(SqList *pList)            /*将线性表保存到文件中*/
{
     FILE *fp;
     int i;
     fp=fopen("list.txt","r+");
     
     for(i = 0;i < pList -> nLength; i++)
       fprintf(fp,"%d ", pList -> pBase[i]);
      
    fclose(fp);
    return TURE;
}

int main()
{
       int n, e1, e2;
       SqList *pList;
      printf ("(1).初始化线性表\n");
      InitList (pList);
      
      printf ("请输入n的值:  ");
      scanf ("%d", &n);
      ReadList(pList, n);
      printf ("\n");
      
      printf ("(2).输出%d个数至屏幕\n", n);
      ShowList(pList);
      
      printf ("(3).查找元素的位序\n");
      printf ("元素7的位序为%d\n", LocateElem(pList, 7));
      printf ("\n");
      
      printf ("(4).在顺序表头、中、尾各插一个元素\n");
      ListInsert(pList, 1, 1);
      ListInsert(pList, 9, 8);
      ListInsert(pList, 17, 15);
      ShowList(pList);
      printf ("\n");
      
      printf ("(5).输入修改后的表至屏幕,并保存至文件\n");
      ShowList(pList);
      SaveList(pList);
      printf ("\n");
      
      /*printf ("(6).删除顺序表的任意两个元素\n");
      ListDelete(pList, 5, e1);
      ListDelete(pList, 11, e2);*/
      
      DestroyList(pList);
      
      system("pause");
            
}
搜索更多相关主题的帖子: include return 线性表 
2013-09-21 20:15
蔡梓锋
Rank: 4
等 级:业余侠客
帖 子:106
专家分:202
注 册:2013-4-20
收藏
得分:20 
#include <stdio.h>
#include <stdlib.h>

#define FALSE -1
#define TURE  0
#define Max_size 30
#define LIST_INIT_SIZE 10
typedef int ElemType;

typedef struct
{
     ElemType pBase[Max_size];
    int nLength;
    int nSize;         
}SqList;

int InitList(SqList *pList)    /*线性表初始化*/
{
     pList = (SqList *) malloc(sizeof (SqList));
      pList -> nLength = 0;
     pList -> nSize = Max_size;
     return TURE;      
}

void DestroyList(SqList *pList)        /*销毁线性表*/
{
      free(pList);      
}

int ListLength(SqList *pList)        /*线性表的长度*/
{
      return (pList -> nLength);     
}

int ListEmpty(SqList *pList)        /*判断线性表是否为空*/
{
      if (pList -> nLength == 0)
          return FALSE;
     else
         return TURE;      
}

int ShowList(SqList *pList)        /*输出线性表*/
{
      int pos, n = 0;
      if (ListEmpty (pList))
          return FALSE;
     for (pos = 0; pos < pList -> nLength; pos++)
     {
        printf ("%d  ", pList -> pBase[pos]);
        n++;
        if (n % 5 == 0)
            printf ("\n");
     }
     
     printf ("\n");     
}

int GetElem(SqList *pList, int pos, int e)        /*求线性表中某个元素的值*/
{
      if (pos < 1 || pos > pList -> nLength)
          return FALSE;
     else
         e = pList -> pBase [pos - 1];
         
     return TURE;
         
}

int LocateElem(SqList *pList, int e)        /*查找元素e*/
{
      int pos;
      for (pos = 0; pos < pList -> nLength && pList -> pBase[pos] != e;)
           pos++;
          if (pos >= pList -> nLength)
              return FALSE;
          else
             return (pos + 1);      
}

int ListDelete(SqList *pList, int pos, int e)            /*删除数据元素*/
{
      int j;
     if (pos < 1 || pos > pList -> nLength)
         return FALSE;
     pos--;
     e = pList -> pBase[pos];
     
     for (j = pos; j < pList -> nLength - 1; j++)
     {
           pList -> pBase[j] = pList -> pBase[j + 1];      
     }
     
     pList -> nLength--;
     
     return TURE;     
}

int ListInsert(SqList *pList, int pos, int e)            /*插入数据元素*/
{
      int j;
     if (pos < 1 || pos > pList -> nLength)
         return FALSE;
     pos--;
     
     for (j = pList -> nLength; j > pos; j--)
     {
           pList -> pBase[j] = pList -> pBase[j - 1];      
     }
     
     pList -> pBase[pos]= e;
     pList -> nLength++;
     
     return TURE;
}

int ReadList(SqList *pList,int n)        /*从文件读入n(n > 10)个整数,填入顺序表*/
{
     int i;
     FILE * fp = fopen("list.txt","r");
     
     if (fp == NULL)
     {
           printf ("错误!无法读入文件!");
         exit(0);         
     }
     
     for(i = 0; i < n; i++)
        fscanf(fp,"%d ",&pList -> pBase[i]);

     pList -> nLength = n;
     fclose(fp);
     return TURE;
}

int SaveList(SqList *pList)            /*将线性表保存到文件中*/
{
     FILE *fp;
     int i;
     fp=fopen("list.txt","r+");
     
     for(i = 0;i < pList -> nLength; i++)
       fprintf(fp,"%d ", pList -> pBase[i]);
      
    fclose(fp);
    return TURE;
}

int main()
{
       int n;
       SqList pList;
      printf ("(1).初始化线性表\n");
      InitList (&pList);
      
      printf ("请输入n的值:  ");
      scanf ("%d", &n);
      ReadList(&pList, n);
      printf ("\n");
      
      printf ("(2).输出%d个数至屏幕\n", n);
      ShowList(&pList);
      
      printf ("(3).查找元素的位序\n");
      printf ("元素7的位序为%d\n", LocateElem(&pList, 7));
      printf ("\n");
      
      printf ("(4).在顺序表头、中、尾各插一个元素\n");
      ListInsert(&pList, 1, 1);
      ListInsert(&pList, 9, 8);
      ListInsert(&pList, 17, 15);
      ShowList(&pList);
      printf ("\n");
      
      printf ("(5).输入修改后的表至屏幕,并保存至文件\n");
      ShowList(&pList);
      SaveList(&pList);
      printf ("\n");
      
      /*printf ("(6).删除顺序表的任意两个元素\n");
      ListDelete(pList, 5, e1);
      ListDelete(pList, 11, e2);*/
      
      DestroyList(&pList);
      
      system("pause");
            
}

加油加油!
2013-09-21 20:34
蔡梓锋
Rank: 4
等 级:业余侠客
帖 子:106
专家分:202
注 册:2013-4-20
收藏
得分:0 
就是不知道你return 0和-1有什么用,你下面有不用返回值判断,给你小改了一下,我用了dev和C与C++程序设计学习与实验系统 2013都可以用,就不知道你用什么软件

加油加油!
2013-09-21 20:36
zc1992312
Rank: 2
等 级:论坛游民
帖 子:43
专家分:12
注 册:2013-3-20
收藏
得分:0 
回复 3楼 蔡梓锋
嘿嘿  谢谢  我会好好看看的、、
   那为什么我要实现删除操作时(下面被注销的部分) 就有错误了呢 ?
   RETUEN 0 , -1
    那一般应该怎么写啊 、
2013-09-21 20:38
zc1992312
Rank: 2
等 级:论坛游民
帖 子:43
专家分:12
注 册:2013-3-20
收藏
得分:0 
回复 2楼 蔡梓锋
明白了 、、
  非常感谢、、
    我都调试了 好长时间了。
  还有一个问题   为什么 主程序那pList 前面要加个地址符呢 ?
2013-09-21 20:49
蔡梓锋
Rank: 4
等 级:业余侠客
帖 子:106
专家分:202
注 册:2013-4-20
收藏
得分:0 
你是用指针赋值给指针,貌似会变成二级指针,貌似也是可以的,可能编译器问题(这个我也不太懂)反正我看过的书,一般都是用地址赋值给指针,所以你一般要传递的值就定义成SqList pList;不要定义成指针
SqList *pList;,然后传递时就加个&传递,(&pList);下面有错的你给pList加个&看行不行。

加油加油!
2013-09-21 21:23
快速回复:为什么这个程序可以通过编译,却不能执行呢, 能给我点指点吗?
数据加载中...
 
   



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

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