1、输入一组学生信息,建立一个顺序表。
2、遍历该顺序表,输出所有学生信息。
3、查找某特定的学生,查找成功返回 1,否则返回 0。
4、编写在顺序表中插入一个元素和删除一个元素。
5、编写一个主函数,调试上述算法。
#include "stdio.h"
#include "malloc.h"
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
#define LIST_INIT_SIZE 100 /*初始分配的顺序表长度*/
#define LISTINCREMENT 10 /*溢出时,顺序表长度的增量*/
typedef struct ElemType /*定义表元素的类型*/
{
int num;
char* name;
}ElemType;
typedef struct Sqlist{
ElemType *elem; /*存储空间的基地址*/
int length; /*顺序表的当前长度*/
int listsize; /*当前分配的存储空间*/
}SqList;
Status InitList_Sq(SqList &L) { // 算法2.3
// 构造一个空的线性表L。
L.elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if (!L.elem) return ERROR; // 存储分配失败
L.length = 0; // 空表长度为0
L.listsize = LIST_INIT_SIZE; // 初始存储容量
return OK;
} // InitList_Sq
void main()
{
SqList l;
if(InitList_Sq(l))
printf("初始化成功。");
else
printf("存储分配失败");
}
2、遍历该顺序表,输出所有学生信息。
3、查找某特定的学生,查找成功返回 1,否则返回 0。
4、编写在顺序表中插入一个元素和删除一个元素。
5、编写一个主函数,调试上述算法。
#include "stdio.h"
#include "malloc.h"
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
#define LIST_INIT_SIZE 100 /*初始分配的顺序表长度*/
#define LISTINCREMENT 10 /*溢出时,顺序表长度的增量*/
typedef struct ElemType /*定义表元素的类型*/
{
int num;
char* name;
}ElemType;
typedef struct Sqlist{
ElemType *elem; /*存储空间的基地址*/
int length; /*顺序表的当前长度*/
int listsize; /*当前分配的存储空间*/
}SqList;
Status InitList_Sq(SqList &L) { // 算法2.3
// 构造一个空的线性表L。
L.elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if (!L.elem) return ERROR; // 存储分配失败
L.length = 0; // 空表长度为0
L.listsize = LIST_INIT_SIZE; // 初始存储容量
return OK;
} // InitList_Sq
void main()
{
SqList l;
if(InitList_Sq(l))
printf("初始化成功。");
else
printf("存储分配失败");
}