顺序表
**
1.创建一个顺序表
**
备注:
在C语言中,typedef和define都是用来起别名的关键字,但它们的应用方式和效果却存在明显差异。typedef用于为已有的数据类型创建新的名称,而define则用于定义预处理宏,在编译时会被替换为指定的文本。
例:
#define PI 3.14159
#typedef int type int
正文:
typedef struct sqlist
{
elemtype *pList;
int length;
int listsize;
}sqlist;
#define INIT_SIZE=100;
#define LIST_SIZE=30;
void main(sqlist &q)
{
q.pList=(elemtype *)malloc(INIT_SIZE*sizeof(elemtype));
q.length=INIT_SIZE;
q.listsize=LIST_SIZE;
}
**
2.从顺序表中删除最小元素,空出位置由最后一个元素补充
**
正文:
void main(sqlist &L)
{
int CCindex=0;
for(int i=0;i<L.length;i++)
{
if(L[i]<L[i+1])
{
index=i;
}
}
L[i]=L[length-1];
length--;
}
**
3.在无序顺序表中删除s到t之间的所有元素
**
正文:
void main(int s,int t,nsqlist &L)
{
int idex=0;
for(int current=0;current<L.Length;current++)
{
int e=L[current];
if(e<s||e>t)
{
L[idex]=e;
idex++;
}
}
L.Length=idex;
}
**
4.在非减顺序表中删除值在[s,t]之间的所有元素。
**
正文:
void main(Sqlist &L)
{
int newLength;
int index=0;
while(index<=L.Length-1)
{
int e=L.plist[index];
if(e>=s)
{
break;
}
index++;
}
newLength=index;
index=L.Length-1;
while(index>=0)
{
int e=L.plist[index];
if(e<=s)
{
break;
}
index--;
}
index++;
while(index<L.Length)
{
L.plist[newLength]=L.plist[index];
newLength++;
index++;
}
L.Length=newLength;
}
总结:
先定义一个新数组长度。在定义一个数组位置变量,先while一次从前向后找到>=s并break,此时新数组长度=数组位置变量;再while一次从后向前找到<=t并break,将此时数组位置变量++;最后while(newlength<length)从数组位置变量位置值全部复制到新数组长度位置值并将二者++;
**
5.删除非递减顺序表L中重复元素。
**
正文:
void main(Sqlist &L)
{
int newlength=0;
for(int i=1;i<L.Length-1;i++)
{
int e=L.pList[i];
if(e!=L.pList[i-1])
{
L.pList[newlength]=e;
newlength++;
}
}
L.Length=index;
}
总结:
借用了非顺序删除s到t之间元素的末班修改为本题只要不和元素重复就尾插到新表中最后输出的新数组以及数组长度就是将重复元素剔除的结果
**
6.顺序表A和B元素分别是m和n,表A升序,表B降序,,两个表中都不存在相同的元素。 ①将两表合并,元素存储到C中。②表A有m+n个存储空间,将A、B两表合并,所有元素都存储到A中;
**
①正文:
void mergeAB(sqlist &A,sqlist &B,sqlist &C)//最小值法合并有序表
{
C.plist=(elemtype *)malloc(sizeof(elemtype)*length);
C.length=A.length+B.length;//表长度是A+B的和
C.listsize=0;
int currenlength=0;
int i=0;
int j=b.length-1;
while(i<A.length-1&&j>=0)//这里是与不是或 j退出的条件是大于等于0
{
if(A.plist[i]>B.plist[j])
{
C.plist[currenlength]=B.plist[j];
j--;
currenlength++;
}
else
{
C.plist[currenlength]=A.plist[i];
i++;
currenlength++;
}
}
while(i<A.length)//若i=A.length-1,说明还剩下一个没加进去所以此处这么写
{
C.plist[currenlength]=A.plist[i];
i++;
currenlength++;
}
while(j>=0)
{
C.plist[currenlength]=B.plist[j];
j--;
currenlength++;
}
free(A.plist);
free(B.plist);//此处要释放的是指针,因为你给空间的也是指针而不是结构体
A.length=B.length=0;
A.listsize=B.listsize=0;
}
②正文:
void mergeBToA(sqlist &A,sqlist &B)//本代码用最大值法求解有序表的合并
{
A.listsize=m+n;
A.plist=(elemtype*)malloc(sizeof(elemtype)*(m+n))//A有空间这里就不用新申请,新申请空间,地址,长度只适用于新的顺序表
int current=0;//当前表位置
int i=A.length-1;
int j=0;//坑点就是最大值法j如何取B值以及B的范围
while(i>=0&&j<B.length)//如何取B值,B是在自己表里取值拉进A中,所以从0开始
{
if(A.plist[i]>B.plist[j])
{
A.plist[A.length-current-1]=A.plist[i];
i--;
current++;
}
else
{
A.plist[A.length-current-1]=B.plist[j];
j++;
current++;
}
}
while(i>=0)
{
A.plist[A.length-current-1]=A.plist[i];
i--;
current++;
}
while(j<B.length)
{
A.plist[A.length-current-1]=B.plist[j];
j++;
current++;
}
A.length=B.length+A.length;//在这里更新A的长度,注意长度是随时可变的,故在最后更新长度,listsize是固定的不用更新
B.length=0;
}