标签:
前面我们实现了顺序表,但是我们应该会考虑到一个问题,顺序表一次性创建那么大空间造成的浪费很多,所以在这里,我们需要一个可以动态增长的顺序表来满足我们的需求!
实现中需要注意的是:在这里我们要注意的是首先你应该给顺序表一个容量,当每次满了的时候,进行扩容!
另外,在这里我分别使用了三种的排序算法:插入排序,选择排序,冒泡排序。
#define _CRT_SECURE_NO_WARNINGS 1
#ifndef __DYNAMIC_SEQLIST_H__
#include<stdlib.h>
#include<stdio.h>
#include<assert.h>
#define INIT_SIZE 3
#define CAPACITY_SIZE 3
typedef int datatype;
typedef struct seqlist
{
datatype *data;
int size;
int capacity;
}Seqlist,*pSeqlist;
static enum seq
{
EXIT,
INIT,
PUSHBACK,
POPBACK,
PUSHFRONT,
POPFRONT,
INSERT,
REMOVE,
REMOVEALL,
BUBBLESORT,
SELECTSORT,
INSERTIONSORT,
ERASE,
BINARYSEARCH,
PRINTFSEQLIST
};
void PrintSeqlist(pSeqlist pSeq);
void InitSqlist(pSeqlist pSeq);
void DestorySeqlist(pSeqlist pSeq);
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);
void RemoveAll(pSeqlist pSeq, datatype x);
void BubbleSort(pSeqlist pSeq);
void SelectSort(pSeqlist pSeq);
void InsertionSort(pSeqlist pSeq);
void Erase(pSeqlist pSeq, int pos);
int BinarySearch(pSeqlist pSeq, datatype x);
void meau();
#endif // !__DYNAMIC_SEQLIST_H__
#define _CRT_SECURE_NO_WARNINGS 1
#include"dynamic_seqlist.h"
void meau()
{
printf("$$$$$$$$$$$$$$ SEQLIST $$$$$$$$$$$$$$$$\n");
printf("$$$$$$$$$$$$$$$$$$$$$$¥$$$$$$$$$$$$$$$$$$$$$\n");
printf("$$$ 1.init 2.push_back $$$\n");
printf("$$$ 3.pop_back 4.push_front $$$\n");
printf("$$$ 5.pop_front 6.insert $$$\n");
printf("$$$ 7.remove 8.removeall $$$\n");
printf("$$$ 9.bubblesort 10.selectsort $$$\n");
printf("$$$ 11.insertionsort 12.earse $$$\n");
printf("$$$ 13.binarysearch 14.printseqlist $$$\n");
printf("$$$ 0.EXIT $$$\n");
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
}
void InitSqlist(pSeqlist pseq)
{
pseq->data = (datatype *)malloc(sizeof(datatype)*INIT_SIZE);
if (NULL == pseq->data)
{
perror("out of memory");
exit(-1);
}
pseq->size = 0;
pseq->capacity = INIT_SIZE;
}
void DestorySeqlist(pSeqlist pSeq)
{
free(pSeq->data);
pSeq->data = NULL;
pSeq->size = 0;
pSeq->capacity = 0;
}
void PrintSeqlist(pSeqlist pSeq)
{
int i = 0;
for (i = 0; i < pSeq->size; i++)
{
printf("%3d", pSeq->data[i]);
}
printf("\n");
}
void check(pSeqlist pSeq)
{
datatype *tmp = NULL;
if (pSeq->size == pSeq->capacity)
{
tmp = (datatype *)realloc(pSeq->data, sizeof(datatype)*(pSeq->size + CAPACITY_SIZE));
if (tmp == NULL)
{
perror("out of memory");
exit(-1);
}
else
{
pSeq->data = tmp;
pSeq->capacity += CAPACITY_SIZE;
}
}
}
void PushBack(pSeqlist pSeq, datatype x)
{
check(pSeq);
pSeq->data[pSeq->size] = x;
pSeq->size++;
}
void PopBack(pSeqlist pSeq)
{
if (pSeq->size <= 0)
{
printf("顺序表已空\n");
return;
}
pSeq->size--;
}
void PushFront(pSeqlist pSeq, datatype x)
{
check(pSeq);
for (int i = pSeq->size; i >= 0; i--)
{
pSeq->data[i + 1] = pSeq->data[i];
}
pSeq->data[0] = x;
pSeq->size++;
}
void PopFront(pSeqlist pSeq)
{
if (pSeq->size <= 0)
{
printf("顺序表已空\n");
return;
}
for (int i = 0; i < pSeq->size; i++)
{
pSeq->data[i] = pSeq->data[i + 1];
}
pSeq->size--;
}
void Insert(pSeqlist pSeq, int pos, datatype x)
{
check(pSeq);
for (int i = pSeq->size; i >= pos; i--)
{
pSeq->data[i + 1] = pSeq->data[i];
}
pSeq->data[pos] = x;
pSeq->size++;
}
void Remove(pSeqlist pSeq, datatype x)
{
if (pSeq->size <= 0)
{
printf("顺序表已空,无法删除\n");
return;
}
for (int i = 0; i < pSeq->size; i++)
{
if (pSeq->data[i] == x)
{
for (int j = i; j < pSeq->size - i; j++)
{
pSeq->data[j] = pSeq->data[j + 1];
}
pSeq->size--;
return;
}
}
}
void RemoveAll(pSeqlist pSeq, datatype x)
{
if (pSeq->size <= 0)
{
printf("顺序表已空,无法删除\n");
return;
}
for (int i = 0; i < pSeq->size; i++)
{
if (pSeq->data[i] == x)
{
for (int j = i; j <pSeq->size; j++)
{
pSeq->data[j] = pSeq->data[j + 1];
}
pSeq->size--;
}
}
}
void BubbleSort(pSeqlist pSeq)
{
char ch = 0;
int i = 0;
int j = 0;
if (pSeq->size <= 0)
{
printf("顺序表已空,无法排序\n");
return;
}
printf("请选择排序类型(>:从大到小),(<:从小到大):\n");
fflush(stdin);
scanf("%c", &ch);
switch (ch)
{
case‘>‘:
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])
{
int tmp = pSeq->data[j];
pSeq->data[j] = pSeq->data[j + 1];
pSeq->data[j + 1] = tmp;
}
}
}
break;
case‘<‘:
for (i = 0; i < pSeq->size - 1; i++)
{
for (j = i; j < pSeq->size - i - 1; j++)
{
if (pSeq->data[j] > pSeq->data[j + 1])
{
int tmp = pSeq->data[j];
pSeq->data[j] = pSeq->data[j + 1];
pSeq->data[j + 1] = tmp;
}
}
}
break;
default:
printf("所给出排序类型有问题!\n");
break;
};
}
void SelectSort(pSeqlist pSeq)
{
datatype min = 0;
int i = 0;
int j = 0;
if (pSeq->size <= 0)
{
printf("顺序表已空,无法排序\n");
return;
}
for (i = 0; i < pSeq->size - 1; i++)
{
min = i ;
for (j = i + 1; j < pSeq->size; j++)
{
if (pSeq->data[min]>pSeq->data[j])
{
min = j;
}
}
if (min != i)
{
datatype tmp = pSeq->data[i];
pSeq->data[i] = pSeq->data[min];
pSeq->data[min] = tmp;
}
}
}
void InsertionSort(pSeqlist pSeq)
{
int i = 0;
int j = 0;
if (pSeq->size <= 0)
{
printf("顺序表已空,无法排序\n");
return;
}
for (i = 1; i < pSeq->size; i++)
{
datatype tmp = pSeq->data[i];
for (j = i - 1; j >= 0; j--)
{
if (pSeq->data[j]>tmp)
{
pSeq->data[j + 1] = pSeq->data[j];
}
else
{
break;
}
}
pSeq->data[j + 1] = tmp;
}
}
void Erase(pSeqlist pSeq, int pos)
{
int i = 0;
if (pSeq->size <= 0)
{
printf("顺序表已空,无法删除\n");
return;
}
else if (pos<0 || pos>=pSeq->size)
{
printf("输入位置不合法");
return;
}
for (i = pos; i <pSeq->size-1; i++)
{
pSeq->data[i] = pSeq->data[i + 1];
}
pSeq->size--;
}
int BinarySearch(pSeqlist pSeq, datatype x)
{
int left = 0;
int right = pSeq->size - 1;
int mid = 0;
while (left <= right)
{
mid = (left + right) >> 1;
if (pSeq->data[mid] < x)
{
left = mid + 1;
}
else if (pSeq->data[mid]>x)
{
right = mid - 1;
}
else
return mid;
}
return -1;;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include"dynamic_seqlist.h"
void Test()
{
Seqlist seq;
int input = 1;
int x = 0;
int pos = 0;
int search = 0;
InitSqlist(&seq);
while (input)
{
meau();
printf("请选择:");
scanf("%d", &input);
fflush(stdin);
switch (input)
{
case INIT:
InitSqlist(&seq);
break;
case PUSHBACK:
fflush(stdin);
printf("请输入你所要尾部push的元素:\n");
scanf("%d", &x);
PushBack(&seq, x);
break;
case POPBACK:
PopBack(&seq);
break;
case PUSHFRONT:
fflush(stdin);
printf("请输入你所要头部push的元素:\n");
scanf("%d", &x);
PushFront(&seq, x);
break;
case POPFRONT:
PopFront(&seq);
break;
case INSERT:
fflush(stdin);
printf("请输入你所要插入的元素:\n");
scanf("%d", &x);
fflush(stdin);
printf("请输入你所要插入的位置:\n");
scanf("%d", &pos);
Insert(&seq, pos, x);
break;
case REMOVE:
fflush(stdin);
printf("请输入要删除的元素\n");
scanf("%d", &x);
Remove(&seq, x);
break;
case REMOVEALL:
fflush(stdin);
printf("请输入要删除的元素\n");
scanf("%d", &x);
RemoveAll(&seq, x);
break;
case BUBBLESORT:
BubbleSort(&seq);
break;
case SELECTSORT:
SelectSort(&seq);
break;
case INSERTIONSORT:
InsertionSort(&seq);
break;
case ERASE:
fflush(stdin);
printf("请输入你所要删除元素的位置");
scanf("%d", &pos);
Erase(&seq,pos);
break;
case BINARYSEARCH:
printf("请输入要二分查找的元素\n");
fflush(stdin);
scanf("%d", &x);
search = BinarySearch(&seq, x);
printf("%d所在的位置是:%d", x, search);
break;
case EXIT:
DestorySeqlist(&seq);
input = 0;
break;
case PRINTFSEQLIST:
PrintSeqlist(&seq);
break;
default:
break;
}
}
}
int main()
{
Test();
system("pause");
return 0;
}
标签:
原文地址:http://blog.csdn.net/qq_26768741/article/details/51558274