套用系列一的线性表类,又加了三个成员函数:
1 #include <fstream> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdlib> 5 6 using namespace std; 7 8 #define N 100 9 enum Status{success,fail,fatal,range_error,overflow}; 10 11 class List{ 12 private: 13 char *list; 14 int len; 15 16 public: 17 List(char *tlist=""){ 18 list=new char[N]; 19 strcpy(list,tlist);//初始化 20 len=strlen(list); 21 } 22 ~List(){ 23 delete list; 24 } 25 void listClear(){//销毁 26 list[0]=‘\0‘; 27 len=0; 28 } 29 bool listEmpty(){//判空 30 return len==0; 31 } 32 int listSize(){//求长度 33 return len; 34 } 35 Status listRetrieve(int pos,char &elem){//取指定位置元素 36 if(pos>=0&&pos<len){ 37 elem=list[pos]; 38 return success; 39 }else return range_error; 40 } 41 Status listLocate(char elem,int &pos);//按值查找 42 Status listInsert(int pos,char elem);//插入 43 Status listRemove(int pos);//删除 44 Status listPrior(int pos,char &elem);//求前驱 45 Status listNext(int pos,char &elem);//求后继 46 void listShow();//显示表内容 47 Status listUnion(List &list2);//集合合并 48 Status listMerge(List &list1,List &list2);//有序表合并 49 }; 50 51 int main(){ 52 //freopen("D:\\input.in","r",stdin); 53 //freopen("D:\\output.out","w",stdout); 54 List s1("hello"),s2("echo"),s10("abcdegkl"),s11("bdefhk"),s12; 55 56 s1.listShow(); 57 if(s1.listUnion(s2)==success) s1.listShow(); 58 59 s10.listShow(); 60 s11.listShow(); 61 if(s12.listMerge(s10,s11)==success) s12.listShow(); 62 return 0; 63 } 64 Status List::listLocate(char elem,int &pos){ 65 for(int i=0;i<len;i++){ 66 if(list[i]==elem){ 67 pos=i; 68 return success; 69 } 70 } 71 return fail; 72 } 73 Status List::listInsert(int pos,char elem){ 74 if(len==N-1) return overflow;//因为终止符 75 if(pos<0||pos>len) return range_error; 76 for(int i=len+1;i>pos;i--) list[i]=list[i-1]; 77 list[pos]=elem; 78 len++; 79 return success; 80 } 81 Status List::listRemove(int pos){ 82 if(pos<0||pos>=len) return range_error; 83 for(int i=pos;i<len-1;i++) list[i]=list[i+1]; 84 len--; 85 return success; 86 } 87 Status List::listPrior(int pos,char &elem){ 88 return listRetrieve(pos-1,elem); 89 } 90 Status List::listNext(int pos,char &elem){ 91 return listRetrieve(pos+1,elem); 92 } 93 void List::listShow(){ 94 puts(list); 95 } 96 Status List::listUnion(List &list2){ 97 char elem; 98 Status status; 99 int pos,len2=list2.listSize(); 100 for(int i=0;i<len2;i++){ 101 list2.listRetrieve(i,elem); 102 status=listLocate(elem,pos); 103 if(status!=success){ 104 status=listInsert(len,elem); 105 if(status!=success) break; 106 } 107 } 108 return status; 109 } 110 Status List::listMerge(List &list1,List &list2){ 111 char elem1,elem2; 112 int len1,len2,i=0,j=0; 113 Status status; 114 len1=list1.listSize(); 115 len2=list2.listSize(); 116 while(i<len1&&j<len2){ 117 list1.listRetrieve(i,elem1); 118 list2.listRetrieve(j,elem2); 119 if(elem1<elem2){ 120 status=listInsert(len,elem1); 121 i++; 122 }else{ 123 status=listInsert(len,elem2); 124 j++; 125 } 126 if(status!=success) return status; 127 } 128 while(i<len1){ 129 list1.listRetrieve(i,elem1); 130 listInsert(len,elem1); 131 if(status!=success) return status; 132 i++; 133 } 134 while(j<len2){ 135 list2.listRetrieve(j,elem2); 136 status=listInsert(len,elem2); 137 if(status!=success) return status; 138 j++; 139 } 140 return status; 141 }
敲代码期间遇到一个bug,就是字符串的终止符问题,合并时不小心把终止符给覆盖了,结果程序就飞了。
时间: 2024-10-10 09:53:59