顺序表的基本操作
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
void printList(ElemType pList[],int length)
{
for(int i = 0;i < length;i++)
{
printf("%d ",pList[i]);
}
printf("\n");
}
#define MAX_SIZE 10
typedef struct SeqList
{
ElemType pList[MAX_SIZE];
int length;
}SeqList;
void initList(SeqList &L)
{
ElemType enterData;
L.length = 0;
while(scanf("%d",&enterData) && enterData < 99999)
{
L.pList[L.length++] = enterData;
}
}
#define INIT_SIZE 100
#define INCRE_SIZE 10
typedef struct SqList
{
ElemType *pList;
int length;
int listSize;
}SqList;
void initList(SqList &L)
{
L.pList = (ElemType *)malloc(INIT_SIZE * sizeof(ElemType));
L.length = 0;
ElemType enterData;
while(scanf("%d",&enterData) && enterData < 99999)
{
L.pList[L.length++] = enterData;
}
L.listSize = INIT_SIZE;
}
void insertIthElem(SqList &L,int i,ElemType e)
{
if(i > 0 && i <= L.length && L.length < L.listSize)
{
for(int curIndex = L.length - 1;curIndex >= i - 1;curIndex--)
{
L.pList[curIndex + 1] = L.pList[curIndex];
}
L.pList[i-1] = e;
L.length += 1;
}
}
ElemType deleteIthElem(SqList &L,int i,ElemType &e)
{
if(i > 0 && i <= L.length)
{
e = L.pList[i-1];
for(int curIndex = i;curIndex < L.length;curIndex++)
{
L.pList[curIndex - 1] = L.pList[curIndex];
}
L.length -= 1;
}
return e;
}
int getElem(SqList L,int i)
{
return L.pList[i-1];
}
int getElemIth(SqList L,ElemType e)
{
for(int curIndex = 0;curIndex < L.length;curIndex++)
{
if(L.pList[curIndex] == e)
{
return curIndex + 1;
}
}
return -1;
}
void deleteMinElem(SqList &L)
{
int minIndex = 0;
for(int curIndex = 1;curIndex < L.length;curIndex++)
{
if(L.pList[curIndex] < L.pList[minIndex])
{
minIndex = curIndex;
}
}
L.pList[minIndex] = L.pList[L.length - 1];
L.length -= 1;
}
void deleteDisorderElemST(SqList &L,ElemType s,ElemType t)
{
int curLength = 0;
int curIndex = 0;
while(curIndex < L.length)
{
int e = L.pList[curIndex];
if(e < s || e > t)
{
L.pList[curLength++] = e;
}
curIndex++;
}
L.length = curLength;
}
void deleteOrderElemST(SqList &L,ElemType s,ElemType t)
{
int curIndex = 0;
int curLength = 0;
while(curIndex < L.length)
{
if(L.pList[curIndex] < s)
{
curLength = curIndex + 1;
}
else if(L.pList[curIndex] > t)
{
L.pList[curLength++] = L.pList[curIndex];
}
curIndex++;
}
L.length = curLength;
}
void deleteRepeatElem(SqList &L)
{
int curIndex = 1;
int curLength = 1;
while(curIndex < L.length)
{
if(L.pList[curIndex] > L.pList[curLength - 1])
{
L.pList[curLength++] = L.pList[curIndex];
}
curIndex++;
}
L.length = curLength;
}
void mergeABToC(SqList &La,SqList &Lb,SqList &Lc)
{
Lc.length = La.length + Lb.length;
Lc.pList = (ElemType *)malloc(sizeof(ElemType) * (Lc.length));
int curLength = 0;
int indexLa = 0;
int indexLb = Lb.length - 1;
while(curLength < Lc.length)
{
while(indexLa < La.length && indexLb >= 0)
{
if(La.pList[indexLa] < Lb.pList[indexLb])
{
Lc.pList[curLength++] = La.pList[indexLa++];
}
else
{
Lc.pList[curLength++] = Lb.pList[indexLb--];
}
}
while(indexLa < La.length)
{
Lc.pList[curLength++] = La.pList[indexLa++];
}
while(indexLb >= 0)
{
Lc.pList[curLength++] = Lb.pList[indexLb--];
}
}
free(La.pList);
La.length = 0;
La.listSize = 0;
free(Lb.pList);
Lb.length = 0;
Lb.listSize = 0;
}
void mergeABToA(SqList &La,SqList &Lb)
{
int indexLa = La.length - 1;
int indexLb = 0;
La.length = La.length + Lb.length;
int curIndex = La.length - 1;
while(curIndex >= 0)
{
while(indexLa >= 0 && indexLb < Lb.length)
{
if(La.pList[indexLa] > Lb.pList[indexLb])
{
La.pList[curIndex--] = La.pList[indexLa--];
}
else
{
La.pList[curIndex--] = Lb.pList[indexLb++];
}
}
while(indexLa >= 0)
{
La.pList[curIndex--] = La.pList[indexLa--];
}
while(indexLb < Lb.length)
{
La.pList[curIndex--] = Lb.pList[indexLb++];
}
}
free(Lb.pList);
Lb.length = 0;
Lb.listSize = 0;
}
void sortListA(SqList &La,int r)
{
int curIndex = r;
while(curIndex < La.length)
{
int e = La.pList[curIndex];
int i = curIndex;
while(i > 0 && La.pList[i - 1] > e)
{
La.pList[i ] = La.pList[i-1];
i--;
}
La.pList[i] = e;
curIndex++;
}
}
void mergeAB(SqList &La,SqList &Lb)
{
int curLength = 0;
int indexLa = 0;
int indexLb = 0;
while(indexLa < La.length && indexLb < Lb.length)
{
if(La.pList[indexLa] < Lb.pList[indexLb])
{
indexLa++;
}
else if(La.pList[indexLa] > Lb.pList[indexLb])
{
indexLb++;
}
else
{
La.pList[curLength++] = La.pList[indexLa];
indexLa++;
indexLb++;
}
}
La.length = curLength;
}
void except(SqList &La,SqList &Lb)
{
int curLength = 0;
int indexLa = 0;
int indexLb = 0;
while(indexLa < La.length && indexLb < Lb.length)
{
if(La.pList[indexLa] < Lb.pList[indexLb])
{
La.pList[curLength++] = La.pList[indexLa++];
}
else if(La.pList[indexLa] > Lb.pList[indexLb])
{
indexLb++;
}
else
{
indexLa++;
indexLb++;
}
}
La.length = curLength;
}
void reverseL(SqList &L)
{
int low = 0;
int high = L.length - 1;
while(low < high)
{
int temp = L.pList[low];
L.pList[low] = L.pList[high];
L.pList[high] = temp;
low++;
high--;
}
}
void reverseList(SqList &L,int low,int high)
{
while(low < high)
{
int temp = L.pList[low];
L.pList[low] = L.pList[high];
L.pList[high] = temp;
low++;
high--;
}
}
void leftMove(SqList &L,int r)
{
reverseList(L,0,L.length - 1);
reverseList(L,0,L.length - r - 1);
reverseList(L,L.length - r,L.length - 1);
}
void rightMove(SqList &L,int r)
{
reverseList(L,0,L.length - 1);
reverseList(L,0,r-1);
reverseList(L,r,L.length - 1);
}