「已注销」 2024-06-09 00:28 采纳率: 0%
浏览 54
已结题

数据结构实验系列(1):顺序表

#include "stdio.h"
#include "stdlib.h"
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2

typedef int status;
typedef int ElemType; //数据元素类型定义

#define LIST_INIT_SIZE 100
#define LISTINCREMENT  10
typedef int ElemType;
typedef struct{  //顺序表(顺序结构)的定义
          ElemType * elem;
          int length;
          int listsize;
         }SqList;
status  SaveList(SqList L,char FileName[])
// 如果线性表L存在,将线性表L的的元素写到FileName文件中,返回OK,否则返回INFEASIBLE。
{
    // 请在这里补充代码,完成本关任务
    /********** Begin *********/
    /********** End **********/
}
status  LoadList(SqList &L,char FileName[])
// 如果线性表L不存在,将FileName文件中的数据读入到线性表L中,返回OK,否则返回INFEASIBLE。
{
    // 请在这里补充代码,完成本关任务
    /********** Begin *********/
    /********** End **********/
}
status AddList(LISTS *Lists, char ListName[]) 
{
    // 请在这里补充代码,完成本关任务
    /********** Begin *********/
    /********** End **********/
}
status RemoveList(LISTS *Lists, char ListName[]) 
//Lists中删除一个名称为Listname的线性表
{
    // 请在这里补充代码,完成本关任务
    /********** Begin *********/
    /********** End **********/
}
int LocateList(LISTS Lists,char ListName[])
// 在Lists中查找一个名称为ListName的线性表,成功返回逻辑序号,否则返回0
{
    // 请在这里补充代码,完成本关任务
    /********** Begin *********/


    /********** End **********/
}

第13关:线性表读写文件
本关任务:编写两个函数实现将顺序表的数据元素的读写文件。
函数原型:status SaveList(SqList L,char FileName[])
函数原型:status LoadList(SqList &L,char FileName[])
功能说明:SaveList:如果线性表L不存在,返回INFEASIBLE;否则将线性表L的全部元素写入到文件名为FileName的文件中,返回OK。LoadList:如果线性表L存在,表示L中已经有数据,读入数据会覆盖原数据造成数据丢失,返回INFEASIBLE;否则将文件名为FileName的数据读入到线性表L中,返回OK。本实验不考虑用追加的方式读入文件数据追加到现有线性表中。
第14关:多线性表管理:增加一个新线性表
本关任务:编写一个函数实现增加一个新的线性表。
函数原型:status AddList(LISTS &Lists,char ListName[])
功能说明:Lists是一个以顺序表形式管理的线性表的集合,在集合中增加一个新的空线性表。增加成功返回OK,否则返回ERROR。
第15关:多线性表管理:移除一个线性表
本关任务:编写一个函数实现移除一个指定的线性表。
函数原型:status RemoveList(LISTS &Lists,char ListName[])
功能说明:Lists是一个以顺序表形式管理的线性表的集合,在集合中查找名称为ListName的线性表,有则删除,返回OK,无则返回ERROR。
第16关:多线性表管理:查找线性表
本关任务:编写一个函数实现查找一个指定的线性表。
函数原型:int LocateList(LISTS Lists,char ListName[])
功能说明:Lists是一个以顺序表形式管理的线性表的集合,在集合中查找名称为ListName的线性表,有则返回线性表的逻辑序号,无则返回0。

  • 写回答

23条回答 默认 最新

  • 「已注销」 2024-06-09 00:52
    关注

    第13关的main.cpp

    #include "def.h"
    #include "string.h"
    
    #include "stu.h"
    int main()
    {
    SqList L;
    FILE *fp;
    //char FileName[30];
    int f,i=0,j,e;
    //strcpy(FileName,"src/step13/list.dat");
    scanf("%d",&f);
    if (!f)
    {
            L.elem=NULL;
               j=SaveList(L,"src/step13/list.dat");
               if (j!=INFEASIBLE) printf("不能对不存在的线性表进行写文件操作!");
            else 
          {
                 L.elem=(ElemType *) malloc(sizeof(ElemType));
                    j=LoadList(L,"src/step13/list.dat");
                    if (j!=INFEASIBLE) printf("不能对已存在的线性表进行读文件操作!");
                else printf("INFEASIBLE"); 
                 free(L.elem);
          }
    }
    else
         {
            L.elem=(ElemType *) malloc(sizeof(ElemType)*LIST_INIT_SIZE);
            L.length=0;
            L.listsize= LIST_INIT_SIZE;
            scanf("%d",&e);
            while (e)
            {
                L.elem[i++]=e;
                scanf("%d",&e);
            }
            L.length=i;
            j=SaveList(L,"src/step13/list.dat");
               free(L.elem); 
            L.elem=NULL;
            j=LoadList(L,"src/step13/list.dat");
            printf("%d\n",L.length);
            for(i=0;i<L.length;i++) 
                printf("%d ",L.elem[i]);
         }
    return 1;
    }
    /*
    测试说明
    平台会自动读取输入数据,对你编写的代码进行测试,并输出结果。
    
    测试输入:0;此时表示对不存在的线性表进行写文件操作,或表示读入文件数据覆盖已存在的线性表导致数据丢失。
    预期输出:
    INFEASIBLE
    
    测试输入:1 11 12 13 14 15 0,第一个数1表示对一个已存在的线性表进行操作,后续是一个以0结束的数据元素序列。
    预期输出:
    5
    11 12 13 14 15
    */
    

    第14关的main.cpp

    #include "def.h"
    #include "../step01/stu.h"
    #include "../step10/stu.h"
    #include "../step12/stu.h"
    #include "stu.h"
    int main() {
        LISTS Lists;
       int n,e;
       char name[30];
       Lists.length=0;
        scanf("%d", &n);
        while(n--)
       {
            scanf("%s",name);
               AddList(Lists,name);
          scanf("%d",&e);
          while (e)
          {
                  ListInsert(Lists.elem[Lists.length-1].L,Lists.elem[Lists.length-1].L.length+1,e);
                  scanf("%d",&e);
          }
       }
       for(n=0;n<Lists.length;n++)
       {
               printf("%s ",Lists.elem[n].name);
               ListTraverse(Lists.elem[n].L);
            putchar('\n');
       }
    return 1;
    }
    /*
    测试说明:测试输入数据说明,第一行数n是要增加的线性表个数,调用你编写的函数增加n个空线性表到集合中;接着每行是一个线性表名称和以0结束的线性表数据元素,后台自动读入这些数据,依次插入到对应线性表中。
    
              湖北 5 10 20 30 50 0;
              湖南 3 12 21 13 60 0;
    预期输出:湖北 5 10 20 30 50;
           湖南 3 12 21 13 60;
    */
    

    第15关的main.cpp

    #include "def.h"
    #include "../step01/stu.h"
    #include "../step02/stu.h"
    #include "../step10/stu.h"
    #include "../step12/stu.h"
    #include "../step14/stu.h"
    #include "stu.h"
    int main() {
        LISTS Lists;
       int n,e;
       char name[30];
       Lists.length=0;
        scanf("%d", &n);
        while(n--)
       {
            scanf("%s",name);
               AddList(Lists,name);
          scanf("%d",&e);
          while (e)
          {
                  ListInsert(Lists.elem[Lists.length-1].L,Lists.elem[Lists.length-1].L.length+1,e);
                  scanf("%d",&e);
          }
       }
       scanf("%s",name);
       if (RemoveList(Lists,name)==OK)
           for(n=0;n<Lists.length;n++)
               {
                   printf("%s ",Lists.elem[n].name);
                   ListTraverse(Lists.elem[n].L);
                putchar('\n');
               }
       else printf("删除失败");
    return 1;
    }
    /*
    测试说明:测试输入数据说明,第一行数n是要增加的线性表个数,调用你编写的函数增加n个空线性表到集合中;接着每行是一个线性表名称和以0结束的线性表数据元素,后台自动读入这些数据,依次插入到对应线性表中。最后一行是要删除的线性表名称,删除成功后显示剩余的线性表。
    
    测试输入:3
              湖北 5 10 20 30 50 0;
              北京 30 50 60 70 80 0;
              湖南 3 12 21 13 60 0;
              北京
    预期输出:湖北 5 10 20 30 50;
           湖南 3 12 21 13 60;
    
    测试输入:2
              湖北 5 10 20 30 50 0;
              湖南 3 12 21 13 60 0;
              北京
    预期输出:删除失败
    */
    

    第16关的main.cpp

    #include "def.h"
    #include "../step01/stu.h"
    #include "../step10/stu.h"
    #include "../step12/stu.h"
    #include "../step14/stu.h"
    #include "stu.h"
    int main() {
        LISTS Lists;
       int n,e;
       char name[30];
       Lists.length=0;
        scanf("%d", &n);
        while(n--)
       {
            scanf("%s",name);
               AddList(Lists,name);
          scanf("%d",&e);
          while (e)
          {
                  ListInsert(Lists.elem[Lists.length-1].L,Lists.elem[Lists.length-1].L.length+1,e);
                  scanf("%d",&e);
          }
       }
       scanf("%s",name);
       if (n=LocateList(Lists,name))
               {
                   printf("%s ",Lists.elem[n-1].name);
                   ListTraverse(Lists.elem[n-1].L);
             putchar('\n');
               }
       else printf("查找失败");
    return 1;
    }
    
    /*
    测试说明 :测试输入数据说明,第一行数n是要增加的线性表个数,调用你编写的函数增加n个空线性表到集合中;接着每行是一个线性表名称和以0结束的线性表数据元素,后台自动读入这些数据,依次插入到对应线性表中。最后一行是要查的线性表名称,查找成功后显示该线性表。
    
    测试输入:3
              湖北 5 10 20 30 50 0;
              北京 30 50 60 70 80 0;
              湖南 3 12 21 13 60 0;
              湖北
    预期输出:湖北 5 10 20 30 50;
    
    测试输入:2
              湖北 5 10 20 30 50 0;
              湖南 3 12 21 13 60 0;
              北京
    预期输出:查找失败
    */
    
    评论

报告相同问题?

问题事件

  • 系统已结题 6月17日
  • 创建了问题 6月9日