文档详情

数据结构实验二试验报告 线性表的基本操作.doc

发布:2018-09-30约9.41千字共17页下载文档
文本预览下载声明
PAGE PAGE 13 数据结构试验报告 实验二 线性表的基本操作 实验内容:线性表两种存储结构的基本运算       专业班级:网络工程 1002班    组 长:王星 (2010100230) 组 员:郭坤铭(2010100243) 张磊 (2010100244) 2012年 实验项目目的和要求 掌握线性表的特点。 掌握线性表的顺序存储结构和链式存储结构的基本运算。 尽可能考虑算法的健壮性。 实验报告中要写出测试数据、错误分析以及收获。 实验内容 1.用结构体类型描述线性表的两种存储结构 2.完成课堂上所讲的两种存储结构的基本运算 3.要求用二级菜单实现 ***************************** * 1顺序表 * * 2链 表 * * 0退 出 * ***************************** 请输入的选择:(0-2): 线性表的链式存储 ############################## # 1前插建立链表 # # 2后插建立链表 # # 3访问第i个元素 # # 4插入 # # 5删除 # # 6求线性表的表长 # # 0退出 # ############################## 请输入选择(0-6): 实验代码 #include stdio.h #include stdlib.h //宏定义// #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 #define LIST_INIT_SIZE 50 #define LISTINCREMENT 10 //结构体// typedef int Status; typedef int ElemType; typedef struct {ElemType *elem; int length; int listsize; }SqList; typedef struct link { int data; struct link *next; }link,*linklist; //顺序表中的函数// Status InitList_Sq(SqList *L) { int i,a; L-elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType)); //分配空间 if(!L-elem) exit(OVERFLOW);//若存储分配失败,返回-2 L-length=0; //空表,长度为0 L-listsize=LIST_INIT_SIZE; //初始存储容量 printf(请输入结点数:); scanf(%d, a); printf(请输入数据:\n); for(i=0;ia;i++) { scanf(%d,L-elem[i]); (L-length)++; } return OK; } void printlist_sq(SqList L) { int i; printf(输入的元素为:); for(i=0;iL.length;i++) printf(%d,L.elem[i]); printf(\n); } Status ListInsert_Sq(SqList *L,int i,ElemType e) { int *q=(L-elem[i-1]); ElemType *newbase,*p; if (i1||iL-length+1) return ERROR; //检查i值是否合理 //将线性表第i个元素之后的所有元素向后移动 if(L-length=L-listsize) { newbase=(ElemType*)r
显示全部
相似文档