数据结构实验-线性表

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define MAXSIZE 101

typedef int DataType;

typedef struct {
	int *data;
	int length;
}SeqList;


// 1.Init 
void InitList (SeqList* L) {
	L->data = new int [MAXSIZE];
	L->length = 0;
}

//2. Create
bool CreateList (SeqList* L, DataType a[], int n) {
	if(n > MAXSIZE) {
		printf("The space is not enough, cann't create a List\n");
		return false;
	} else {
		for(int i = 0; i < n; i ++ ) {
			L->data[i] = a[i];
		}
		L->length = n;
		return 1;
	}
}

// 3. get the length of List
int GetLength(SeqList* L) {
	return L -> length;
}

// 4. print the List
void PrintList(SeqList* L) {
	for(int i = 0; i < L->length; i ++ ) {
		printf("%d%c", L->data[i], " \n"[i == L->length - 1]);
	}
}

// 5. return  the first index of this value  (index start at 1 not 0)
// if the key of return is zero , means a query failure
int Locate(SeqList* L, DataType value) {
	for(int i = 0; i < L->length; i ++ ) {
		if(L->data[i] == value) {
			return i + 1;
		}
	}
	return 0;
}


// 6.  find the value of the index from the List
bool GetValue(SeqList* L, int idx, int* value) { 
	if(idx < 1 || idx > L->length) {
		return false;
	} else {
		*value = L->data[idx - 1]; // store in value
		return true;
	}
}

// 7. insert The range of insert is [1, L->length + 1] (idx - 1) 
bool Insert(SeqList* L,	int idx, int value) {
	if(L->length >= MAXSIZE) {
		return false;
	} else if(idx < 1 || idx > L->length + 1) {
		return false;
	} else {
		for(int i = L->length; i >= idx; i -- ) {
			L->data[i] = L->data[i - 1];
		}
		L->data[idx - 1] = value;
		++ L->length;
		return true;
	}
}

// 8. delete  
bool Delete(SeqList* L,	int idx, int* value) {
	if(L->length == 0) {
		printf("The list is empty, delete fault\n");
		return false;
	} else if(idx < 1 || idx > L->length) {
		printf("The index is illegal, delete fault\n");
		return false;
	} else {
		*value = L->data[idx - 1]; // store in value
		for(int i = idx; i < L->length; i ++ ) {
			L->data[i - 1] = L->data[i];
		}
		-- L->length;
		return true;
	}
}

// 9. judge the list is empty
bool Empty(SeqList* L) {
	return (L->length == 0);
}

// 10. erase the list 

void erase(SeqList *L) {
	delete L->data;
	L->length = 0;
}

// 11. calcate one's prev

int GetPrev(SeqList *L, int idx, int *value) {
	if(idx <= 1 || idx > L->length) {
		return false;  // not exist
	} else {
		*value = L->data[idx - 2]; //start at zero
		return true;
	}
}

// 12. calcate one's next

bool GetNext(SeqList *L, int idx, int *value) {
	if(idx < 1 || idx > L->length - 1) {
		return false;
	} else {
		*value = L->data[idx];
		return true;
	}
}

//print the list
void print() {
	printf("1----清空线性表\n");
	printf("2----判断线性表是否为空\n");
	printf("3----求线性表长度\n");
	printf("4----获取线性表指定位置元素\n");
	printf("5----求前驱\n");
	printf("6----求后继\n");
	printf("7----在线性表指定位置插入指定元素\n");
	printf("8----删除线性表指定位置元素\n");
	printf("9----显示线性表\n");
	printf("     推出,输出一个负数!\n");
	printf("注意:线性表的下标从 1 开始,线性表的最大长度是 100\n");
	printf("请输入操作代码: "); 
}

int main() {
	
	SeqList L;
	InitList(&L);
	
	while(true) {
		print();
		int op, x, idx; scanf("%d", &op);
		
		if(op < 0) {
			printf("操作结束,再见!\n");
			break;
		} else if(op == 1) {
			erase(&L);
		} else if(op == 2) {
			if(Empty(&L)) printf("线性表为空");
			else printf("线性表不为空");
		} else if(op == 3) {
			printf("线性表的长度为: %d\n", GetLength(&L));
		} else if(op == 4) {
			printf("请输入要查询的位置: "); scanf("%d", &idx);
			if(GetValue(&L, idx, &x)) { 
				printf("位于 %d 的元素是: %d\n", idx, x);
			} else {
				printf("查询失败,请检查输入是否合法\n");
			}
		} else if(op == 5){
			printf("请输入要查询的位置: "); scanf("%d", &idx);
			if(GetPrev(&L, idx, &x)) {
				printf("位置 %d 的前驱元素是: %d\n", idx, x);
			} else {
				printf("查询失败,请检查输入是否合法\n");
			}
		} else if(op == 6){
			printf("请输入要查询的位置: "); scanf("%d", &idx);
			if(GetNext(&L, idx, &x)) {
				printf("位置 %d 的后继元素是: %d\n", idx, x);
			} else {
				printf("查询失败,请检查输入是否合法\n");
			}
		} else if(op == 7) {
			printf("请输入要插入的位置: "); scanf("%d", &idx);
			printf("请输入要插入的元素: "); scanf("%d", &x);
			if(Insert(&L, idx, x)) {
				printf("插入成功!\n");
			} else {
				printf("插入失败!请检出输入是否合法\n");
			}
		} else if(op == 8) {
			printf("请输入要删除的位置: "); scanf("%d", &idx);
			if(Delete(&L, idx, &x)) {
				printf("删除成功!\n 删除的位置的元素是 %d\n", x);
			} else {
				printf("删除失败,请检查输入是否合法\n");
			}
		} else if(op == 9) {
			PrintList(&L); 
		}
		Sleep(1000);
		system("cls");
	}
	
	return 0;
}
posted @ 2022-03-18 20:51  ccz9729  阅读(41)  评论(0编辑  收藏  举报