不定长顺序表

不定长顺序表和定长顺序表在很多地方都是一样的,只不过采用了动态分配的方式处理数据。具体代码如下:

头文件 desqlist.h

#pragma once
//不定长顺序表

#define INIT_SIZE 10 //初始化单元数量

typedef struct DSeqList
{
	int *elem;//指向存储数据的内存
	int length;//有效数据个数
	int listsize;//总格子数(开辟的总单元个数)
}DSeqList,*PDSeqList;


void InitDSeqList(PDSeqList ps);//SeqList *ps

bool Insert(PDSeqList ps,int pos,int val);

bool IsEmpty(PDSeqList ps);

//rtval输出参数
bool Delete(PDSeqList ps,int pos,int *rtval);

bool  GetElem(PDSeqList ps,int pos,int *rtval);

bool SetElem(PDSeqList ps,int pos,int newval);

int Search(PDSeqList ps,int key);

void Show(PDSeqList ps);

void Destroy(PDSeqList ps);

void Clear(PDSeqList ps);

//获取有效数据的个数
int GetLength(PDSeqList ps);

void Merge(PDSeqList a,PDSeqList b); //A=AUB

void Nerge(PDSeqList a,PDSeqList b);// A=AnB

具体实现 Desqlist.cpp文件

#include"seqlist.h"
#include<stdio.h>
#include<assert.h>

void InitSeqList(PSeqList ps)//初始化
{
	assert(ps != NULL);
	if(ps == NULL)
	{
		return ;
	}
	ps->length = 0;
}

static bool IsFull(PSeqList ps)//内部判满
{
	return ps->length == NUM;
}

bool Insert(PSeqList ps,int pos,int val)//插入
{
	assert(ps != NULL);
	if(ps == NULL || pos>ps->length || pos<0 || IsFull(ps))//pos>ps->length 是因为插入时可以直接插入在顺序表的最后面
	{
		return false;
	}
	
	for(int i = ps->length-1;i>=pos;i--)   
	{                                   
		ps->elem[i+1]=ps->elem[i];
	}

	ps->elem[pos] = val;

	ps->length++;

	return true;
}

bool IsEmpty(PSeqList ps)//判空
{
	return  ps->length == 0;
}

//rtval输出参数
bool Delete(PSeqList ps,int pos,int *rtval)//删除
{
	assert(ps != NULL);
	if(ps == NULL || pos>=ps->length || pos<0)
	{
		return false;
	}
	if(rtval!=NULL)
	{
		*rtval=ps->elem[pos];
	}

	for(int i = pos;i<ps->length;i++)
	{
		ps->elem[i]=ps->elem[i+1];
	}

	ps->length--;

	return true;
}

bool  GetElem(PSeqList ps,int pos,int *rtval)//得到某位元素
{
	assert(ps != NULL);
	if(ps == NULL || pos>ps->length || pos<0)
	{
		return false;
	}

	*rtval=ps->elem[pos];

	return true;
}

bool SetElem(PSeqList ps,int pos,int newval)//改变某位元素
{
	assert(ps != NULL);
	if(ps == NULL || pos>=ps->length || pos<0)
	{
		return false;
	}

	ps->elem[pos]=newval;

	return true;
}

int Search(PSeqList ps,int key)//查找
{
	assert(ps != NULL);
	if(ps == NULL)
	{
		return -1;
	}

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

	return -1;
}

int GetLength(PSeqList ps)//获取有效数据的个数
{
	return ps->length;
}

void Show(PSeqList ps)//打印
{
	assert(ps != NULL);
	if(ps == NULL)
	{
		return ;
	}

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

void Destroy(PSeqList ps)//销毁
{
	Clear(ps);
}

void Clear(PSeqList ps)//清除
{
	ps->length = 0;
}

test.cpp

int main()
{
        DSeqList a;
	InitDSeqList(&a);
	for(int i=0;i<4;i++)
	{
		Insert(&a,i,i);
	}
	Show(&a);
	
	DSeqList b;
	InitDSeqList(&b);
	for(int i=0;i<5;i++)
	{
		Insert(&b,i,i);
	}
	Show(&b);
	//Merge(&a,&b);
	//Show(&a);
	Nerge(&a,&b);
	Show(&a);
	Clear(&a);
	Destroy(&a);
	Clear(&b);
	Destroy(&b);
}




猜你喜欢

转载自blog.csdn.net/swty3356667/article/details/79241795
今日推荐