資料結構:順序結構和鏈式結構的資料型別定義

mf1crystal發表於2017-12-04

順序結構:

標頭檔案宣告:sequenclist.h

#ifndef _SEQUENCE_H_
#define _SEQUENCE_H_


#define MAXSIZE    10
#define FAILURE    1000000
#define SUCCESS    1000001
#define TRUE       1000002
#define FALSE      1000003


typedef int ElemType;


struct sequence
{
ElemType *data;
int size;
int length;
};
typedef struct sequence SeList;


int ListInit(SeList *s);
int ListInsert(SeList *s,int i,ElemType e);
int ListLength(SeList s);
int EmptyList(SeList s);
int GetElem(SeList s,int p,ElemType *e);
int ListTraverse(SeList s,void(*look)(ElemType));
int LocateElem(SeList s,ElemType e,int(*compare)(ElemType,ElemType));
int DeleteList(SeList *s, int pos);


#endif


sequencelist.c

#include<stdio.h>
#include "sequencelist.h"
#include <stdlib.h>


int ListInit(SeList *s)
{
s->data = (ElemType *)malloc(sizeof(ElemType)*MAXSIZE);
if(NULL == s->data)
{
return FAILURE;
}


s->size = MAXSIZE;
s->length = 0;


return SUCCESS;
}


int ListInsert(SeList *s,int i,ElemType e)
{
int k;


if( i<0 || i >=MAXSIZE)
{
return FAILURE;
}
if(s->length >=MAXSIZE)
{
return FAILURE;
}


for(k = 0; k < s->length-i; k++)
{
s->data[s->length-k]=s->data[s->length-k-1];


}
s->data[i] = e;
s->length++;


return SUCCESS;
}


int ListLength(SeList s)
{
return s.length;
}


int EmptyList(SeList s)
{
return (s.length == 0) ? TRUE : FALSE;
}


int GetElem(SeList s,int p,ElemType *e)
{
if (p<0||p>=MAXSIZE)
{
return FAILURE;
}
*e=s.data[p];
return SUCCESS;


}


int ListTraverse(SeList s,void(*look)(ElemType))
{
int i;
for(i=0;i<s.length;i++)
{
look(s.data[i]);
}
return SUCCESS;
}


int LocateElem(SeList s,ElemType e,int(*compare)(ElemType,ElemType))
{
int i;


for(i=0;i<s.length;i++)

{
if(compare(e,s.data[i]) == TRUE)
return i;
}


return FAILURE;
}


int DeleteList(SeList *s, int pos)
{
int i;
if(pos < 0 || pos >10)
{
return FALSE;
}
else
{
for(i = pos;i < s->length; i++)
{
s->data[i] = s->data[i+1];
}
s->length--;
return SUCCESS;
}


}



main.c

#include <stdio.h>
#include "sequencelist.h"


void visit(ElemType e)
{
printf(" %d",e);
}


int equal(ElemType a,ElemType b)
{
return (a==b) ? TRUE :FALSE;
}


int large(ElemType a,ElemType b)
{
return (a<=b) ? TRUE :FALSE;
}


int main()
{
int ret,i,j;
SeList SList;
int place,pos,t;
ElemType e;


ret = ListInit(&SList);
if(SUCCESS == ret)
{
printf("Init Success!\n");


}
else
{
printf("Init Failure!\n");
}

for(i=0;i<6;i++)
{
ret= ListInsert(&SList,i,i+1);
if(SUCCESS ==ret)
{
printf("Insert %d Success!\n");


}
else
{
printf("Insert %d Failure!\n");
}


}


ret = EmptyList(SList);
if(TRUE==ret)
{
printf("List is Empty!\n");
}
else if(FALSE == ret)
{
printf("List is Not Empty!\n");
}
printf("Length :%d\n",ListLength(SList));


place=3;
ret = GetElem(SList,place,&e);
if(SUCCESS == ret)
{
printf("%d element is %d\n",place,e);
}
else
{
printf("Get %d Place Failure!\n",place);
}


ret = ListTraverse(SList,visit);
printf("\n");
if(SUCCESS == ret)
{
printf("Traverse Success!\n");
}
else
{
printf("Traverse Failure!\n");
}


e=4;
ret = LocateElem(SList,e,equal);
if(FAILURE == ret)
{
printf("Not Fount!\n");
}
else
{
printf("%d is %dth element!\n",e,ret);
}
int a[10]={0};

printf("Please input a number:\n");
for(i = 0;i < 10; i++)
{
scanf("%d",&a[i]);
}
ret = ListInit(&SList);
for(j = 0;j < 10;j++)
{
     ret = LocateElem(SList,a[j],large);
if(ret == FAILURE)
{
  SList.data[SList.length]=a[j];
SList.length++;
}
else
{
ListInsert(&SList,ret,a[j]);
}
}

for(i = 0;i < 10; i++)
{
printf("%d ",SList.data[i]);
}
printf("\n");


printf("Please input the position:\n");
scanf("%d",&pos);
ret = DeleteList(&SList,pos);
j = 0;
if(ret == FALSE)
{
printf("UNKOWN!\n");
}
while(SList.length != 0)
{
printf("%d ",SList.data[j]);
SList.length--;
j++;
}
printf("\n");





return 0;


鏈式結構:

LinkList.h

#ifndef _LINKLIST_H_
#define _LINKLIST_H_


#define SUCCESS 1000001
#define FAILURE 1000002
#define TRUE    1000003
#define FALSE   1000004


typedef int ElemType;


struct node
{
ElemType data;
struct node *next;
};
typedef struct node Node;
typedef Node *LinkList;


int ListInit(LinkList *L);
int ListInsert(LinkList *L,int i, ElemType e);
int ListTraverse(LinkList L, void(*print)(ElemType));
int GetElem(LinkList L,int k,ElemType *e);
int ListLength(LinkList L);
int ListEmpty(LinkList L);
int ListDelete(LinkList L,int i,ElemType *e);
int ListClean(LinkList L);
int ListDestroy(LinkList L);


#endif


LinkList.c

#include <stdio.h>
#include "LinkList.h"
#include <stdlib.h>


int ListInit(LinkList *L)
{
(*L) = (LinkList)malloc(sizeof(Node));
if(NULL == *L)
{
return FAILURE;
}
(*L)->next = NULL;
return SUCCESS;
}


int ListInsert(LinkList *L,int i,ElemType e)
{
LinkList p = *L;
int j = 1;


while(p != NULL && j < i)
{
p = p->next;
j++;
}


if(p == NULL || j > i)
{
return FAILURE;
}

LinkList n =(LinkList)malloc(sizeof(Node));
if(NULL == n)
{
return FAILURE;
}
n->data = e;
n->next = p->next;
p->next = n;


return SUCCESS;
}


int ListTraverse(LinkList L, void(*print)(ElemType))
{
LinkList p = L->next;


while(p)
{
print(p->data);
p = p->next;
}


return SUCCESS;
}


int GetElem(LinkList L,int k,ElemType *e)
{
int length = 0;
LinkList p = L->next;


while(p)
{
length++;
if(k == length)
{
break;
}
p = p->next;
}


if(k > length)
{
return FAILURE;
}
*e = p->data;
return SUCCESS;
}


int ListLength(LinkList L)
{
int length = 0;
LinkList p = L->next;


while(p)
{
length++;
p = p->next;
}

return length;
}


int ListEmpty(LinkList L)
{
return(L->next == NULL) ? TRUE : FALSE;
}


int ListDelete(LinkList L, int i, ElemType *e)
{
LinkList p = L;
int j = 1;
LinkList tmp;


while(p != NULL && j < i)
{
p = p->next;
j++;
}


if(p == NULL || j > i)
{
return FAILURE;
}


tmp = p->next;
p->next = tmp->next;
free(tmp);


return SUCCESS;
}


int ListClean(LinkList L)
{
LinkList p = L->next;


if(!p)
{
return SUCCESS;
}


while(p)
{
L->next = p->next;
free(p);
p = L->next;
}


return SUCCESS;
}


int ListDestroy(LinkList L)
{
free(L);
L = NULL;


return SUCCESS;
}


main.c

include <stdio.h>
#include "LinkList.h"
void visit(ElemType e)
{
printf("%d ",e);
}


int main()
{
int ret, i;
int place = 0;
LinkList list;
    ElemType e;


ret = ListInit(&list);
if(FAILURE == ret)
{
printf("Init Failure!\n");
}
else 
{
printf("Init Success!\n");
}


for(i = 0; i < 10; i++)
{
ret = ListInsert(&list,i+1,i+1);
if(FAILURE == ret)
{
printf("Insert %d Failure!\n",i+1);
}
else
{
printf("Insert %d Success!\n",i+1);
}
}


ret = ListTraverse(list,visit);
if(FAILURE == ret)
{
printf("Traverse Failure!\n");
}
else
{
printf("Traverse Success!\n");
}


place = 3;
ret = GetElem(list,place,&e);
if(FAILURE == ret)
{
printf("Get %dth Failure!\n",place);
}
else
{
printf("%dth Element is %d\n",place,e);
}


printf("length is %d\n",ListLength(list));


ret = ListEmpty(list);
if(TRUE == ret)
{
printf("List is Empty!\n");
}
else
{
printf("List is not Empty!\n");
}


place = 3;
ret = ListDelete(list ,place,&e);
if(SUCCESS == ret )
{
printf("Delete %d Success !\n",e);
}
else
{
printf("Delete Failure!\n");
}
ret = ListTraverse(list,visit);
if(FAILURE == ret)
{
printf("Traverse Failure!\n");
}
else
{
printf("Traverse Success!\n");
}


ret = ListClean(list);
if(SUCCESS == ret)
{
printf("Clean Success!\n");
}
else
{
printf("Clean Failure!\n");
}
printf("length is %d\n",ListLength(list));


ret = ListDestroy(list);
if(SUCCESS == ret)
{
printf("Destroy Success!\n");
}


return 0;
}


相關文章