データ構造実験(一)——順序表と単鎖表の設計と実現


シーケンステーブルと単一チェーンテーブルの設計と実現
1.順序表
#include
#include
#define MaxSize 50
typedef char ElemType;
typedef struct
{
	ElemType data[MaxSize];
	int length;
}Sqlist;//     
void CreateList(Sqlist *&L,ElemType a[],int n)
{
	int i;
	L=(Sqlist *)malloc(sizeof(Sqlist));
	for(i=0;idata[i]=a[i];
	}
	L->length=n;
}//     
void InList(Sqlist *&L)
{
		L=(Sqlist *)malloc(sizeof(Sqlist));
		L->length=0;
}//      
bool ListInsert(Sqlist *&L,int i,ElemType e)
{
	int j;
	if(i<1 ||i>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;
}//    
void DispList(Sqlist *L)
{
	int i;
	for(i=0;ilength;i++)
		printf("%c",L->data[i]);
	printf("
"); }// int ListLength(Sqlist * L) { return (L->length); }// bool ListEmpty(Sqlist *L) { return(L->length==0); }// 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(ilength && L->data[i]!=e) i++; if(i>L->length) return 0; else printf("%d
", i+1); }// bool ListDelete(Sqlist * &L,int i,ElemType &e) { int j; if(i<1||i>L->length) return false; i--; e=L->data[i]; for(j=i;jlength-1;j++) L->data[j]=L->data[j+1]; L->length--; return true; }// void DestoryList (Sqlist *&L) { free(L); } int main() { Sqlist *L; InList(L);//1 ElemType e[]={'a','b','c','d','e'}; ElemType p; ElemType a1='a'; ElemType f1='f'; int mm=4; int m3=3; CreateList(L,e, 5); //2 DispList(L);//3 printf("%d
",ListLength(L));//4 ListEmpty(L);//5 GetElem(L,3,p);//6 printf("%c
",p); LocateElem(L,a1) ;//7 ListInsert(L,mm,f1);//8 DispList(L);//9 GetElem(L,m3,p);//10 DispList(L);//11 DestoryList (L);//12 return 0; }

2.単鎖表
#include
#include
typedef char ElemType;
typedef struct LNode
{
   ElemType data;
   struct LNode *next;
}LinkList;//     
void CreateListR(LinkList *&L,ElemType a[],int n)
{
	LinkList *s,*r;
	int i;
	L=(LinkList *)malloc (sizeof(LinkList));
	r=L;
    for(i=0;idata=a[i];
		r->next=s;
		r=s;
	}
	r->next=NULL;
}//     
void InitList(LinkList *&L)
{
	L=(LinkList *)malloc (sizeof(LinkList));
	L->next=NULL;

}//   
void DestoryList(LinkList *&L)
{
    LinkList *pre=L,*p=L->next;
	while (p!=NULL)
	{
		free(pre);
		pre=p;
		p=pre->next;
	}
	free(pre);
}//     
bool ListEmpty(LinkList *L)
{
	return(L->next==NULL);
}//         
int ListLength(LinkList *L) 
{
	int n=0;
	LinkList *p=L;
	while (p->next!=NULL)
	{
		n++;
		p=p->next;
	}
	return (n);
}//  
void DispList(LinkList *L)
{
	LinkList *p=L->next;
	while(p!=NULL)
	{
		printf("%c",p->data);
			p=p->next;
	}
	printf("
"); }// bool GetElem(LinkList *&L,ElemType &e,int i) { int j=0; LinkList *p=L; while(jnext; } if(p==NULL) return false; else { e=p->data; return true; } }// int LocateElem(LinkList *L,ElemType e) { int i=1; LinkList *p=L->next; while(p!=NULL && p->data!=e) { p=p->next; i++; } if(p==NULL) return(0); else printf("%d
",i); }// bool ListInsert(LinkList *&L,int i,ElemType e) { int j=0; LinkList *p=L,*s; while(jnext; } if(p==NULL) return false; else { s=(LinkList *)malloc (sizeof(LinkList)); s->data=e; s->next=p->next; p->next=s; return true; } }// bool ListDelete(LinkList *&L,int i,ElemType &e) { int j=0; LinkList *p=L,*q; while(jnext; } if(p==NULL) return false; else { q=p->next; if(q==NULL) return false; e=q->data; p->next=q->next; free(q); return true; }// } int main() { LinkList *h; InitList(h);//1 ElemType a[]={'a','b','c','d','e'}; ElemType e; ElemType m='f'; ElemType k='a'; CreateListR(h,a,5);//2 DispList(h);//3 printf("%d
",ListLength(h));//4 ListEmpty(h);//5 GetElem(h,e,3);//6 printf("%c
",e); LocateElem(h,k);//7 ListInsert(h,4,m);//8 DispList(h);//9 ListDelete(h,3,e);//10 DispList(h);//11 DestoryList(h);//12*/ return 0; }