데이터 구조 선형 표 순서 저장 의 설계 와 실현

4370 단어 데이터 구조
seqlist. h 파일:
#pragma once
#include "stdlib.h "
#include "stdio.h"
#include "memory.h"

typedef void SeqList;
typedef void SeqListNode;

//     
SeqList* SeqList_Create(int capacity);
//     
void SeqList_Destroy(SeqList* list);
//  
void SeqList_Clear(SeqList *list);
//           
int SeqList_Length(SeqList *list);
//
int SeqList_Capacity(SeqList *list);
//     pos        node
int SeqList_Insert(SeqList *list, SeqListNode *node,int pos);
//     pos     
SeqListNode* SeqList_Get(SeqList *list,int pos);
//  pos     ,          ,        NULL
SeqListNode* SeqList_Delete(SeqList *list,int pos);

seqlist. c 파일:
#include "seqlist.h"
/*
                ,                
                ,                
*/

typedef struct _tag_SeqList
{
	int length;//  
	int capacity;//    
	unsigned int *node;//        ,          int *node[]
}TSeqList;

SeqList* SeqList_Create(int capacity)
{
	TSeqList* tem = NULL;
	int rec = 0;
	tem = (TSeqList*)malloc(sizeof(TSeqList));//    ,                        ,       
	if (tem == NULL)
	{
		rec = -1;
		printf("SeqList_Create tem err:%d",rec);
		return NULL;
	}
	memset(tem, 0, sizeof(TSeqList));// tem      0

	//  capacity          
	tem->node = (unsigned int *)malloc(sizeof(unsigned int *) * capacity);
	if (tem->node == NULL)
	{
		rec = -2;
		printf("SeqList_Create tem->node err:%d", rec);
		return NULL;
	}
	tem->capacity = capacity;//        
	tem->length = 0;
	return tem;
}

void SeqList_Destroy(SeqList* list)
{
	int n = 0;
	if (list == NULL)
	{
		n = -1;
		printf("SeqList_Destroy err:%d", n);
		return;
	}
	TSeqList* tem = (TSeqList*)list;

	if (tem->node != NULL)
	{
		free(tem->node);
	}
	free(tem);
	return;
}

void SeqList_Clear(SeqList *list)
{
	int n = 0;
	if (list == NULL)
	{
		n = -1;
		printf("SeqList_Clear err:%d", n);
		return;
	}
	TSeqList* tem = (TSeqList*)list;
	tem->length = 0;
	return;
}
int SeqList_Length(SeqList *list)
{
	int n = 0;
	if (list == NULL)
	{
		n = -1;
		printf("SeqList_Length err:%d", n);
		return -1;
	}
	TSeqList* tem = (TSeqList*)list;
	return tem->length;
}
int SeqList_Capacity(SeqList *list)
{
	int n = 0;
	if (list == NULL)
	{
		n = -1;
		printf("SeqList_Capacity err:%d", n);
		return -1;
	}
	TSeqList* tem = (TSeqList*)list;
	return tem->capacity;
}

int SeqList_Insert(SeqList *list, SeqListNode *node, int pos)
{
	int i = 0,n = 0;
	if (list == NULL || node == NULL || pos < 0)
	{
		n = -1;
		printf("SeqList_Insert err:%d", n);
		return -1;
	}
	TSeqList* tem = (TSeqList*)list;

	//       
	if (tem->length >= tem->capacity)
	{
		n = -2;
		printf("SeqList_Insert err:%d", n);
		return -1;
	}

	//    ,       6,   10,   8       ,       7     
	if (pos > tem->length)
	{
		pos = tem->length;
	}

	for (i = tem->length;i>pos;i--)
	{
		tem->node[i] = tem->node[i-1];
	}
	tem->node[i] = (unsigned int)node;
	tem->length++;

	return 0;
}
SeqListNode* SeqList_Get(SeqList *list, int pos)
{
	SeqListNode* liNode=0;
	int n = 0;
	if (list == NULL)
	{
		n = 1;
		printf("SeqList_Get err:%d", n);
		return NULL;
	}
	TSeqList* tem = (TSeqList*)list;
	liNode = (void *)tem->node[pos];

	return liNode;
}

SeqListNode* SeqList_Delete(SeqList *list, int pos)
{
	int n = 0,i = 0;
	if (list == NULL || pos < 0)
	{
		n = -1;
		printf("SeqList_Delete err:%d", n);
		return NULL;
	}
	TSeqList* tem = (TSeqList*)list;
	for (i = pos;i < tem->length-1;i++)
	{
		tem->node[i] = tem->node[i+1];
	}
	tem->length--;
	return NULL;
}

main. c 파일
#include "seqlist.h"

typedef struct Teacher
{
	int age;
	char name[64];
}Teacher;

int main()
{
	SeqList *list = NULL;

	Teacher t1, t2, t3, t4;
	t1.age = 31;
	t2.age = 32;
	t3.age = 33;
	t4.age = 34;

	list = SeqList_Create(10);
	
	SeqList_Insert(list, (SeqList*)&t1, 0);
	SeqList_Insert(list, (SeqList*)&t2, 0);
	SeqList_Insert(list, (SeqList*)&t3, 0);
	SeqList_Insert(list, (SeqList*)&t4, 0);

	for (int i=0;iage);
	}

	while (SeqList_Length(list)>0)
	{
		SeqList_Delete(list, 0);
	}

	system("pause");
	return 0;
}

좋은 웹페이지 즐겨찾기