线性表的顺序实现(C语言)

线性表的顺序实现(C语言)

IDE:VC 6++

 

欢迎各位感兴趣的同学共同讨论,学习。

 

/****************** 定义*******************/


#ifndef _Sequential_List_  

#define _Sequential_List_

 

#ifdef __cplusplus

extern "C" {

#endif

 

#define OK  (2)

#define ERROR (-1)

#define TRUE            (1)

#define FALSE          (0)

 

#define LIST_INIT_SIZE (100)

#define LISTINCERMENT (10)

 

typedef   int DataType;

 

typedef struct  _SeqList

{

DataType *elem;  /*存储空间基址*/

int length;      /*当前长度*/

int listsize;            /*当前分配的存储容量*/

 

}SeqList;

 

int InitList(SeqList *seq);

void DestroyList(SeqList *seq);

void ClearList(SeqList *seq);

int ListEmpty(SeqList *seq);

int ListLength(SeqList *seq);

void GetElem(SeqList *seq, int i, DataType *e);

void PriorElem(SeqList *seq, int cur_e, DataType *pre_e);

void NextElem(SeqList *seq, int cur_e, DataType *next_e);

int ListInsert(SeqList *seq, int i, DataType e);

int ListDelete(SeqList *seq, int i, DataType *e);

void ShowList(SeqList *seq);

 

#ifdef __cplusplus

}

#endif

 

#endif

 

/****************** 实现*******************/

 

#include "Sequential List.h"

#include <stdlib.h>

#include <string.h>

#include <stdio.h>

 

int InitList(SeqList *seq)

{

if (seq == NULL)

{

printf("seq == NULL/n");

return ERROR;

}

 

seq->elem = (DataType*)malloc(LIST_INIT_SIZE*sizeof(DataType));

if (seq->elem == NULL)

{

return ERROR;

}

 

memset(seq->elem, 0x00, LIST_INIT_SIZE*sizeof(DataType));

seq->length = 0;

seq->listsize = LIST_INIT_SIZE;

 

return OK;

}

 

void DestroyList(SeqList *seq)

{

if (seq == NULL)

{

printf("seq == NULL/n");

return;

}

 

if (seq->elem)

{

free(seq->elem);

seq->elem = NULL;

}

seq->length = 0;

seq->listsize = 0;

 

}

 

void ClearList(SeqList *seq)

{

if (seq == NULL)

{

printf("seq == NULL/n");

return;

}

 

if (seq->elem)

{

memset(seq->elem, 0x00, LIST_INIT_SIZE*sizeof(DataType));

}

seq->length = 0;

seq->listsize = LIST_INIT_SIZE*sizeof(DataType);

 

}

 

int ListEmpty(SeqList *seq)

{

if (seq == NULL)

{

printf("seq == NULL/n");

return ERROR;

}

 

if (seq->length == 0)

{

return TRUE;

}

else

{

return FALSE;

}

 

}

 

int ListLength(SeqList *seq)

{

if (seq == NULL)

{

return ERROR;

}

 

return seq->length;

}

 

void GetElem(SeqList *seq, int i, DataType *e)

{

if (seq == NULL || seq->elem == NULL || e == NULL)

{

return;

}

 

if (i < 1 || i > ListLength(seq))

{

return;

}

 

*e = seq->elem[i];

 

}

 

void PriorElem(SeqList *seq, int cur_e, DataType *pre_e)

{

if (seq == NULL || seq->elem == NULL || pre_e == NULL)

{

return;

}

 

if (cur_e <= 1 || cur_e > ListLength(seq))

{

return;

}

 

*pre_e = seq->elem[cur_e - 2];

 

}

void NextElem(SeqList *seq, int cur_e, DataType *next_e)

{

if (seq == NULL || seq->elem == NULL)

{

return;

}

 

if (cur_e < 1 || cur_e >= ListLength(seq))

{

return;

}

 

if (next_e == NULL)

{

return;

}

 

*next_e = seq->elem[cur_e ];

 

}

 

int ListInsert(SeqList *seq, int i, DataType e)

{

DataType *p, *q;

 

if (seq == NULL || seq->elem == NULL)

{

return ERROR;

}

 

if (i < 1 || i > ListLength(seq) + 1)

{

return ERROR;

}

 

if (seq->length >= seq->listsize)

{

DataType* newbase = NULL;

 

newbase = (DataType*)realloc(seq->elem, (LIST_INIT_SIZE + LISTINCERMENT)*sizeof(DataType));

if (newbase == NULL)

{

return ERROR;

}

seq->elem = newbase;

seq->listsize += LISTINCERMENT;

}

 

q = &seq->elem[i - 1];

p = &seq->elem[seq->length - 1];

 

for (; p >= q; --p)

{

*(p+1) = *p;

}

 

*q = e;

++seq->length;

 

return OK;

 

}

 

int ListDelete(SeqList *seq, int i, DataType *e)

{

DataType *p, *q;

 

if (seq == NULL || seq->elem == NULL)

{

return ERROR;

}

 

if (i < 1 || i > ListLength(seq))

{

return ERROR;

}

 

q = &seq->elem[i - 1];

*e = *q;

p = &seq->elem[seq->length - 1];

 

for (++q; q <= p; ++q)

{

*(q-1) = *q;

}

 

--seq->length;

 

return OK;

 

}

 

void ShowList(SeqList *seq)

{

if (seq == NULL)

{

printf("seq == NULL/n");

return;

}

printf("*********List start **********/n");

for (int i = 0; i < ListLength(seq); i++)

{

printf("number %d is %d/n", i+1, seq->elem[i]);

}

printf("*********List end **********/n");

 

return;

}

 

/****************** 测试*******************/

/**********为了方便,用c++写的测试代码**********/

#include "Sequential List.h"

#include <iostream>

using namespace std;

 

int main(int argc, char* argv[])

{

SeqList *seq = NULL;

seq = new SeqList;

 

InitList(seq);

 

ListInsert(seq, 1, 111);

ListInsert(seq, 1, 222);

ListInsert(seq, 1, 333);

cout<<"List Length = "<<ListLength(seq)<<endl;

if (ListEmpty(seq) == FALSE)

{

DataType e;

ListDelete(seq, 1, &e);

}

ShowList(seq);

cout<<"List Length = "<<ListLength(seq)<<endl;

 

DataType pre_e;

PriorElem(seq, 2, &pre_e);

cout<<"PriorElem = "<<pre_e<<endl;

 

DataType next_e;

NextElem(seq, 1, &next_e);

cout<<"NextElem = "<<next_e<<endl;

 

DataType ee;

GetElem(seq, 1, &ee);

cout<<"GetElem(seq, 1, &ee) = "<<ee<<endl<<endl<<endl<<endl;

 

delete seq;

seq = NULL;

 

return 0;

}

 

 

 

 

你可能感兴趣的:(c,list,测试,null,存储,语言)