第三周项目一(3)
分类:
文章
•
2024-04-30 17:05:16
-
/*
-
烟台大学计算机学院
-
-
文件名称:xmyc.cpp
-
-
作者:李恩
-
-
完成日期:2017年9月20日
-
-
问题描述:顺序表建立,插入,销毁
-
-
输入描述:无
-
-
输出描述:顺序表插入后
-
-
*/
-
-
-
-
#include <stdio.h>
-
#include <malloc.h>
-
-
#define MaxSize 50//存储空间大小宏定义
-
-
-
typedef int ElemType; //定义ElemType为int
-
typedef struct
- {
- ElemType data[MaxSize]; //利用了前面MaxSize和ElemType的定义
- int length;
- } SqList;
-
void InitList(SqList *&L);
-
bool ListInsert(SqList *&L,int i,ElemType e);
-
void DispList(SqList *L);
-
bool ListEmpty(SqList *L);
-
int main()
- {
- SqList *sq;
- InitList(sq);
- ListInsert(sq, 1, 5);
- ListInsert(sq, 2, 3);
- ListInsert(sq, 1, 4);
- DispList(sq);
- return 0;
- }
-
-
void InitList(SqList *&L)//初始化顺序表函数
- {
- L=(SqList*)malloc(sizeof(SqList));
- L->length=0;
- }
-
-
bool ListInsert(SqList *&L,int i,ElemType e)//插入线性表函数
- {
- int j;
- if(i<1 || i>L->length+1)//判断i是否有误
- {
- return false;
- }
- i--;
- for(j=L->length;j>i;j--)//后移
- {
-
- L->data[j]=L->data[j-1];
- }
- L->data[i]=e;//插入e
- L->length++;
- return true;
-
-
-
- }
-
-
void DispList(SqList *L)
- {
- int i;
- if (ListEmpty(L))
- return;
- for (i=0; i<L->length; i++)
- printf("%d ",L->data[i]);
- printf("\n");
- }//输出线性表
-
bool ListEmpty(SqList *L)
- {
- return(L->length==0);
- }//判断是否为空表