#ifndef HEAD_H__
#define HEAD_H__
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int status;
typedef int boolean;
typedef int ElementType;
#define MAX 100
void view(ElementType *);
status compare(ElementType ,ElementType);
void buildSet(ElementType a[],int length,int beg,int end);
void printSet(ElementType a[],int length);
void swap(ElementType *e1,ElementType *e2);
void quickSort(ElementType a[],int p,int r);
void randomizedQuickSort(ElementType a[],int p,int r);
#endif
#ifndef STATICLINKLIST_H__
#define STATICLINKLIST_H__
#include"head.h"
typedef struct{
ElementType elem;
int cursor;
}component,sLinkList[MAX];
void initSpace(sLinkList L);
int initSLinkList(sLinkList L);
status clearSLinkList(sLinkList L,int n);
status empty(sLinkList L,int n);
int length(sLinkList L,int n);
status getElement(sLinkList L,int n,int i,ElementType *e);
int locateElement(sLinkList L,int n,ElementType e);
status priorElement(sLinkList L,int n,ElementType cur_e,ElementType *pre_e);
status nextElement(sLinkList L,int n,ElementType cur_e,ElementType *next_e);
status myInsert(sLinkList L,int n,int i,ElementType e);
status myDelete(sLinkList L,int n,int i,ElementType *e);
status myTraverse(sLinkList L,int n,void(*view)(ElementType *));
#endif
#include"head.h"
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void view(ElementType *e)
{
printf("%d\t",*e);
}
status compare(ElementType e1,ElementType e2)
{
if(e1==e2)
return TRUE;
else
return FALSE;
}
void buildSet(ElementType a[],int length,int beg,int end)
{
int i;
srand((unsigned)time(NULL));
for(i=0;i<length;i++)
a[i]=rand()%(end-beg+1)+beg;
}
void printSet(ElementType a[],int length)
{
int i,count=1;
for(i=0;i<length;i++,count++)
{
printf("%d\t",a[i]);
if(count%10==0)
printf("\n");
}
}
void swap(ElementType *e1,ElementType *e2)
{
ElementType tmp;
tmp=*e1;
*e1=*e2;
*e2=tmp;
}
#include<stdlib.h>
#include<time.h>
#include"head.h"
/*###########需要的头文件,定义一个algorithm.h即可#####################
#define MAX 1000
typedef int ElementType;
void swap(ElementType *x,ElementType *y);
void quickSort(ElementType a[],int p,int r);
void randmizedQuickSort(ElementType a[],int p,int r);#################*/
/*#####################这是普通版本的快速排序#######################*/
static int partition(ElementType a[],int p,int r)
{
int i,j,x;
x=a[r];
i=p-1;
for(j=p;j<r;j++)
{
if(a[j]<=x)
{
i++;
swap(&a[i],&a[j]);
}
}
swap(&a[r],&a[i+1]);
return i+1;
}
void quickSort(ElementType a[],int p,int r)
{
int q;
if(p<r)
{
q=partition(a,p,r);
quickSort(a,p,q-1);
quickSort(a,q+1,r);
}
}
/*##############################随机化版本的快速排序##########################*/
static int randomizedPartition(ElementType a[],int p,int r)
{
int i;
srand((unsigned int)time(NULL));
i=rand()%(r-p+1)+p;//生成一个区间为(p,r)的随机数的方法
swap(&a[i],&a[r]);
return partition(a,p,r);
}
void randomizedQuickSort(ElementType a[],int p,int r)
{
int q;
if(p<r)
{
q=randomizedPartition(a,p,r);
randomizedQuickSort(a,p,q-1);
randomizedQuickSort(a,q+1,r);
}
}
#include"staticLinkList.h"
static int myMalloc(sLinkList space)
{
int i=space[0].cursor;
if(i)
space[0].cursor=space[i].cursor;//从备用静态链表中拿走一个空间,并把备用链表修改
return i;//i就是从备用空间中拿出来的一个空间指针。
}
static void myFree(sLinkList space,int k)//将下标为K的结点回收回来。
{
space[k].cursor=space[0].cursor;//类似于在首个元素之前插入结点
space[0].cursor=k;
}
void initSpace(sLinkList L)//开辟一个空间L[0]是头结点,大小是MAX-1个元素.
{
int i;
for(i=0;i<MAX-1;i++)
L[i].cursor=i+1;
L[MAX-1].cursor=0;
}
int initSLinkList(sLinkList L)
{
int i;
i=myMalloc(L);
L[i].cursor=0;
return i;
}
status clearSLinkList(sLinkList L,int n)
{
int j,k,i=L[n].cursor;
L[n].cursor=0;
k=L[0].cursor;
L[0].cursor=i;
while(i)
{
j=i;
i=L[i].cursor;
}
L[j].cursor=k;
return OK;
}
status empty(sLinkList L,int n)
{
return (L[n].cursor==0)?TRUE:FALSE;
}
int length(sLinkList L,int n)
{
int j=0,i=L[n].cursor;
while(i)
{
i=L[i].cursor;
j++;
}
return j;
}
status getElement(sLinkList L,int n,int i,ElementType *e)
{
int l,k=n;
if(i<1 || i>length(L,n))
return ERROR;
for(l=1;l<=i;l++)
k=L[k].cursor;
*e=L[k].elem;
return OK;
}
int locateElement(sLinkList L,int n,ElementType e)
{
int k=L[n].cursor;
while(k && L[k].elem!=e)
k=L[k].cursor;
return k;
}
status priorElement(sLinkList L,int n,ElementType cur_e,ElementType *pre_e)
{
int k=L[n].cursor;
while(L[k].cursor && L[L[k].cursor].elem!=cur_e)
{
k=L[k].cursor;
}
if(k)
{
*pre_e=L[k].elem;
return OK;
}
else
return ERROR;
}
status nextElement(sLinkList L,int n,ElementType cur_e,ElementType *next_e)
{
int k=L[n].cursor;
while(L[k].cursor && L[k].elem!=cur_e)
k=L[k].cursor;
if(L[k].cursor && L[k].elem == cur_e)
{
*next_e=L[L[k].cursor].elem;
return OK;
}
else
return ERROR;
}
status myInsert(sLinkList L,int n,int i,ElementType e)
{
int l,j,k=n;
if(i<1 || i>length(L,n)+1)
return ERROR;
j=myMalloc(L);
if(j)
{
L[j].elem=e;
for(l=1;l<i;l++)
k=L[k].cursor;
L[j].cursor=L[k].cursor;
L[k].cursor=j;
return OK;
}
return ERROR;
}
status myDelete(sLinkList L,int n,int i,ElementType *e)
{
int j,k=n;
if(i<1 || i>length(L,n))
return ERROR;
for(j=1;j<i;j++)
k=L[k].cursor;
j=L[k].cursor;
L[k].cursor=L[j].cursor;
*e=L[j].elem;
myFree(L,j);
return OK;
}
status myTraverse(sLinkList L,int n,void(*view)(ElementType *))
{
int k=L[n].cursor;
while(k)
{
view(&L[k].elem);
k=L[k].cursor;
}
return OK;
}
#include<stdio.h>
#include"head.h"
#include"staticLinkList.h"
/*
void initSpace(sLinkList L);
int initSLinkList(sLinkList L);
status clearSLinkList(sLinkList L,int n);
status empty(sLinkList L,int n);
int length(sLinkList L,int n);
status getElement(sLinkList L,int n,int i,ElementType *e);
int locateElement(sLinkList L,int n,ElementType e);
status priorElement(sLinkList L,int n,ElementType cur_e,ElementType *pre_e);
status nextElement(sLinkList L,int n,ElementType cur_e,ElementType *next_e);
status myInsert(sLinkList L,int n,int i,ElementType e);
status myDelete(sLinkList L,int n,int i,ElementType *e);
status myTraverse(sLinkList L,int n,void(*view)(ElementType *));
*/
int main()
{
sLinkList L;
ElementType a[MAX],e,pre_e,next_e;
int i,first,index;
initSpace(L);
printf("the space initiallize of staticLinkList adrees is:%d\n",L);
first=initSLinkList(L);
printf("the adress of staticLinkList is %x , the first cursor is %d\n",L,first);
if(empty(L,first))
printf("the list is empty\n");
printf("the length of the list is %d\n",length(L,first));
printf("display the random arraya\n");
buildSet(a,MAX,0,100);
printSet(a,MAX);
printf("insert the array into the list\n");
for(i=0;i<MAX;i++)
myInsert(L,first,i,a[i]);
myTraverse(L,first,view);
printf("\n");
if(getElement(L,1,1,&e))
printf("the 1's element is : %d\n",e);
index=locateElement(L,first,55);
printf("the 55's index is : %d, and the element is: %d\n",index,L[index].elem);
if(priorElement(L,first,55,&pre_e))
printf("the prior element of 55 is : %d\n",pre_e);
if(nextElement(L,first,55,&next_e))
printf("the next element of 55 is : %d\n",next_e);
printf("the following elements are delete\n");
for(i=0;i<20;i++)
{
myDelete(L,first,20-i,&e);
printf("%d\t",e);
}
printf("the static link list now is :\n");
myTraverse(L,first,view);
putchar('\n');
printf("clear the link list\n");
clearSLinkList(L,first);
printf("after clear the list the length is : %d\n",length(L,first));
return 0;
}