截图: 代码:
#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); #endif #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; } #endif //文件名:main.cpp #include "LinearList1.h" int main() { SqList* L; ElemType e; cout << " 顺序表的基本运算如下:" << endl; cout << " (1)初始化顺序表L" << endl; InitList(L); cout << " (2)依次插入有序的" << Length << "个元素" << endl; cout << " 请输入" << Length << "个元素,以空格键隔开" << endl; int i; char ch; for (i = 1; i <= Length; i++) { cin >> ch; ListInsert(L, i, ch); } cout << " (3)输出顺序表L:"; DispList(L); cout << " (4)顺序表L长度:" << ListLength(L) << endl; string str = (ListEmpty(L)) ? "空" : "非空";//使用了字符串,记得添加#include <string> cout << " (5)顺序表L为" << str << endl; cout << " 输入想查看的元素的位置:"; int j; cin >> j; GetElem(L, j, e); cout << " (6)顺序表L的第" << j << "个元素为:" << e << endl; cout << "输入你想查看位置的元素:"; //string ch1; //网上查的方法,没有解决问题 截断 cin >> ch; cout << " (7)元素" << ch << "的位置" << LocateElem(L, 'ch') << endl; cout << " 请输入想插入的元素和将要插入的位置:"; //string ch2; cin >> ch >> j; cout << " 在第" << j << "个元素的位置插上" << ch << "元素" << endl; ListInsert(L, j, 'ch'); cout << " (9)输出顺序表:"; DispList(L); cout << " 请输入想删除的元素位置:"; int k; cin >> k; cout << " (10)删除L的第" << k << "个元素" << endl; ListDelete(L, k, e); cout << " (11)输出顺序表L:"; //这里不用换行 DispList(L); cout << " (12)释放顺序表L" << endl; DestroyList(L); return 1; //为什么是return 1 而不是 return 0 ? } typedef char ElemType; typedef struct { ElemType data[MaxSize]; //存放顺序表元素 int length; //存放顺序表的长度 } SqList;原因分析: 发现(7)查看元素位置出错,位置是0,即不在这个顺序表中; (8)无论我想插入那个元素,最后都插入的是h; 其他地方都正常。
分析1:我调用的问题吗?
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; } 我调用的这个函数:warning cin >> ch; cout << " (7)元素" << ch << "的位置" << LocateElem(L, 'ch') << endl; 我有改为:warning ch = 'f'; cout << " (7)元素" << ch << "的位置" << LocateElem(L, 'ch') << endl;分析2:函数的问题?因为存储位置不一样吗?必须是顺序表里面的元素才行,而ch是重新分配的存储空间, 分析3:不对,ch是以ASCI存放的,是int型,所以总是提醒我"int"到”ElemType"会截断 截断截断应该是长变短了嘛 那我如果强制类型转换的话,会解决吗?试一试
分析4:这样编译是没有warning,但是运行的结果还是不行。
cin >> ch; //ch = 'f'; cout << " (7)元素" << ch << "的位置" << LocateElem(L, char('ch')) << endl; cout << " 请输入想插入的元素和将要插入的位置:"; //string ch2; cin >> ch >> j; cout << " (8)在第" << j << "个元素的位置插上" << ch << "元素" << endl; ListInsert(L, j, char('ch'));分析5:我又试了这种:没有warning 但是运行结果就是不对;心态炸了呜呼
cin >> ch; //ch = 'f'; cout << " (7)元素" << ch << "的位置" << LocateElem(L, ElemType('ch')) << endl; cout << " 请输入想插入的元素和将要插入的位置:"; //string ch2; cin >> ch >> j; cout << " (8)在第" << j << "个元素的位置插上" << ch << "元素" << endl; ListInsert(L, j, ElemType('ch'));分析5:睡了一觉我有回来了 梦中我想着用ElemType作为变量的数据类型,醒来匆匆打开电脑测试了一下,梦想真的美妙,但是现实是依旧不行的.然后想到昨天似乎有个博客是叫用ASCII的,于是我改成用ASCII码,然后解决了(至于我为什么不试试,完全是懒呀,不想去翻厚厚的书,本来百度一下就可以的,唉思维的惰性;还有就是觉得如果每次都用ASCI码,不符合我要求程序达到的功能,我要的是输入字符,而不是每次都输入ASCI码.)虽然现在解决的这个问题,但问题依旧存在,怎么样可以不用ASCII码就能达到要求呢?
cout << "输入你想查看位置的元素:"; //string ch1; //网上查的方法,没有解决问题 截断 //cin >> ch; 把ch的类型定为ElemType试试(睡觉想到的) //ElemType ch1; //这个也不行呀 用ASCI码试试 //cin >> ch1; //ch = 'f'; 这个也不行 int a; cin >> a; int x = LocateElem(L,ElemType(a));//这里必须要强制类型转换,不然就会截断 cout << " (7)元素" <<ElemType(a)<< "的位置" << x << endl; cout << " 请输入想插入的元素和将要插入的位置(元素的位置用ASCII码:a为97,z为122:"; //string ch2; //cin >> ch1; cin >> a; int m; cin >> m; cout << " (8)在第" << m << "个元素的位置插上" << ElemType(a) << "元素" << endl; ListInsert(L, m, ElemType(a));输出: