线性表的基本操作:

#include<stdio.h>

#include<stdlib.h>

#define MaxSize 20

typedef int ElemType;

typedef struct

{

ElemType  data[MaxSize];

int length;

}SqList;


void InitList(SqList *&L)

{

L=(SqList*)malloc(sizeof(SqList));

L->length=0;

}

void CreateList(SqList*&L,ElemType a[],int n)

{

int i;

L=(SqList *)malloc(sizeof(SqList));

for(i=0;i<n;i++)

L->data[i]=a[i];

L->length=n;

}

void DestroyList(SqList *&L)

{

free(L);

}

bool ListEmpty(SqList *L)

{

return (L->length==0);

}

void DisplayList(SqList *L)

{

int i;

for(i=0;i<L->length;i++)

printf("% d",L->data[i]);

printf("\n");

}

bool GetElem(SqList *L,int i,ElemType &e)

{

if(i<1||i>L->length)

return false;

e=L->data[i-1];

return true;

}

int LocateElem(SqList *L,ElemType e)

{

int i=0;

while(i<L->length&&L->data[i]!=e)

{

i++;

}

if(i>=L->length)

return 0;

else

return i+1;

}

bool InsertList(SqList *&L,int i,ElemType e)

{

int j;

if(i<1||i>L->length+1)//insert range is [1,L->length+1]

return false;

i--;

for(j=L->length;j>i;j--)

{

L->data[j]=L->data[j-1];

}

L->data[i]=e;

L->length++;

return true;

}

bool DeleteList(SqList *&L,int i,ElemType &e)

{

int j;

if(i<1||i>L->length)

return false;

i--;

e=L->data[i];

for(j=1;j<L->length-1;j++)

L->data[j]=L->data[j+1];

L->length--;

return true;

}

void delnode1(SqList *&L,ElemType x)//delete all  the element is x

{

int k=0,i;

for(i=0;i<L->length;i++)

if(L->data[i]!=x)

{

L->data[k]=L->data[i];

k++;

}

L->length=k;

}

void delnode2(SqList *&L,ElemType x)

{

int k=0,i=0;

while(i<L->length)

{

if(L->data[i]==x)

k++;

else

L->data[i-k]=L->data[i];

i++;

}

L->length-=k;

}

void move1(SqList *&L)//以第一个元素为界限,键跟小雨他的元素移到他的前面,大于他的元素移到他的后面

{

int i=0,j=L->length-1;

ElemType pivot =L->data[0];

ElemType tmp;

while(i<j)

{

while(i<j&&L->data[j]>pivot)

j--;

while(i<j&&L->data[i]<=pivot)

i++;

if(i<j)

{

tmp=L->data[i];

L->data[i]=L->data[j];

L->data[j]=tmp;

}

}

tmp=L->data[0];

L->data[0]=L->data[j];

L->data[j]=tmp;

printf("i=%d\n",i);

}

void move2(SqList *&L)

{

int i=0,j=L->length-1;

ElemType pivot=L->data[0];

while(i<j)

{

while(j>i&&L->data[j]>pivot)

j--;

L->data[i]=L->data[j];

i++;

while(i<j&&L->data[i]<=pivot)

i++;

L->data[j]=L->data[i];

j--;

}

L->data[i]=pivot;

printf("i=%d\n",i);

}

void main()

{

printf("线性表基本操作:\n");

printf("1-创造一个线性表   2-插入一个元素\n");

printf("3-删除一个元素     4-得到线性表中某个位置上的元素\n");

printf("5-得到一个元素在线性表中的位置   \n  ");

printf("6-删除线性表中所有的指定元素  7-对线性表进行移动\n");

int n;

SqList *s;

bool flag=true;

while(flag)

{

printf("please enter your choose:\n");

scanf_s("%d",&n);

switch(n)

{

case 1:{

int a[10];

printf("enter the ten data :\n");

for(int i=0;i<10;i++)

scanf_s("%d",&a[i]);

CreateList(s,a,10);

DisplayList(s);

break;

  }

case 2:{

printf("enter the element you want to insert:\n");

int x=0;

scanf_s("%d",&x);

int i=0;

printf("enter the position you want to insert:\n");

scanf_s("%d",&i);

InsertList(s,i,x);

DisplayList(s);

break;

  }

case 3:{

printf("enter the position you want to delete:\n");

int j=0;

scanf_s("%d",&j);

int e=0;

DeleteList(s,j,e);

printf("the delete element is:%d\n",e);

DisplayList(s);

break;

  }

case 4:{

printf("enter the position:\n");

int d=0;

scanf_s("%d",&d);

int ee=0;

GetElem(s,d,ee);

printf("the %d position's element is %d\n",d,ee);

DisplayList(s);

break;

  }

case 5:{

printf("enter the element:\n");

int x=0;

scanf_s("%d",&x);

if(LocateElem(s,x)!=0)

{

printf("%d position is%d\n ",x,LocateElem(s,x));

}

else

printf("error\n");

DisplayList(s);

break;

  }

case 6:{

printf("enter the element:\n");

int x=0;

scanf_s("%d",&x);

delnode1(s,x);

DisplayList(s);

printf("enter the element:\n");

int xx=0;

scanf_s("%d",&xx);

delnode1(s,xx);

DisplayList(s);

break;

  }

case 7:{

move1(s);

DisplayList(s);

//move2(s);

//DisplayList(s);

break;

  }

}//switch

printf("exit enter 0\n");

int choose =0;

scanf_s("%d",&choose);

if(choose==0)

flag=false;

else

flag=true;

}//while

 system("pause");

}