线性表(顺序表)
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define false 0;
#define true 1;
#define ok 1
#define error -1
typedef int ElemType;
typedef int Status;
#define LIST_INIT_SIZE 80
typedef struct SqList{
ElemType *elem;
int length;
int listsize;
}SqList;
Status InitSqList_Sq(SqList *L)
{
L->elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if(!L->elem){
printf("存储空间分配失败\n");
return error;
}
L->length = 0;
L->listsize = LIST_INIT_SIZE;
printf("线性表初始化完成\n");
return ok;
}
void Increase(SqList *List, int len){
int i;
ElemType *p = List->elem;
List->elem = (ElemType*)malloc((List->listsize+len)*sizeof(ElemType));
for(i = 0; i < List->length; i++){
List->elem[i] = p[i];
}
List->listsize = List->listsize + len;
free(p);
printf("成功增加了空间\n");
}
Status ValueList_Sq(SqList *L)
{
int i, j;
printf("请输入线性表的元素个数:");
scanf("%d", &i);
if(i>L->listsize)
{
printf("赋值元素个数超出表长\n");
}
else{
for(j = 0; j < i; j++){
scanf("%d", &L->elem[j]);
}
}
L->length = i;
printf("赋值成功!\n");
return ok;
}
Status DestroyList_Sq(SqList *L)
{
if(!L->elem){
printf("线性表不存在\n");
}
free(L->elem);
L->elem = NULL;
L->length = 0;
L->listsize = 0;
printf("线性表销毁成功\n");
return ok;
}
Status EmptyList_Sq(SqList *L)
{
if(L->elem)
{
if(L->length != 0){
printf("线性表不是空表\n");
return ok;
}
else{
printf("线性表是空表\n");
return error;
}
}
else
printf("线性表不存在\n");
return error;
}
Status Length1(SqList *L)
{
if(L->elem)
return L->length;
else{
printf("线性表(顺序表)不存在\n");
return false;
}
return ok;
}
Status LocateElem(SqList *L, ElemType e){
int i;
for(i = 0; i < L->length; i++){
if(L->elem[i] == e){
printf("查找成功\n");
return i;
}
}
return error;
}
Status GetElem(SqList *L, int e){
int i;
if(L->length == 0){
printf("当前线性表中没有值\n");
return error;
}
else{
if(e>=0 && e < L->length){
for(i = 0; i < L->length; i++){
if(i == e){
return i;
}
}
}
if(e < 0){
printf("位序应该大于等于0\n");
return error;
}
if(e >= L->length){
printf("位值超过线性表位序(0~%d)\n",L->length-1);
return error;
}
}
return error;
}
void PrintList(SqList *L){
int i;
for(i = 0; i < L->length; i++){
if(i < (L->length-1))
printf("%d,",L->elem[i]);
else
printf("%d\n",L->elem[i]);
}
}
Status ListInsert(SqList *L, int i, ElemType e){
int j;
if (i < 1 || i > L->length-1){
return error;
}
if(L->length >= L->listsize)
return error;
for(j = L->length; j >= i; j--)
{
L->elem[j] = L->elem[j-1];
}
L->elem[i - 1] = e;
L->length++;
return ok;
}
Status ListDelete(SqList *L, int i, ElemType *p){
int j;
if(i < 1 || i > L->length)
return error;
int *q = &(L->elem[i-1]);
*p = *q;
for(j = i; j < L->length; j++)
{
L->elem[j-1] = L->elem[j];
}
L->length--;
return ok;
}
int main()
{
int t;
int n;
int i;
ElemType e;
ElemType m;
ElemType p;
struct SqList list;
InitSqList_Sq(&list);
while (1)
{
printf("================================\n");
printf(" 线性表\n");
printf("================================\n");
printf("1:求表长 2:增加线性表空间 3:对线性表赋值\n");
printf("4:查找指定元素 5:按位查找 6:打印线性表\n");
printf("7:插入元素 8:删除操作 9:判断是否为空\n");
printf("10:退出\n");
printf("--------------------------------\n");
printf("请选择:");
scanf("%d", &t);
switch (t) {
case 1:
printf("表长为:%d\n",Length1(&list));
break;
case 2:
printf("请输入要增多大的空间:");
scanf("%d", &n);
Increase(&list, n);
break;
case 3:
ValueList_Sq(&list);
break;
case 4:
printf("请输入你要查找的值:");
scanf("%d",&e);
m = LocateElem(&list,e);
if(m != -1)
printf("%d\n",list.elem[m]);
else
printf("没有指定元素\n");
break;
case 5:
printf("请输入你要查找的位序:");
scanf("%d",&e);
if(GetElem(&list,e) != -1){
printf("查找成功,值为:%d\n",list.elem[GetElem(&list,e)]);
}
break;
case 6:
PrintList(&list);
break;
case 7:
printf("请输入想在第几位插入元素:");
scanf("%d",&i);
printf("插入元素的值为:");
scanf("%d",&e);
if(ListInsert(&list,i,e) == 1){
printf("插入成功\n");
}else{
printf("插入失败\n");
}
break;
case 8:
printf("请输入想删除第几个元素:");
scanf("%d",&i);
if(ListDelete(&list, i, &p) == 1){
printf("删除成功,删除元素值为:%d\n",p);
}else{
printf("删除失败\n");
}
break;
case 9:
EmptyList_Sq(&list);
break;
case 10:
printf("线性表模拟结束Bye!\n");
exit(0);
break;
}
}
return 0;
}