头文件
//
// SqList.h
// 数据结构
//
// Created by 王思源 on 2019/1/21.
// Copyright © 2019 王思源. All rights reserved.
//
#ifndef SqList_h
#define SqList_h
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
//Status是函数的类型,其值是函数结果的状态代码
typedef int Status;
typedef int ElemType;
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct{
ElemType *elem;
int length;
int listsize;
}SqList;
Status InitList_Sq(SqList *);
Status DestroyList_Sq(SqList *);
Status ClearList_Sq(SqList *);
Status ListEmpty_Sq(SqList);
Status ListLength_Sq(SqList);
Status GetElem_Sq(SqList,int,ElemType *);
Status PriorElem_Sq(SqList,ElemType,ElemType *);
Status NextElem_Sq(SqList,ElemType,ElemType *);
Status ListInsert_Sq(SqList *,int,ElemType);
Status ListDelete_Sq(SqList *,int,ElemType *);
#endif /* SqList_h */
函数.c
//
// SqList.c
// 数据结构
//
// Created by 王思源 on 2019/1/21.
// Copyright © 2019 王思源. All rights reserved.
//
#include "SqList.h"
Status InitList_Sq(SqList *L){
//构造一个空的线性表
L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if (!L->elem) exit(OVERFLOW);
L->length=0;
L->listsize=LIST_INIT_SIZE;
return OK;
}//InitList_Sq
Status DestroyList_Sq(SqList *L){
//销毁线性表
if(L->elem==NULL)
return ERROR;
free (L->elem);
L->length=0;
L->listsize=0;
return OK;
}
Status ClearList_Sq(SqList *L){
//清空线性表
if(L->listsize==0)
return ERROR;
int i;
for(i=0;i<L->length;i++){
L->elem[i]=0;
}
L->length=0;
return OK;
}
Status ListEmpty_Sq(SqList L){
// 线性表判空
if(L.listsize==0)
return ERROR;
else if(L.length==0){
return TRUE;
}
else return FALSE;
}
Status ListLength_Sq(SqList L){
//线性表长度
if(L.listsize==0)
return ERROR;
return L.length;
}
Status GetElem_Sq(SqList L,int i,ElemType* e){
//线性表查询
if(L.listsize==0)
return ERROR;
if(i<1&&i>L.length)
return ERROR;
else {
*e=L.elem[i-1];
return OK;
}
}
Status PriorElem_Sq(SqList L,ElemType cur_e,ElemType * pre_e){
//查询前驱
if(L.listsize==0)
return ERROR;
int i;
for(i=0;i<L.length;i++){
if(L.elem[i]==cur_e){
if(i!=0) {
*pre_e=L.elem[i-1];
}
}
}
return OK;
}
Status NextElem_Sq(SqList L,ElemType cur_e,ElemType *next_e){
//查询后继
if(L.listsize==0)
return ERROR;
int i;
for(i=0;i<L.length;i++){
if(L.elem[i]==cur_e){
if(i!=L.length-1) {
*next_e=L.elem[i+1];
}
}
}
return OK;
}
Status ListInsert_Sq(SqList *L,int i,ElemType e){
//插入元素
if(L->listsize==0)
return ERROR;
if(i<1&&i>L->length+1)
return ERROR;
if(L->listsize==L->length){
ElemType *newbase;
newbase=(ElemType *)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(ElemType));
L->elem=newbase;
L->listsize=L->listsize+LISTINCREMENT;
}
int j;
for(j=L->length+1;j>i;j--)
L->elem[j-1]=L->elem[j-2];
L->elem[i-1]=e;
L->length+=1;
return OK;
}
Status ListDelete_Sq(SqList *L,int i,ElemType *e){
//删除元素
if(L->listsize==0)
return ERROR;
if(i<1&&i>L->length)
return ERROR;
*e=L->elem[i-1];
int j;
for(j=i;j<L->length;j++)
L->elem[j-1]=L->elem[j];
L->elem[L->length-1]=0;
L->length-=1;
return OK;
}
main.c
//
// main.c
// 数据结构
//
// Created by 王思源 on 2019/1/20.
// Copyright © 2019 王思源. All rights reserved.
//
#include <stdio.h>
#include "SqList.h"
int main(int argc, const char * argv[]) {
// insert code here...
printf("Hello, World!\n");
SqList L;
printf("即将构建一个空的线性表...");
InitList_Sq(&L);
printf("\n构建成功");
int i;
printf("请输入要插入的元素数目:");
scanf("%d",&i);
int j;
ElemType e;
for(j=0;j<i;j++){
printf("请输入要插入的第%d个值:",j+1);
scanf("%d",&e);
ListInsert_Sq(&L, j+1, e);
}
printf("\n输出线性表中所有元素:");
for(i=0;i<L.length;i++){
GetElem_Sq(L, i+1, &e);
printf("%d ",e);
}
printf("\n请输入要删除的元素位置:");
scanf("%d",&i);
ListDelete_Sq(&L, i, &e);
printf("\n删除成功,删除了第%d个元素,元素值为%d",i,e);
printf("\n输出线性表中所有元素:");
for(i=0;i<L.length;i++){
GetElem_Sq(L, i+1, &e);
printf("%d ",e);
}
printf("\n这个线性表是否是空表?");
i=ListEmpty_Sq(L);
if(i==TRUE) printf("\n这个线性表是空表");
else printf("\n这个线性表不是空表");
printf("\n正在清空线性表...");
ClearList_Sq(&L);
printf("\n清空线性表");
printf("\n这个线性表是否是空表?");
i=ListEmpty_Sq(L);
if(i==TRUE) printf("\n这个线性表是空表");
else printf("\n这个线性表不是空表");
printf("\n正在销毁线性表...");
DestroyList_Sq(&L);
printf("\n销毁成功");
return 0;
}
网友评论