您的位置:首页 > 其它

动态顺序表

2016-03-03 11:31 113 查看
顺序表是在进算计内存总以数组的形式保存的线性表,是指用一组地址连续的存储单元依次存储数据元素的线性结构。线性表采用的顺序存储就称之为顺序表。 顺序表是将表中的节点依次存放在计算机内存中一组地址连续的存储单元中。 任务要求:实现一个动态顺序表 功能要求:表可以动态增长,尾插元素,尾删元素,头插元素,头删元素,在指定位置插入指定元素,删除指定元素,删除所有指定的元素,排序,二分查找,翻转表。 【代码实现】
/*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;
}

【结果显示】


本文出自 “Pzd流川枫” 博客,请务必保留此出处http://xujiafan.blog.51cto.com/10778767/1747070
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: