| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 640 人关注过本帖
标题:求大神看看我的代码
只看楼主 加入收藏
c764193441
Rank: 2
等 级:论坛游民
帖 子:18
专家分:16
注 册:2012-9-23
结帖率:60%
收藏
已结贴  问题点数:20 回复次数:2 
求大神看看我的代码
程序代码:
本人刚开始学数据结构,编了一个基本的代码,可怎么也不成功,找不出错在哪里?求指导




#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ElemType int

typedef struct
{
    int *elem;
    int length;
    int listsize;
}SqList;

int InitList_Sq(SqList *L)
{
L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));// 算法2.3,构造一个空的线性表L,该线性表预定义大小为LIST_INIT_SIZE
if(!L->elem)exit(ERROR);
L->length=0;
L->listsize=LIST_INIT_SIZE;
return OK;
}

int Load_Sq(SqList *L)
{
// 输出顺序表中的所有元素
    int i;
    if(!L->elem) printf("The List is empty!"); 
    else
    {
        printf("The List is: ");
        for(i=0;i<L->length;i++) printf("%d ",L->elem); 
    }
    printf("\n");
    return OK;
}

int ListInsert_Sq(SqList *L,int i,int e)
{int *newbase,*p,*q;
// 算法2.4,在顺序线性表L中第i个位置之前插入新的元素e
if(i<1||i>L->length+1)return ERROR;// i的合法值为1≤i≤L.length +1
if(L->length>=L->listsize)
{newbase=(ElemType *)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(ElemType));}
if(!newbase)exit(ERROR);
L->elem=newbase;
L->listsize+=LISTINCREMENT;
q=&(L->elem[i-1]);
for(p=&(L->elem[L->length-1]);p>=q;--p)
*(p+1)=*p;
*p=e;
++(L->length);
return OK;
}

int ListDelete_Sq(SqList *L,int i, int e)
{int *p,*q;
// 算法2.5,在顺序线性表L中删除第i个位置的元素,并用e返回其值
if(i<1||i>L->length)return ERROR;// i的合法值为1≤i≤L.length
p=&(L->elem[i-1]);
e=*p;
q=L->elem+L->length-1;
for(++p;p<=q;++p)*(p-1)=*p;
--L->length;
return e;
}

int main()
{
    SqList T;
    int a, i;
    ElemType e, x;
    if(InitList_Sq(&T) )   // 判断顺序表是否创建成功
    {
        printf("A Sequence List Has Created.\n");
    }
    while(1)
    {
        printf("1:Insert element\n2:Delete element\n3:Load all elements\n0:Exit\nPlease choose:\n");
        scanf("%d",&a);
        switch(a)
        {
            case 1: scanf("%d%d",&i,&x);
                    if( ListInsert_Sq(&T,i,x)) printf("Insert Error!\n"); // 判断i值是否合法
                    else printf("The Element %d is Successfully Inserted!\n", x);
                    break;
            case 2: scanf("%d",&i);
                    if(ListDelete_Sq(&T,i,e)) printf("Delete Error!\n"); // 判断i值是否合法
                    else printf("The Element %d is Successfully Deleted!\n", e);
                    break;
            case 3: Load_Sq(&T);
                    break;
            case 0: return 1;
        }
    }
}
搜索更多相关主题的帖子: 成功 
2012-09-23 15:24
c764193441
Rank: 2
等 级:论坛游民
帖 子:18
专家分:16
注 册:2012-9-23
收藏
得分:0 
这是调试例子,可输入1 2 时就发生错误了
Input
测试样例格式说明:
根据菜单操作:
1、输入1,表示要实现插入操作,紧跟着要输入插入的位置和元素,用空格分开
2、输入2,表示要实现删除操作,紧跟着要输入删除的位置
3、输入3,表示要输出顺序表的所有元素
4、输入0,表示程序结束

Sample Input
1
1 2
1
1 3
2
1
3
0
Sample Output
A Sequence List Has Created.
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The Element 2 is Successfully Inserted!
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The Element 3 is Successfully Inserted!
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The Element 3 is Successfully Deleted!
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The List is: 2
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
2012-09-23 15:27
netlin
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:24
帖 子:544
专家分:4308
注 册:2012-4-9
收藏
得分:20 
楼主,看了你的代码,做了一点点修改(你代码中的“}”位置可能有问题),你试试:
int ListInsert_Sq(SqList *L,int i,int e)
{int *newbase,*p,*q;
// 算法2.4,在顺序线性表L中第i个位置之前插入新的元素e
if(i<1||i>L->length+1)return ERROR;// i的合法值为1≤i≤L.length +1
if(L->length>=L->listsize)
{newbase=(ElemType *)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(ElemType));
if(!newbase)exit(ERROR);
L->elem=newbase;
L->listsize+=LISTINCREMENT;}
q=&(L->elem[i-1]);
for(p=&(L->elem[L->length-1]);p>=q;--p)
*(p+1)=*p;
*p=e;
++(L->length);
return OK;
}


做自己喜欢的事!
2012-09-26 13:12
快速回复:求大神看看我的代码
数据加载中...
 
   



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

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