//线性表的使用及函数定义 #include#include #define _CRT_SECURE_NO_WARNINGS_ #define OK 2 #define ERROR -1 #define TRUE 1 #define FALSE 0 #define OVERFLOW -2 #define Status int//用数值返回状态,状态值如上所示。 #define ElemType char//串一半都是字符串。 #define InitSize 100 #define IncreaseSize 10 typedef struct str { ElemType* base; int Alllen; int CurLen; }String; Status StrAssign(String& S) { S.base = (ElemType*)malloc(InitSize * sizeof(ElemType)); S.Alllen = InitSize; S.CurLen = 0; }//只是定义了一个空包,没有往里边放数值,这个得到时候去问一下老师,或者去查一下 int StrLen(String& S) { return S.CurLen; } Status StrCopy(String& S, String T) { S.base = (ElemType*)realloc(S.base, T.Alllen * sizeof(ElemType)); if (!S.base) return OVERFLOW; for (int i = 0; i < T.CurLen; i++)S.base[i] = T.base[i]; S.CurLen = T.CurLen; S.Alllen = T.Alllen; return OK; }//将T串全部复制到S串里边。 Status StrEmpty(String& S) { if (S.CurLen = 0) return TRUE; else return FALSE; }//判断串是否为空,如果串是空的,返回真,反之返回假 Status max(int a, int b) { if (a < b) return b; else return a; }//取a,b之中更大的一项返回 int StrCamp(String S, String T) { if (S.CurLen > T.CurLen) return 1; if (S.CurLen < T.CurLen) return -1; if (S.CurLen == T.CurLen) { int flag = 1; int i = 0; while (flag&&i<=max(S.CurLen,T.CurLen)) { if (S.base[i] == T.base[i]) flag = flag; else flag = 0; i++; } if (i == max(S.CurLen, T.CurLen)) return 0; else { if (S.base[i] > T.base[i]) return 1; else return -1; } } }//比较:先比较长度,如果长度相等,判断第一个不相等字符的大小。S>T 1;S



