動態順序表

        順序表是在進算計內存總以數組的形式保存的線性表,是指用一組地址連續的存儲單元依次存儲數據元素的線性結構。線性表採用的順序存儲就稱之爲順序表。

        順序表是將表中的節點依次存放在計算機內存中一組地址連續的存儲單元中。

        任務要求:實現一個動態順序表

        功能要求:表可以動態增長,尾插元素,尾刪元素,頭插元素,頭刪元素,在指定位置插入指定元素,刪除指定元素,刪除所有指定的元素,排序,二分查找,翻轉表。

  【代碼實現】

/*DynamicSeqList.h*/

#pragma once

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

#define INIT_SIZE 3
#define EXPAND_SIZE 3
#define MAX_LIST 10

typedef int DataType;

typedef	struct SeqList
{
	DataType *data;
	int size;
	int capacity;
}SeqList, *pSeqList;

void InitList(pSeqList pSeq);
void CheckCapacity(pSeqList pSeq);
void Print(SeqList Seq);
void PushBack(pSeqList pSeq, DataType x);
void PopBack(pSeqList pSeq);
void PushFront(pSeqList pSeq, DataType x);
void PopFront(pSeqList pSeq);
void Insert(pSeqList pSeq, int pos, DataType x);
void Remove(pSeqList pSeq, DataType x);
int Find(pSeqList pSeq, DataType x);
void Erase(pSeqList pSeq,int pos);
void RemoveAll(pSeqList pSeq, DataType x);
void ReverseList(pSeqList pSeq);
void SortList(pSeqList pSeq);
int BinarySearch(SeqList Seq, DataType x);
void DestroySeqList(pSeqList pSeq);

 

/*DynamicSeqList.c*/

#include "DynamicSeqList.h"

/*初始化,銷燬空間*/
void InitList(pSeqList pSeq)
{
	assert(pSeq);
	pSeq->data = (DataType*)malloc(INIT_SIZE*sizeof(DataType));
	memset(pSeq->data, 0, INIT_SIZE*sizeof(DataType));
	pSeq->capacity = INIT_SIZE;
	pSeq->size = 0;
}
void DestroySeqList(pSeqList pSeq)
{
	if(pSeq)
	{
		free(pSeq->data);
		pSeq->data = NULL;
		pSeq->size = 0;
		pSeq->capacity = 0;
	}
}

/*檢查容量,進行擴容*/
void CheckCapacity(pSeqList pSeq)
{
	assert(pSeq);
	if(pSeq->size == pSeq->capacity)
	{
		pSeq->data = (DataType*)realloc(pSeq->data, sizeof(DataType)*(pSeq->capacity+EXPAND_SIZE));
		pSeq->capacity = pSeq->capacity + EXPAND_SIZE;
	}
}

/*尾插,尾刪,頭插,頭刪*/
void PushBack(pSeqList pSeq, DataType x)
{
	CheckCapacity(pSeq);
	pSeq->data[pSeq->size] = x;
	pSeq->size++;
}
void PopBack(pSeqList pSeq)
{
	if(pSeq->size == 0)
	{
		printf("表已空!!\n");
		return;
	}
	pSeq->size -= 1;
}
void PushFront(pSeqList pSeq, DataType x)
{
	int i = 0;
	CheckCapacity(pSeq);
	for(i=pSeq->size-1; i>=0; i--)
	{
		pSeq->data[i+1] = pSeq->data[i];
	}
	pSeq->data[0] = x;
	pSeq->size += 1;
}
void PopFront(pSeqList pSeq)
{
	int i = 0;
	assert(pSeq);
	if (pSeq->size == 0)
	{
		printf("順序表已空\n");
		return;
	}
	for (i=0; i <= pSeq->size; i++)
	{
		pSeq->data[i] = pSeq->data[i+1];
	}
	pSeq->size--;
}

/*插入元素,刪除元素*/
void Insert(pSeqList pSeq, int pos, DataType x)
{
	int i = 0;
	assert(pSeq);
	CheckCapacity(pSeq); //檢查插入元素前容量
	if (pos > 0 && pos < pSeq->size)
	{
		for(i = pSeq->size; i >= pos; i--)
		{
			pSeq->data[i+1]= pSeq->data[i];
		}
		pSeq->data[pos] = x;
		pSeq->size++;
		CheckCapacity(pSeq); //檢查插入元素後容量
	}	
	else
	{
		printf("請正確輸入要插入數的位置,範圍(0,%d)\n",pSeq->size);
		return;
	}
}
int Find(pSeqList pSeq, DataType x)	//遍歷指定元素
{		
	int i = 0;
	for(i; i<pSeq->size; i++)
	{
		if (pSeq->data[i] == x)
		{
			return i;
		}
	}
	return -1;
}
void Erase(pSeqList pSeq,int pos)	//刪除指定下標元素
{		 
	for(pos; pos<=pSeq->size; pos++)
	{
		pSeq->data[pos] = pSeq->data[pos+1];
	}
	pSeq->size--;
}
void Remove(pSeqList pSeq, DataType x)
{
	int ret = Find(pSeq, x);
	if (ret == -1)
	{
		printf("表中沒有%d!!\n", x);
		return;
	}
	Erase(pSeq,ret);
}
void RemoveAll(pSeqList pSeq, DataType x)
{
	int i = 0;
	for (i=0; i<=pSeq->size-1; i++)
	{
		if (pSeq->data[i] == x)
		{
			Erase(pSeq, i);
			i -= 1;
		}
	}
}

/*翻轉表,排序,二分查找*/
void ReverseList(pSeqList pSeq)
{
	int start = 0;
	int end = pSeq->size-1;
	while (start < end)
	{
		DataType tmp = pSeq->data[start];
		pSeq->data[start] = pSeq->data[end];
		pSeq->data[end] = tmp;
		start++;
		end--;
	}
}
void SortList(pSeqList pSeq)
{
	int i = 0;
	int j = 0;
	for(i=0; i<pSeq->size-1; i++)
	{
		for(j=0; j<pSeq->size-i-1; j++)
		{
			if (pSeq->data[j]>pSeq->data[j+1])
			{
				DataType tmp = pSeq->data[j];
				pSeq->data[j] = pSeq->data[j+1];
				pSeq->data[j+1] = tmp;
			}
		}
	}
}
int BinarySearch(SeqList Seq, DataType x)
{
	int left = 0;
	int right = Seq.size-1;
	while (left<right)
	{
		int mid = (left + (right - left))>>1;
		if (Seq.data[mid] > x)
		{
			right = mid - 1;
		}
		else if(Seq.data[mid] == x)
		{
			return mid;
		}
		else
		{
			left = mid +1;
		}
	}
	return -1;
}

/*打印表*/
void Print(SeqList MySeq)
{
	int i = 0;
	for(i; i<=MySeq.size-1; i++)
	{
		printf("%d ", MySeq.data[i]);
	}
	printf(" over\n");
}

 

/*test.c*/

#include "DynamicSeqList.h"

void test()
{
	SeqList MySeq;
	int ret = 0;
	InitList(&MySeq);
	PushBack(&MySeq, 1);
	PushFront(&MySeq, 2);
	//PopFront(&MySeq);
	Insert(&MySeq, 1, 6);
	//PushBack(&MySeq, 1);
	PushFront(&MySeq, 2);
	PushFront(&MySeq, 2);
	Insert(&MySeq, 1, 4);
	Print(MySeq);
	RemoveAll(&MySeq, 2);
	Print(MySeq);
	ReverseList(&MySeq);
	Print(MySeq);
	SortList(&MySeq);
	Print(MySeq);
	ret = BinarySearch(MySeq,4);
	if(ret == -1)
	{
		printf("表中沒有此元素!!\n");
	}
	else
	{
		printf("要查找的元素在表中的位置是:%d\n",ret+1);
	}
	DestroySeqList(&MySeq);	
}

int main()
{
	test();
	system("pause");
	return 0;
}


【結果顯示】

650) this.width=650;" src="http://s1.51cto.com/wyfs02/M00/7C/C2/wKiom1bXr9aRunASAAAmBuium3E143.png" title="69T{N_FE{OMWTHL`_BZQCAG.png " alt="wKiom1bXr9aRunASAAAmBuium3E143.png" />

本文出自 “Pzd流川楓” 博客,請務必保留此出處http://xujiafan.blog.51cto.com/10778767/1747070

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