数据结构——线性表 顺序存储(1)

测试框架

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"SeqList.h"
using namespace std;

struct  Teacher
{
	int age;
	char name[64];	 
};
void main()
{
	int ret = 0;
	int i = 0;
	Seqlist* list = NULL;
	Teacher t1, t2, t3, t4, t5, t6;
	t1.age = 21;
	strcpy(t1.name,"li1");
	t2.age = 22;
	strcpy(t2.name, "li2");
	t3.age = 23;
	strcpy(t3.name, "li3");
	t4.age = 24;
	strcpy(t4.name, "li4");
	t5.age = 25;
	strcpy(t5.name, "li5");
	list = List_Create(10);
	if (list==NULL)
	{
		return;
	}
	List_Inster(list,&t1,0);//头插法
	List_Inster(list, &t2, 0);
	List_Inster(list, &t3, 0);
	List_Inster(list, &t4, 0);
	List_Inster(list, &t5, 0);
	for ( i = 0; i < List_Length(list); i++)
	{
		Teacher* tmp =(Teacher*)List_Get(list,i);
		if (tmp==NULL)
		{
			return;
		}
		printf("tmp->age : %d\n", tmp->age);
		printf("tmp->name : %s\n", tmp->name);
	}
	while (List_Length(list)>0)
	{
		List_Delete(list,0);
	}
	system("pause");
}

编写的头文件

#pragma once
#ifdef _MYSEQLIST_H_
#define _MYSEQLIST_H_
#endif // _MYSEQLIST_H_
//线性链表的顺序存储
//void指针表示未知类型的指针,可以将任意类型的指针直接赋值给void指针,好比是C#、Java中的object引用,可以把任意 类型的对象赋值给它。但把void类型赋值给特定类型的指针时,就需要进行强制转换,因为C++为类型语言,尽可能保证类型安全,如果使用了强制类型转 换,编译器就认为程序员知道他(她)在干什么,程序也应该负起这样做的责任。
typedef void Seqlist;
typedef void SeqListNode;

Seqlist * List_Create(int capacity);
void List_Destory(Seqlist* list);
void List_Clear(Seqlist* list);
int List_Length(Seqlist* list);
int List_Capacity(Seqlist* list);
SeqListNode* List_Get(Seqlist* list, int pos);
int List_Inster(Seqlist* list, SeqListNode* node, int pos);
int List_Delete(Seqlist* list,int pos);

对头文件的实现

#include"SeqList.h"
#include<stdio.h>
#include<stdlib.h>
using namespace std;
struct  TSeqList
{
	int length;
	int capacity;
	unsigned int **node;
};
/*创建链表并为链表分配空间*/
Seqlist* List_Create(int capacity)
{
	TSeqList* tmp = NULL;
	tmp = (TSeqList*)malloc(sizeof(TSeqList));
	if (tmp==NULL)
	{
		return NULL;
	}
	tmp->node =(unsigned int**)malloc(sizeof(unsigned int*)*capacity);
	tmp->length = 0;
	tmp->capacity = capacity;
	return tmp;
}
/*销毁链表并释放链表的内存空间*/
void List_Destory(Seqlist* list)
{
	TSeqList* tlist = NULL;
	if (list==NULL)
	{
		printf("List_Destory() err:");
		return;
	}
	tlist = (TSeqList*)list;
	if (tlist->node!=NULL)
	{
		free(tlist->node);
	}
	if (tlist->length!=0)
	{
		free(tlist);
	}
	return ;
}
/*清空链表,回到初始链表的状态*/
void List_Clear(Seqlist* list)
{
	TSeqList* tlist = NULL;
	if (list==NULL)
	{
		printf("List_Clear() err:");
		return;
	}
	tlist = (TSeqList*)list;
	tlist->length = 0;
	return ;
}
/*链表长度大小*/
int List_Length(Seqlist* list)
{
	TSeqList* tlist = NULL;
	if (list==NULL)
	{
		printf("List_Length() err:");
		return -1;
	}
	tlist = (TSeqList*)list;
	return tlist->length;
}
/*链表大小获取*/
int List_Capacity(Seqlist* list)
{
	TSeqList* tlist = NULL;
	if (list==NULL)
	{
		printf("List_Capacity() err:");
		return -1;
	}
	tlist = (TSeqList*)list;

	return tlist->capacity;
}
/*元素获取*/
SeqListNode* List_Get(Seqlist* list, int pos)
{
	TSeqList* tlist = NULL;
	if (list == NULL||pos<0)
	{
		printf("List_Get() err:");
		return NULL;
	}
	tlist = (TSeqList*)list;
	
	return tlist->node[pos];
}
/*元素插入*/
int List_Inster(Seqlist* list, SeqListNode* node, int pos)
{
	TSeqList* tlist = NULL;
	if (list == NULL || pos<0||node==NULL)
	{
		printf("List_Inster() err:");
		return -1;
	}
	int i = 0;
	SeqListNode* ret = NULL;
	tlist = (TSeqList*)list;
	//判断是不是链表中已经满了
	if (tlist->length==tlist->capacity)
	{
		printf("List_Inster() err: yiman");
		return -2;
	}
	//容错修正,比如 6歌长度 容量为20,用户在10位置插入,则进行容错
	if (pos>=tlist->length)
	{
		pos = tlist->length;
	}
	//数据元素后移  头插法
	for ( i = tlist->length; i > pos; i--)
	{
		tlist->node[i] = tlist->node[i-1];
	}
    //插入元素
	tlist->node[i] = (unsigned int *)node;
	tlist->length++;
	return 0;
}
/*元素删除*/
int List_Delete(Seqlist* list, int pos)
{
	TSeqList* tlist = NULL;
	if (list==NULL||pos<0)
	{
		printf("List_Delete() err: ");
		return -1;
	}
	tlist = (TSeqList*)list;
	SeqListNode* ret = NULL;
	ret = tlist->node[pos];
	int i = 0;
	for ( i =pos+1; i <tlist->length; i++)
	{
		tlist->node[i - 1] = tlist->node[i];
	}
	tlist->length--;
	return NULL;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章