【数据结构】线性表之顺序存储结构

代码如下:

公共头文件common.h

#ifndef __HI_COMM_H__
#define __HI_COMM_H__


#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <string>

#define LIST_INIT_SIZE 100  /*线性表存储空间的初始分配量;*/
#define LIST_INCREMENT 10   /*线性表存储空间的分配增量;*/
#define ElemType int
//#define NULL (void*)0

/*线性表的顺序存储结构*/
typedef struct{
	ElemType *elem;  //线性表的基地址
	int length;      //当前长度
	int MaxSize;    //当前分配的存储容量
}SqList;

/*线性表的链式存储结构*/
typedef struct frankLNode
{
	ElemType data;
	struct frankLNode* next; 
}LNode;   //


/*栈的顺序存储结构*/
typedef struct 
{
	ElemType* base;
	ElemType* top;
	int StackSize;
}FStack;
#define STACK_INIT_SIZE 100  /*栈存储空间的初始分配量;*/
#define STACK_INCREMENT 10   /*栈存储空间的分配增量;*/


/*队列的顺序存储结构*/
typedef struct frankQNode
{
	ElemType data;
	struct frankQNode *next;
}QNode;

typedef struct frankQueueHeader
{
	QNode* front;
	QNode* rear;
}QueueHeader;

/*二叉树的存储结构*/

typedef struct frankBinTreeNode
{
	ElemType data;
	struct frankBinTreeNode *left;
	struct frankBinTreeNode *right;
}BinTreeNode;
typedef BinTreeNode* pBinTreeNode;


#endif
数据结构算法的实现

头文件LinearList.h

#pragma once
#include "common.h"

class LinearList
{
public:
	LinearList(void);
	~LinearList(void);



	int InitList(SqList *L);

	int DestroyList(SqList *L);

	int ClearList(SqList *L);

	bool isListEmpty(SqList *L);

	int getListLength(SqList *L);

	//给定一个序号,去查找这个序号对应的元素值;
	ElemType getElem(SqList *L,int index);

	//从列表中找到某一元素的位置
	int locateElem(SqList *L,ElemType Q);

	ElemType PriorElem(SqList *L,ElemType Cur_e);

	ElemType NextElem(SqList *L,ElemType Cur_e);

	int AgainMalloc(SqList *L);

	SqList* ListInsert(SqList *L,int index ,ElemType e);

	void ListDelete(SqList *L,int index ,ElemType &e);

	SqList* ChangeElem(SqList *L,int index ,ElemType e);

	void PrintSqList(SqList *L);

	SqList* ListAdd(SqList *L, ElemType e);

	void MergeList(SqList La, SqList Lb ,SqList &Lc);
};
数据结构算法实现:

NodeList.cpp

#include "LinearList.h"

LinearList::LinearList(void)
{
}

LinearList::~LinearList(void)
{
}

int LinearList::InitList(SqList *L)
{
	L->elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
	if (!L->elem)
	{
		return -1;
	}
	L->length = 0;
	L->MaxSize = LIST_INIT_SIZE;
	return 0;
}

int LinearList::DestroyList(SqList *L)
{
	if (L == NULL)
	{
		exit(1);
	}

	free(L);
	L = (SqList *)0;
	return 0;
}

int LinearList::ClearList(SqList *L)
{
	if (L == NULL)
	{
		exit(1);
	}
	free(L->elem);
	L->elem = (ElemType*)0;
	L->MaxSize = 0;
	L->length = 0;
	return 0;
}

bool LinearList::isListEmpty(SqList *L)
{
	if (L == NULL)
	{
		exit(1);
	}
	if (L->MaxSize == 0)
	{
		return true;
	}else
	{
		return false;
	}
}

int LinearList::getListLength(SqList *L)
{
	if (L == NULL)
	{
		exit(1);
	}

	return L->length;
}

//给定一个序号,去查找这个序号对应的元素值;
ElemType LinearList::getElem(SqList *L,int index) 
{
	if (L == NULL)
	{
		exit(1);
	}
	else if (index<0 || index>L->length)
	{
		printf("wrong index number in Function getElem!\n");

	}
	return L->elem[index];
	
}

//从列表中找到某一元素的位置
int LinearList::locateElem(SqList *L,ElemType e)
{
	if (L == NULL)
	{
		exit(1);
	}

	for (int i = 0;i<L->length;i++)
	{
		if (L->elem[i] == e)
		{
			return i+1;
		}
	}
	return -1;
}

ElemType LinearList::PriorElem(SqList *L,ElemType Cur_e)
{
	if (L == NULL)
	{
		exit(1);
	}
	for (int i = 1 ;i<L->length; i++)
	{
		if (L->elem[i] == Cur_e)
		{
			return L->elem[i-1];
		}
	}
	exit(1);
}

ElemType LinearList::NextElem(SqList *L,ElemType Cur_e)
{
	if (L == NULL)
	{
		exit(1);
	}
	for (int i = 0 ;i<L->length-1; i++)
	{
		if (L->elem[i] == Cur_e)
		{
			return L->elem[i+1];
		}
	}
	exit(1);
}

int LinearList::AgainMalloc(SqList *L)
{
	ElemType* p = (ElemType*)malloc((L->MaxSize + LIST_INCREMENT ) * sizeof(ElemType));
	if (!p)
	{
		printf("Malloc Again Fail\n");
		exit(1);
	}
	memcpy(p,L->elem,sizeof(L->elem)*L->MaxSize); //之前遗漏这一句,注意,要把以前的元素拷贝过来
	L->elem = p;
	L->MaxSize = L->MaxSize + LIST_INCREMENT; 
	return 0;
}

SqList* LinearList::ListInsert(SqList *L,int index ,ElemType e)
{
	if (L == NULL)
	{
		exit(1);
	}
	if (index<0||index>L->length)
	{
		printf("index error in function ListInsert\n");
		exit(1);
	}

	if (L->length == L->MaxSize)
	{
		//重新分配更大的空间
		AgainMalloc(L);
	}
	for (int i = L->length+1; i>index; i--)
	{
		L->elem[i]=L->elem[i-1];
	}
	L->elem[index] = e;
	L->length = L->length + 1;
	return L;
}

SqList* LinearList::ListAdd(SqList *L, ElemType e)
{
	if (L == NULL)
	{
		exit(1);
	}

	if (L->length == L->MaxSize)
	{
		//重新分配更大的空间
		AgainMalloc(L);
	}
	
	L->elem[L->length]=e;
	L->length = L->length + 1;
	return L;
}

void LinearList::ListDelete(SqList *L,int index ,ElemType &e)
{
	if (L == NULL)
	{
		exit(1);
	}
	if (index<0 || index>=L->length)
	{
		printf("index error in function ListDelete\n");
		exit(1);
	}

	for (int i = index; i< L->length-1; i++)
	{
		L->elem[i] = L->elem[i+1];
	}
	L->length = L->length - 1;
	e = L->elem[index];
}

SqList* LinearList::ChangeElem(SqList *L,int index ,ElemType e)
{
	if (L == NULL)
	{
		exit(1);
	}
	if (index<0 || index>=L->length)
	{
		printf("index error in function ListDelete\n");
		exit(1);
	}
	L->elem[index] = e;
	return L;

}

void LinearList::PrintSqList(SqList *L)
{
	if (L == NULL)
	{
		exit(1);
	}

	for (int i = 0; i< L->length; i++)
	{
		printf("SqList[%d]:%d  ", i ,getElem(L,i));
	}
	printf("\n");
}

//归并;
void LinearList::MergeList(SqList La, SqList Lb ,SqList &Lc)
{
	Lc.elem = (ElemType *)malloc((La.length + Lb.length) * sizeof(ElemType));
	if (!Lc.elem)
	{
		exit(1);
	}

	ElemType* pa = La.elem;
	ElemType* pb = Lb.elem;
	ElemType* pc = Lc.elem;
	ElemType* pa_last = La.elem + La.length - 1;
	ElemType* pb_last = Lb.elem + Lb.length - 1;

	while (pa<=pa_last && pb<=pb_last)
	{
		if (*pa <= *pb)
		{
			*pc++ = *pa++;
		}
		else
		{
			*pc++ = *pb++;
		}

	}

	while (pa <= pa_last)
	{
		*pc++ = *pa++;
	}
	while(pb <= pb_last)
	{
		*pc++ = *pb++;
	}
}


测试:

void TEST_LinearList()
{
	printf("-----------------------------------------------------\n");
	printf("-------Here is A Test For LinearList-----------------\n");

	SqList* L = (SqList *)malloc(sizeof(SqList));
	LinearList linearLIST;
	linearLIST.InitList(L);
	int i;
	printf("Add Elem!\n");
	for (i = 0;i<10;i++)
	{
		linearLIST.ListAdd(L,i);
	}
	linearLIST.PrintSqList(L);

	linearLIST.isListEmpty(L);

	int k = linearLIST.locateElem(L,5);

	SqList* Q =  linearLIST.ListInsert(L,5,123);
	linearLIST.PrintSqList(L);
	linearLIST.PrintSqList(Q);

	ElemType e;
	linearLIST.ListDelete(L, 8, e);
	linearLIST.PrintSqList(L);
}


说明:代码仅供参考,有错误的还望指正。




















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值