#include<iostream>
#include<malloc.h>
using namespace std;
#define MaxSize 100
typedef int ElemType;
typedef struct{
int length;
ElemType data[MaxSize];
}SqList;
void CreateList(SqList *&L,ElemType a[],int n)
{
L=(SqList *)malloc(sizeof(SqList));
int i=0;
int k=0;
while(i<n)
{
L->data[i]=a[i];
i++;
k++;
}
L->length=k;
} //½¨Ë³Ðò±í
void InitList(SqList *&L)
{
L=(SqList *)malloc(sizeof(SqList));
L->length=0;
}
void DeleteList(SqList *&L)
{
free(L);
}
bool ListEmpty(SqList *L)
{
return L->length==0;
}
int ListLength(SqList *L)
{
return L->length;
}
void DispList(SqList *L)
{
int i;
for(i=0;i<L->length;i++)
cout<<L->data[i];
cout<<"\n"<<endl;
}
bool GetElem(SqList *&L,int i,ElemType &e)
{
if(i<1||i>L->length)
return false;
else
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 ListInsert(SqList *&L,int i,ElemType e)
{
if(i<1||i>L->length+1)
return false;
int j;
i--;
for(j=L->length;j>i;j--)
L->data[j]=L->data[j-1];
L->data[i]=e;
L->length++;
return true;
}
bool ListDelete(SqList *&L,int i,ElemType &e)
{
if(i<1||i>L->length)
return false;
int j;
i--;
e=L->data[i];
for(j=i;j<L->length;j++)
L->data[j]=L->data[j+1];
L->length--;
return false;
}
int partition1(SqList *&L)
{
int i=0,j=L->length-1;
ElemType pivot=L->data[0];
while(i<j)
{
while(i<j&&L->data[j]>pivot)
j--;
while(i<j&&L->data[i]<=pivot)
i++;
if(i<j)
swap(L->data[i],L->data[j]);
}
swap(L->data[0],L->data[i]);
}
void partition2(SqList *&L)
{
int i=0;int j=L->length-1;
ElemType pivot=L->data[0];
while(i<j)
while(i<j&&L->data[j]>pivot)
j--;
L->data[i];
while(i<j&&L->data[j]<=pivot)
i++;
}
int main()
{
SqList *L;
ElemType a[10];
int i;
for(i=0;i<10;i++)
cin>>a[i];
InitList(L);
CreateList(L,a,10);
partition1(L);
DispList(L);
if(ListEmpty(L))
cout<<"this list is empty!"<<endl;
else
cout<<"this list is not empty!"<<endl;
cout<<"the length of list is "<<ListLength(L)<<endl;
ElemType e;
GetElem(L,3,e);
cout<<"the third of the list is "<<e<<endl;
cout<<"the location of '4' is "<<LocateElem(L,4)<<endl;
DispList(L);
e=3;
ListInsert(L,3,e);
DispList(L);
ListDelete(L,2,e);
DispList(L);
}
基本单链表
最新推荐文章于 2024-03-06 10:43:10 发布