本文主要實現了線性表中順序表的初始化、創建、增加、刪除、修改、清空,判斷表長、判斷表長等基本操作。
主要依據嚴蔚敏版數據結構教材以及王道數據結構考研輔導書。
基本函數如下:
int InitList(SqList &L);//初始化
bool CreateList(SqList &L, int n);
int Length(SqList L);//返回長度信息
bool ListInsert(SqList &L, int i, ElemType e);//前插
bool ListDelete(SqList &L, int i, ElemType &e);//刪除
int LocateElem(SqList L, ElemType e); //查找值爲e的第一個元素的位置
ElemType GetElem(SqList L, int i);//獲取第i個位置的值
void PrintList(SqList L);//輸出操作
bool Empty(SqList L);//判空
void DestroyList(SqList &L);//銷燬,表結構不存在
void Clear(SqList &L);//清空操作,表結構依然存在
功能菜單如下:
以下是詳細代碼(VS2015編譯通過):
#include <iostream>
#include <cstring>//memset的頭文件頭文件
#include <stdlib.h>//exit的頭文件
#define MaxSize 50
#define ElemType int
//注意:我們平時所說的第幾個,如第i個元素,對應數組下標的i-1
using namespace std;
typedef struct {
ElemType data[MaxSize];
int length;
}SqList;
/*-----------------------------基本函數-------------------*/
int InitList(SqList &L);//初始化
bool CreateList(SqList &L, int n);
int Length(SqList L);//返回長度信息
bool ListInsert(SqList &L, int i, ElemType e);//前插
bool ListDelete(SqList &L, int i, ElemType &e);//刪除
int LocateElem(SqList L, ElemType e); //查找值爲e的第一個元素的位置
ElemType GetElem(SqList L, int i);//獲取第i個位置的值
void PrintList(SqList L);//輸出操作
bool Empty(SqList L);//判空
void DestroyList(SqList &L);//銷燬,表結構不存在
void Clear(SqList &L);//清空操作,表結構依然存在
/*-----------功能性函數--------------------*/
bool Create(SqList &L);//創建表
void Insert(SqList &L);
void Delete(SqList &L);
void Search(SqList L);
void showLength(SqList L);
void isEmpty(SqList L);
void menu();
void menu() {
cout << "********1.創建 2.插入*********" << endl;
cout << "********3.刪除 4.查找*********" << endl;
cout << "********5.輸出 6.求表長*********" << endl;
cout << "********7.判表空 8.清空***********" << endl;
cout << "********9.退出********************" << endl;
}
int main() {
SqList L;
int choice;
InitList(L);
while (1)
{
menu();
cout << "請輸入菜單序號:" << endl;
cin >> choice;
if (choice == 9) break;
switch (choice)
{
case 1:Create(L);break;
case 2:Insert(L);break;
case 3:Delete(L);break;
case 4:Search(L);break;
case 5:PrintList(L);break;
case 6:showLength(L);break;
case 7:isEmpty(L);break;
case 8:Clear(L);break;
default:cout << "輸入錯誤!!!" << endl;
}
}
return 0;
}
/*-----------------------------基本函數-------------------*/
int InitList(SqList &L) {//初始化
//L.data=(int *)malloc(sizeof(int) * MaxSize);
/*void *memset(void *s, int ch, size_t n);
將s中當前位置後面的n個字節用 ch 替換並返回 s 。*/
memset(L.data, 0, sizeof(L));
if (!L.data)
exit(0);
L.length = 0;
return 0;
}
bool CreateList(SqList &L, int n) {//創建表
for (int i = 0;i<n;i++) {
cin >> L.data[i];
L.length++;
}
return true;
}
int Length(SqList L) {//求表長
return L.length;
}
int LocateElem(SqList L, ElemType e) {
for (int i = 0;i<L.length;i++)
{
if (L.data[i] == e)
{
return i + 1;
}
}
return 0;
}
ElemType GetElem(SqList L, int i) {
return L.data[i - 1];
}
bool ListInsert(SqList &L, int i, ElemType e) {
if (i<1 || i>L.length + 1)//如果位置是第一個之前,或者表長長度+1之後(可在最後一位後插入),則位置不合法
return false;
if (L.length >= MaxSize)
return false;
for (int j = L.length;j >= i - 1;j--)//整體往後挪
{
L.data[j] = L.data[j - 1];
}
L.data[i - 1] = e;//目標插入點被賦值
L.length++;
return true;
}
bool ListDelete(SqList &L, int i, ElemType &e) {
if (i<1 || i>L.length + 1)
return false;
if (L.length >= MaxSize)
return false;
e = L.data[i - 1];
for (int j = i;i<L.length;j++) {//前移
L.data[j - 1] = L.data[j];
L.length--;
}
cout << "刪除成功!!!" << endl;
return true;
}
void PrintList(SqList L) {
cout << "表中元素分別爲";
for (int i = 0;i<L.length;i++)
{
cout << L.data[i] << " ";
}
cout << endl;
}
bool Empty(SqList L) {
if (L.length == 0)
return true;
return false;
}
void DestroyList(SqList &L) {
//考的實際上是數組的空間釋放問題
}
void Clear(SqList &L) {
L.length = 0;
cout << "表已清空" << endl;
}
/****************功能性函數*****************/
bool Create(SqList &L) {
int n;
cout << "請輸入要創建的表的長度:" << endl;
cin >> n;
if (n<0 && n>MaxSize)
return false;
cout << "請輸入這" << n << "個數(用空格隔開)" << endl;
if (CreateList(L, n))//判斷即調用過
cout << "創建成功!" << endl;
return true;
}
void showLength(SqList L) {
cout << "表長爲" << Length(L) << endl;
}
void Insert(SqList &L) {
int n;
ElemType e;
cout << "請輸入想要插入的位置:" << endl;
cin >> n;
cout << "請輸入第" << n << "個位置上的元素的值:" << endl;
cin >> e;
ListInsert(L, n, e);
}
void Delete(SqList &L) {
int n;
ElemType e;
cout << "請輸入想刪除的元素的位置:" << endl;
cin >> n;
ListDelete(L, n, e);
}
void Search(SqList L) {
int choice, i;
ElemType e;
cout << "1.按值查找;2.按序號查找;3.返回上一步" << endl;
cout << "請輸出你選擇操作的序號:" << endl;
cin >> choice;
switch (choice) {
case 1:
cout << "請輸入要查找的元素的值:";
cin >> e;
cout << "值爲" << e << "的元素在第" << LocateElem(L, e) << "個位置" << endl;
break;
case 2:
cout << "請輸入要查找的元素的序號:";
cin >> i;
cout << "第" << i << "個位置上的元素是" << GetElem(L, i) << endl;
break;
case 3:
break;
default:
cout << "輸入有誤!" << endl;
break;
}
}
void isEmpty(SqList L) {
if (Empty(L))
cout << "表爲空" << endl;
else
cout << "表非空" << endl;
}
有不足請批評指正