标题:顺序表基本操作,不知道哪里错了?
只看楼主
九千七
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2019-12-15
 问题点数:0 回复次数:2 
顺序表基本操作,不知道哪里错了?
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 10
typedef  int ElemType ;
struct SqList {
    ElemType *elem;
    int length;
    int listsize;
};
void initSqlist(SqList &L)//初使化一张空表
{
    L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
    if(!L.elem)
        printf("OVERFLOW!");
    L.length=0;
    L.listsize=LIST_INIT_SIZE;
}
void PrintSqList(SqList L)//输出顺序表L
{
    int i;
    for(i=0;i<L.length;i++)
        printf(" %d ",L.elem[i]);
    printf("\n");
}
int LocateElem_Sq(SqList L, ElemType e)
{
    int i = 1;
    ElemType *p;
    p = L.elem;
    while( i <= L.length && *p++ != e)
        ++i;
    if (i <= L.length)
        return i;
    else return 0;

}
int ListInsert(SqList &L,int i ,ElemType e)//在顺序表L的第i 的元素前插入一个元素e
{
    ElemType *p,*q,*newbase;
    if(i < 1 || i > L.length + 1)
        printf("ERROR!");
    if (L.length >= L.listsize)
    {
        newbase = (ElemType *)realloc(L.elem,(L.listsize + LISTINCREMENT) * sizeof(ElemType));
        if(!newbase)
            printf("OVERFLOW!");
        L.elem = newbase;
        L.listsize += LISTINCREMENT;
    }
    q = &(L.elem[i-1]);
    for( p = &(L.elem[L.length-1]);p >= q;--p)
        *(p+1) = *p;
        *q = e;
        ++L.length;
    return 1;
}
void CreatSqlist(SqList &L)//创建一张顺序表L
{
    int i,n;
    initSqlist(L);
    printf("请输入你要输入的元素个数: ");
    scanf("%d",&n);
    L.length = n;
    printf("请输入%d个元素: ",n);
    for(i=0;i<n;i++)
        scanf("%d",&L.elem[i]);
    ListInsert(L,i,L.elem[i]);
}
int ListDelete(SqList &L,int i)//删除顺序表L的第i 个元素
{
    ElemType *p,*q;
    if (i<1 || i>L.length)
        printf("ERROR!");
    p = &(L.elem[i-1]);
    q = L.elem + L.length-1;
    for ( ++p; p <= q; ++p)
        *(p-1) = *p;
    --L.length;
    return 1;
}
int reverse(SqList &L)//逆置顺序表L
{
    int i,t;
    for( i = 0; i< L.length/2;i++)
    {
        t = L.elem[i];
        L.elem[i] = L.elem[L.length-i-1];
        L.elem[L.length-i-1] = t;
    }
    return 1;

}
void MergeList(SqList La,SqList Lb,SqList &Lc)
{ // 已知顺序线性表La和Lb的元素按值非递减排列,归并La和Lb得到新的顺序线性表Lc,Lc的元素也按值非递减排列
    ElemType *pa,*pb,*pc,*pa_last,*pb_last;
    pa = La.elem; pb = Lb.elem;
    Lc.listsize = Lc.length = La.length + Lb.length;
    pc=Lc.elem = (ElemType *)malloc(Lc.listsize * sizeof(ElemType));
    if(!Lc.elem)
        printf("OVERFLOW!");
    pa_last = La.elem + La.length - 1;
    pb_last = Lb.elem + Lb.length - 1;
    while(pa <= pa_last && pb <= pb_last)
    {
        if(*pa <= *pb)
            *pc++ = *pa++;
        else *pc++ = *pb++;
    }
    while(pa <= pa_last)
        *pc++ = *pa++;
    while(pb <= pb_last)
        *pc++ = *pb++;

}
void menu()//程序操作菜单
{
    printf("|==================================|\n");
    printf("|    ****---- 选择式菜单 ----****  |\n");
    printf("|==================================|\n");
    printf("|                                  |\n");
    printf("|           1-顺序表的创建算法     |\n");
    printf("|                                  |\n");
    printf("|           2-顺序表的遍历算法     |\n");
    printf("|                                  |\n");
    printf("|           3-顺序表的插入算法     |\n");
    printf("|                                  |\n");
    printf("|           4-顺序表的删除算法     |\n");
    printf("|                                  |\n");
    printf("|           5-顺序表的逆置算法     |\n");
    printf("|                                  |\n");
    printf("|           6-顺序表的归并算法     |\n");
    printf("|                                  |\n");
    printf("|           0:退出菜单             |\n");
    printf("|                                  |\n");
    printf("|==================================|\n");
}//menu
int main()
{
    SqList L,La,Lb,Lc;
    int i,m,k,flag;
    ElemType e;
    int exitflag=0;

    while(!exitflag)
    {
        menu();
        scanf("%d",&i);
        switch(i){
        case 0: exitflag=1; break;
        case 1:
            CreatSqlist(L);
            if(L.elem)
            {
                printf("\n\n创立的顺序表为:  ");
                PrintSqList(L);
            }
            else
                printf("It's failure for the list that we want to create!\n");
            break;
        case 2:system("cls");
            printf("\n\n当前顺序表为:");
            PrintSqList(L);
            printf("请输入你要查找的元素: ");
            scanf("%d",&e);
            m=LocateElem_Sq(L,e);
            if(!m)
                printf("此表中无你要找的元素%d!\n",e);
            else
                printf("你要找的元素%d是该表中的第%d个元素!\n",e,m);
            break;
        case 3:system("cls");
            printf("当前顺序表L为: ");
            PrintSqList(L);
            printf("\n\n请输入要插入的位置(1<=m<=%d)和元素e(用逗号隔开): \n",L.length+1);
            scanf("%d,%d",&m,&e);
            flag=ListInsert(L,m,e);
            if(flag==1){ printf("\n插入一个元素后,顺序表为:  ");
            PrintSqList(L);
            }
            break;
        case 4:system("cls");
            printf("当前顺序表L为: ");
            PrintSqList(L);
            printf("\n\n现在对选项1建立的表进行删除操作,请输入要删除的位置:  \n");
            scanf("%d",&k);
            flag= ListDelete(L,k);
            if(flag==1)
            {printf("\n删除一个元素后,顺序表为: ");
            PrintSqList(L);}
            break;
        case 5:system("cls");
            printf("当前顺序表L为: ");
            PrintSqList(L);
            reverse(L);
            printf("\n逆置操作后,顺序表为: ");
            PrintSqList(L);
            break;
        case 6:system("cls");
            CreatSqlist(La);
            CreatSqlist(Lb);
            printf("当前顺序表La为: ");
            PrintSqList(La);
            printf("当前顺序表Lb为: ");
            PrintSqList(Lb);
            initSqlist(Lc);
            MergeList(La,Lb,Lc);
            printf("归并后顺序表Lc为: ");
            PrintSqList(Lc);
            break;
        default:printf("警告!你给的选项号非法,请重新输入\n");
        }
    }
    return 0;
}
搜索更多相关主题的帖子: length 顺序表 printf elem int 
2020-03-22 14:12
lin5161678
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:45
帖 子:1136
专家分:3729
注 册:2011-12-3
得分:0 
遇到什么错误说清楚

https://zh.
2020-03-23 01:43
九千七
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2019-12-15
得分:0 
回复 2楼 lin5161678
抱歉,忘了说了。运行时出现
错误,多了个任意数
2020-03-23 08:32



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-500508-1-1.html




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

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