C語(yǔ)言實(shí)現(xiàn)順序表-創(chuàng)新互聯(lián)

順序表是C語(yǔ)言中一種基本的結(jié)構(gòu),可以存儲(chǔ)各種基本類型的數(shù)據(jù),而且不但可以存儲(chǔ)基本類型的數(shù)據(jù),也可以存儲(chǔ)一種結(jié)構(gòu)。所以順序表是一種在學(xué)C的過(guò)程中必須掌握的結(jié)構(gòu),通過(guò)學(xué)習(xí)整理,下面來(lái)實(shí)現(xiàn)一下:

成都創(chuàng)新互聯(lián)公司長(zhǎng)期為近千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為漢川企業(yè)提供專業(yè)的成都做網(wǎng)站、網(wǎng)站建設(shè)、外貿(mào)營(yíng)銷(xiāo)網(wǎng)站建設(shè),漢川網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。

 首先,先要想好用什么實(shí)現(xiàn),一般實(shí)現(xiàn)最基本的順序表的話直接用數(shù)組實(shí)現(xiàn),我們?cè)谶@用一個(gè)結(jié)構(gòu)體來(lái)封裝這個(gè)順序表(封裝這一概念是在C++中最常用的概念)

#define ARRAY_EMPTY -2
#define ARRAY_FULL -1
#define MAX_SIZE 10000
typedef int Datatype;


typedef struct Seqlist
{
	Datatype array[MAX_SIZE];
	size_t size;
}Seqlist;

 對(duì)于無(wú)法確定數(shù)組大小,我們可以用宏來(lái)表示。

 把大體結(jié)構(gòu)搭建好了之后,就可以思考一個(gè)順序表應(yīng)該具有的功能,頭插,尾插,定點(diǎn)插入,排序等等都是一些最基本的功能:

	void PrintSeqlist(Seqlist* pSeq);
	void InitSeqlist(Seqlist* pSeq);
	void PushBack(Seqlist* pSeq, Datatype x);
	void PushFront(Seqlist* pSeq, Datatype x);
	void PopBack(Seqlist* pSeq);
	void PopFront(Seqlist* pSeq);
	void Insert(Seqlist* pSeq,size_t pos, Datatype x);
	int Find(Seqlist* pSeq, size_t pos, Datatype x);
	void Erase(Seqlist* pSeq, size_t pos);
	int Remove(Seqlist* pSeq, Datatype x);
	int Removeall(Seqlist* pSeq, Datatype x);
	void Bubblesort(Seqlist* pSeq);
	void Selectsort(Seqlist* pSeq);
	int BinarySearch(Seqlist* pSeq, Datatype x);

 下面就是一些關(guān)鍵函數(shù)的實(shí)現(xiàn):

void PrintSeqlist(Seqlist* pSeq)//輸出順序表
{
	assert(pSeq);
	size_t i = 0;
	if (pSeq->size <= 0)
	{
		printf("Array is empty\n");
		return;
	}
	else if (pSeq->size >= MAX_SIZE)
	{
		printf("Array is full\n");
	}
	for (; i < pSeq->size; ++i)
	{
		printf("%-4d", pSeq->array[i]);
	}
}

void InitSeqlist(Seqlist* pSeq)//初始化順序表
{
	pSeq->size = 0;
	//printf("Initialization is complete\n");
}

void PushBack(Seqlist* pSeq, Datatype x)//后插入
{
	assert(pSeq);
	if (pSeq->size >= MAX_SIZE)
	{
		printf("Array is full, insert the failure\n");
		return;
	}
	pSeq->array[pSeq->size] = x;
	pSeq->size++;
}

void PushFront(Seqlist* pSeq, Datatype x)//前插入
{
	assert(pSeq);
	size_t i = 0;

	if (pSeq->size >= MAX_SIZE)
	{
		printf("Array is full, insert the failure\n");
		return;
	}

	for (i = pSeq->size; i > 0; --i)
	{
		pSeq->array[i] = pSeq->array[i - 1];
	}

	pSeq->array[0] = x;
	pSeq->size++;
}


void PopBack(Seqlist* pSeq)//后刪除
{
	assert(pSeq);

	if (pSeq->size <= 0)
	{
		printf("Array is empty,popback is eeror\n");
		return;
	}

	pSeq->array[pSeq->size-1] = 0;
	pSeq->size--;
}


void PopFront(Seqlist* pSeq)//前刪除
{
	assert(pSeq);
	size_t i = 0;

	if (pSeq->size <= 0)
	{
		printf("Array is empty,popback is eeror\n");
		return;
	}

	for (i = 1; i < pSeq->size; i++)
	{
		pSeq->array[i - 1] = pSeq->array[i];
	}

	pSeq->size--;
}


void Insert(Seqlist* pSeq, size_t pos, Datatype x)//定點(diǎn)刪除
{
	assert(pSeq);
	size_t i = 0;

	if (pSeq->size >= MAX_SIZE)
	{
		printf("Array is full, insert the failure\n");
		return;
	}

	for (i = pSeq->size ; i > pos; i--)
	{
		pSeq->array[i] = pSeq->array[i - 1];
	}

	pSeq->array[pos] = x;
	pSeq->size++;
}


int Find(Seqlist* pSeq, size_t pos, Datatype x)//查找數(shù)據(jù)
{
	assert(pSeq);
	size_t i = 0;

	if (pSeq->size <= 0)
	{
		printf("Array is empty,erase error\n");
		return ARRAY_EMPTY;
	}

	if (pos < 0 || pos >= MAX_SIZE)
	{
		printf("Began to find the position error\n");
		return -1;
	}

	for (i = pos; i < pSeq->size; i++)
	{
		if (pSeq->array[i] == x)
		{
			return i;
		}
	}


	return -2;
}


void Erase(Seqlist* pSeq, size_t pos)//刪除特定數(shù)據(jù)
{
	assert(pSeq);
	size_t i = 0;
	if (pSeq->size <= 0)
	{
		printf("Array is empty,erase error\n");
		return;
	}
	if (pos < 0 || pos >= MAX_SIZE)
	{
		printf("Began to find the position error\n");
		return;
	}
	for (i = pos; i < pSeq->size; i++)
	{
		pSeq->array[i] = pSeq->array[i + 1];
	}
	pSeq->size--;
}


int Remove(Seqlist* pSeq, Datatype x)
{
	assert(pSeq);
	size_t i = 0;
	if (pSeq->size <= 0)
	{
		printf("Array is empt,remove error\n");
		return ARRAY_EMPTY;
	}
	for (; i < pSeq->size; i++)
	{
		if (pSeq->array[i] == x)
		{
			size_t j = i + 1;
			for (; j < pSeq->size; j++)
			{
				pSeq->array[j - 1] = pSeq->array[j];
			}
			pSeq->size--;
			return 0;
		}
	}
	return -1;
}


int Removeall(Seqlist* pSeq, Datatype x)
{
	assert(pSeq);
	size_t i = 0;
	int flag = 0;
	if (pSeq->size <= 0)
	{
		printf("Array is empt,remove error\n");
		return ARRAY_EMPTY;
	}
	for (; i < pSeq->size; i++)
	{
		if (pSeq->array[i] == x)
		{
			size_t j = i + 1;
			for (; j < pSeq->size; j++)
			{
				pSeq->array[j - 1] = pSeq->array[j];
			}
			pSeq->size--;
			flag++;
			if (pSeq->size <= 0)//如果順序表的元素為空就不能再刪除了,返回
			{
				return 0;
			}
			else
			{
				i--;
			}
		}
	}
	if (flag > 0)
		return 0;
	else
		return -1;
}


void Bubblesort(Seqlist* pSeq)//冒泡排序
{
	assert(pSeq);
	size_t i = 0;
	for (; i < pSeq->size; i++)
	{
		size_t j = 0;
		for (; j < pSeq->size - i - 1; j++)
		{
			if (pSeq->array[j] > pSeq->array[j + 1])
			{
				int tmp = pSeq->array[j];
				pSeq->array[j] = pSeq->array[j + 1];
				pSeq->array[j + 1] = tmp;
			}
		}
	}
}



void Selectsort(Seqlist* pSeq)//選擇排序
{
	assert(pSeq);
	size_t i = 0;
	for (; i < pSeq->size; i++)
	{
		int flag = pSeq->array[i];
		size_t j = i;
		for (; j < pSeq->size; j++)
		{
			if (pSeq->array[j] < flag)
			{
				int tmp = pSeq->array[j];
				pSeq->array[j] = flag;
				flag = tmp;
			}
		}
		pSeq->array[i] = flag;
	}
}


int BinarySearch(Seqlist* pSeq, Datatype x)//二分查找
{
	assert(pSeq);
	size_t left = 0;
	size_t right = pSeq->size - 1;
	while (left <= right)
	{
		size_t mid = left + (right - left) / 2;
		if (x > pSeq->array[mid])
		{
			left = mid + 1;
		}
		else if (x < pSeq->array[mid])
		{
			right = mid - 1;
		}
		else if (x == pSeq->array[mid])
		{
			return (int)mid;
		}
	}
	return -1;
}

  這只是最基本的順序表,只能夠存儲(chǔ)基本類型的數(shù)據(jù),如果想要存儲(chǔ)結(jié)構(gòu)體或者string之類的數(shù)據(jù)的話會(huì)涉及C++的深淺拷貝問(wèn)題,而且用一個(gè)數(shù)組來(lái)存未知大小的數(shù)據(jù)結(jié)構(gòu)的話是做不到的。

 本人的博客后面會(huì)更新順序表的一些優(yōu)化寫(xiě)法,希望對(duì)朋友微小的幫助。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

本文題目:C語(yǔ)言實(shí)現(xiàn)順序表-創(chuàng)新互聯(lián)
本文URL:http://www.muchs.cn/article10/dcdogo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷(xiāo)、網(wǎng)站制作企業(yè)網(wǎng)站制作、軟件開(kāi)發(fā)、電子商務(wù)小程序開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化