根据以下代码改写:
//顺序表运算算法 #include <stdio.h> #include <malloc.h> #define MaxSize 50 typedef char ElemType; typedef struct { ElemType data[MaxSize]; //存放顺序表元素 int length; //存放顺序表的长度 } SqList; //声明顺序表的类型 void CreateList(SqList *&L,ElemType a[],int n) //整体建立顺序表 { L=(SqList *)malloc(sizeof(SqList)); for (int i=0;i<n;i++) L->data[i]=a[i]; L->length=n; } void InitList(SqList *&L) //初始化线性表 { L=(SqList *)malloc(sizeof(SqList)); //分配存放线性表的空间 L->length=0; } void DestroyList(SqList *&L) //销毁线性表 { free(L); } bool ListEmpty(SqList *L) //判线性表是否为空表 { return(L->length==0); } int ListLength(SqList *L) //求线性表的长度 { return(L->length); } void DispList(SqList *L) //输出线性表 { for (int i=0;i<L->length;i++) printf("%c ",L->data[i]); printf("\n"); } bool GetElem(SqList *L,int i,ElemType &e) //求线性表中第i个元素值 { if (i<1 || i>L->length) return false; e=L->data[i-1]; return true; } int LocateElem(SqList *L, ElemType e) //查找第一个值域为e的元素序号 { int i=0; while (i<L->length && L->data[i]!=e) i++; if (i>=L->length) return 0; else return i+1; } bool ListInsert(SqList *&L,int i,ElemType e) //插入第i个元素 { int j; if (i<1 || i>L->length+1) return false; i--; //将顺序表位序转化为elem下标 for (j=L->length;j>i;j--) //将data[i]及后面元素后移一个位置 L->data[j]=L->data[j-1]; L->data[i]=e; L->length++; //顺序表长度增1 return true; } bool ListDelete(SqList *&L,int i,ElemType &e) //删除第i个元素 { int j; if (i<1 || i>L->length) return false; i--; //将顺序表位序转化为elem下标 e=L->data[i]; for (j=i;j<L->length-1;j++) //将data[i]之后的元素前移一个位置 L->data[j]=L->data[j+1]; L->length--; //顺序表长度减1 return true; } //文件名:exp2-1.cpp #include "sqlist.cpp" int main() { SqList *L; ElemType e; printf("顺序表的基本运算如下:\n"); printf(" (1)初始化顺序表L\n"); InitList(L); printf(" (2)依次插入a,b,c,d,e元素\n"); ListInsert(L,1,'a'); ListInsert(L,2,'b'); ListInsert(L,3,'c'); ListInsert(L,4,'d'); ListInsert(L,5,'e'); printf(" (3)输出顺序表L:"); DispList(L); printf(" (4)顺序表L长度:%d\n",ListLength(L)); printf(" (5)顺序表L为%s\n",(ListEmpty(L)?"空":"非空")); GetElem(L,3,e); printf(" (6)顺序表L的第3个元素:%c\n",e); printf(" (7)元素a的位置:%d\n",LocateElem(L,'a')); printf(" (8)在第4个元素位置上插入f元素\n"); ListInsert(L,4,'f'); printf(" (9)输出顺序表L:"); DispList(L); printf(" (10)删除L的第3个元素\n"); ListDelete(L,3,e); printf(" (11)输出顺序表L:"); DispList(L); printf(" (12)释放顺序表L\n"); DestroyList(L); return 1; }改写之后的代码: LineatList1.h
#ifndef LINERLIST_H #include <iostream> #include <malloc.h> #include <string> using namespace std; const int MaxSize = 50; const int Length = 6; typedef char ElemType; typedef struct { ElemType data[MaxSize]; //存放顺序表元素 int length; //存放顺序表的长度 } SqList; void CreateList(SqList*& L, ElemType a[], int n); void InitList(SqList*& L); void DestroyList(SqList*& L); bool ListEmpty(SqList* L); int ListLength(SqList* L); void DispList(SqList* L); bool GetElem(SqList* L, int i, ElemType& e); int LocateElem(SqList* L, ElemType e); bool ListInsert(SqList*& L, int i, ElemType e); bool ListDelete(SqList*& L, int i, ElemType& e); #endifLinearList1.cpp
#ifndef LINERLIST_C #include "LinearList1.h" void CreateList(SqList*& L, ElemType a[], int n) //整体建立顺序表 { L = (SqList*)malloc(sizeof(SqList)); if (L == NULL) //VS一定要有判断内存是否分配成功,不然报错; { //printf("内存分配不成功!\n"); cout << "内存分配不成功!" << endl; } else { for (int i = 0; i < n; i++) L->data[i] = a[i]; L->length = n; } } void InitList(SqList*& L) //初始化线性表 { L = (SqList*)malloc(sizeof(SqList)); //分配存放线性表的空间 if (L == NULL) { //printf("内存分配不成功!\n"); cout << "内存分配不成功!" << endl; } else { L->length = 0; } } void DestroyList(SqList*& L) //销毁线性表 { free(L); } bool ListEmpty(SqList* L) //判线性表是否为空表 { return(L->length == 0); } int ListLength(SqList* L) //求线性表的长度 { return(L->length); } void DispList(SqList* L) //输出线性表 { for (int i = 0; i < L->length; i++) //printf("%c ", L->data[i]); cout << L->data[i]; //printf("\n"); cout << endl; } bool GetElem(SqList* L, int i, ElemType& e) //求线性表中第i个元素值 { if (i<1 || i>L->length) return false; e = L->data[i - 1]; return true; } int LocateElem(SqList* L, ElemType e) //查找第一个值域为e的元素序号 { int i = 0; while (i < L->length&& L->data[i] != e) i++; if (i >= L->length) return 0; else return i + 1; } bool ListInsert(SqList*& L, int i, ElemType e) //插入第i个元素 { int j; if (i<1 || i>L->length + 1) return false; i--; //将顺序表位序转化为elem下标 for (j = L->length; j > i; j--) //将data[i]及后面元素后移一个位置 L->data[j] = L->data[j - 1]; L->data[i] = e; L->length++; //顺序表长度增1 return true; } bool ListDelete(SqList*& L, int i, ElemType& e) //删除第i个元素 { int j; if (i<1 || i>L->length) return false; i--; //将顺序表位序转化为elem下标 e = L->data[i]; for (j = i; j < L->length - 1; j++) //将data[i]之后的元素前移一个位置 L->data[j] = L->data[j + 1]; L->length--; //顺序表长度减1 return true; } #endifmain.cpp
//文件名:main.cpp #include "LinearList1.h" int main() { SqList* L; ElemType e; //printf("顺序表的基本运算如下:\n"); cout << "顺序表的基本运算如下:" << endl; //printf(" (1)初始化顺序表L\n"); cout << " (1)初始化顺序表L" << endl; InitList(L); //printf(" (2)依次插入a,b,c,d,e元素\n"); //cout << "(2)依次插入a,b,c,d,e元素" << endl; cout << " (2)依次插入有序的" << Length << "个元素" << endl; //cout << "请输入个元素,以空格键分开:" << endl; cout << " 请输入" << Length << "个元素,以空格键隔开" << endl; int i; char ch; for (i = 1; i <= Length; i++) { cin >> ch; ListInsert(L, i, ch); } /*ListInsert(L, 1, 'a'); ListInsert(L, 2, 'b'); ListInsert(L, 3, 'c'); ListInsert(L, 4, 'd'); ListInsert(L, 5, 'e');*/ //printf(" (3)输出顺序表L:"); cout << " (3)输出顺序表L:"; DispList(L); //printf(" (4)顺序表L长度:%d\n", ListLength(L)); cout << " (4)顺序表L长度:" << ListLength(L) << endl; //printf(" (5)顺序表L为%s\n", (ListEmpty(L) ? "空" : "非空")); string str = (ListEmpty(L)) ? "空" : "非空";//使用了字符串,记得添加#include <string> cout << " (5)顺序表L为" << str << endl; cout << " 输入想查看的元素的位置:"; int j; cin >> j; //cin >> ch; GetElem(L, j, e); //GetElem(L, 3, e); //printf(" (6)顺序表L的第3个元素:%c\n", e); cout << " (6)顺序表L的第" << j << "个元素为:" << e << endl; //(" (7)元素a的位置:%d\n", LocateElem(L, 'a')); //cout << "输入你想查看位置的元素:"; //char ch1; //string ch1; //cin >> ch1; //cout << " (7)元素" << ch1 << "的位置" << LocateElem(L, 'ch1') << endl; cout << " (7)元素a的位置为:" << LocateElem(L, 'a') << endl; //printf(" (8)在第4个元素位置上插入f元素\n"); //cout << " 请输入想插入的元素和将要插入的位置:"; //char ch2; string ch2; //cin >> ch2 >> j; cout << " 请输入要插入的位置:"; cin >> j; //cout << " 在第" << j << "个元素的位置插上" << ch2 << "元素" << endl; cout << "(8)在第" << j << "个位置插入元素f" << endl; //ListInsert(L, j, 'ch2'); ListInsert(L, j, 'f'); //printf(" (9)输出顺序表L:"); cout << " (9)输出顺序表:"; DispList(L); //printf(" (10)删除L的第3个元素\n"); cout << " 请输入想删除的元素位置:"; int k; cin >> k; cout << " (10)删除L的第" << k << "个元素" << endl; ListDelete(L, k, e); //printf(" (11)输出顺序表L:"); cout << " (11)输出顺序表L:"; //这里不用换行 DispList(L); //printf(" (12)释放顺序表L\n"); cout << " (12)释放顺序表L" << endl; DestroyList(L); return 1; //为什么是return 1 而不是 return 0 ? }