数据结构:线性表的顺序存储

本文内容为笔者学习  传智播客  的免费公开课时整理所得,下文为线性表的链式存储代码数据结构:线性表的链式存储 里面老师用的编译环境是vc6.0,我使用的是vs2012,感觉是差不多的,甚至还简单了点。传智播客的老师讲解的非常简单,逻辑清晰,代码也是给一行行打出来的!非常感谢传智播客!下面是我整理的代码,直接上干货

1  main.cpp文件

#include 
#include 
#include 
#include "seqlist.h"

typedef struct Teacher
{ 
	int age;
	char name[64];
}Teacher;
void main()
{
	int ret=0;
	int i=0;
	SeqList* list=NULL;
	Teacher t1,t2,t3,t4,t5;
	t1.age = 31;
	t2.age = 32;
	t3.age = 33;
	t4.age = 34;
	t5.age = 35;

	list=SeqList_Create(10);
	if (list==NULL)
	{
		printf("func SeqList_Create() ret :%d \n ",ret);
		return;
	}
	ret = SeqList_Insert( list,(SeqListNode* )&t1, 0);//头插法
	ret = SeqList_Insert( list,(SeqListNode* )&t2, 0);//头插法
	ret = SeqList_Insert( list,(SeqListNode* )&t3, 0);//头插法
	ret = SeqList_Insert( list,(SeqListNode* )&t4, 0);//头插法
	ret = SeqList_Insert( list,(SeqListNode* )&t5, 0);//头插法

	//遍历
	for(i=0;iage:%d \n",tmp->age);
	}

	//删除链表中的节点
	while(SeqList_Length(list)>0)
	{
		SeqList_Delete(list ,0);
	}

	printf("hello...\n");
	system("pause");
	return ;
}

2  seqlist.h 头文件

#ifndef _HV_SEQLIST_H_
#define _HV_SEQLIST_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);

int SeqList_Insert(SeqList* list,SeqListNode* node, int pos);

SeqListNode* SeqList_Get(SeqList* list,int pos);

SeqListNode* SeqList_Delete(SeqList* list,int pos);

#endif

3 seqlist.cpp  链表的增删改查实现函数

#include 
#include 
#include 
#include "seqlist.h"


//在结构体中套一级指针
//
typedef struct _tag_SeqList
{
	int length;
	int capacity;
	unsigned int *node;
}TSeqList;

SeqList* SeqList_Create(int capacity)
{
	int ret =0;
	TSeqList* tmp =NULL;

	tmp = (TSeqList*)malloc(sizeof(TSeqList));
	if (tmp ==NULL)
	{
		ret =-1;
		printf("func SeqList_Create() err :%d\n ",ret);
		return NULL;
	}
	memset(tmp,0,sizeof(TSeqList));

	tmp->node  = (unsigned int *)malloc(sizeof(unsigned int *)*capacity); 
	if (tmp->node ==NULL)
	{
		ret =-2;
		printf("func SeqList_Create()  malloc err :%d\n ",ret);
		return NULL;
	}
	tmp->capacity = capacity;
	tmp->length = 0;
	
	return tmp;
}

void SeqList_Destroy(SeqList* list)
{
	TSeqList *tlist = NULL;
	if (list == NULL)
	{
		return ;
	}
	tlist = (TSeqList* )list ;
	if (tlist->node != NULL)
	{
		free (tlist->node);
	}

	free(tlist);
	return ;
}

//清空链表
void SeqList_Clear(SeqList* list)
{
	TSeqList *tlist = NULL;
	if (list == NULL)
	{
		return ;
	}
	tlist = (TSeqList* )list ;
	tlist->length = 0;
	return ;
}


int SeqList_Length(SeqList* list)
{
	TSeqList *tlist = NULL;
	if (list == NULL)
	{
		return -1;
	}
	tlist = (TSeqList* )list ;
	return tlist->length;
}


int SeqList_Capacity(SeqList* list)
{   
	TSeqList *tlist = NULL;
	if (list == NULL)
	{
		return -1;
	}
	tlist = (TSeqList* )list ;
	return tlist->capacity;
}


int SeqList_Insert(SeqList* list,SeqListNode* node, int pos)
{
	int i =0;
	int ret = 0;
	TSeqList *tlist = NULL;
	if (list == NULL)
	{
		return -1;
	}
	

	tlist = (TSeqList* )list ;
	//判断是不是满了
	if (tlist->length >= tlist->capacity)
	{
		ret = -1;
		printf("func SeqList_Insert() Tlist->length >= Tlist->capacity err :%d \n ",ret);
		return ret;
	}

	//容错修正
	if (pos>=tlist->length)
	{
		pos = tlist->length;
	}

	//1 元素后移
	for (i=tlist->length;i>pos;i--)
	{
		tlist->node[i] = tlist->node[i-1];
	}


	//2 插入元素
	tlist->node[i] = (unsigned int )node;
	tlist->length ++;

	return 0;
}


SeqListNode* SeqList_Get(SeqList* list,int pos)
{
	int i =0;
	int  ret = 0;
	TSeqList *tlist = NULL;
	if (list == NULL || pos<0)
	{
		ret = - 1;
		printf("func SeqList_Get() err:%d \n ",ret);
		return NULL;
	}
	

	 tlist = (TSeqList* )list ;
     return  (SeqListNode* )tlist->node[pos];
	
}


SeqListNode* SeqList_Delete(SeqList* list,int pos)
{
	int i =0;
	SeqListNode* ret = 0;
	TSeqList *tlist = NULL;
	if (list == NULL || pos<0)
	{
		printf("func SeqList_Get() err:%d \n ",ret);
		return NULL;
	}
	 tlist = (TSeqList* )list ;

	 //第一步:缓存删除位置的节点
	 ret = (SeqListNode*)tlist->node[pos];
	 //第二步:删除节点后的节点前移
	 for(i=pos+1;ilength;i++)
	 {
		 tlist->node[i-1] =tlist->node[i];
	 }
	 tlist->length--;
	
	return NULL;
}






你可能感兴趣的:(数据结构)