标签:动态链表的增删查改
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#define DATA_TYPE int
#define MAX_CAPACITY 5
typedef struct SeqList
{
DATA_TYPE* _array;//数据块指针
size_t _size;//当前有效的数据个数
size_t _capacity;//容量
}SeqList;
void InitSeqList(SeqList* seq);//初始化
void PrintSeqList(SeqList* seq);//打印
void PushBack(SeqList* seq, DATA_TYPE x);//尾插
void PopBack(SeqList* seq);//尾删
void PushFront(SeqList* seq, DATA_TYPE x);//头插
void PopFront(SeqList* seq);//头删
void Insert(SeqList* seq, size_t pos, DATA_TYPE x);//在指定位置插入x
void RemoveAll(SeqList* seq, DATA_TYPE x);//删除所有的x
void Erase(SeqList* seq, size_t pos);//删除pos位置的置
void BubbleSort(SeqList* seq);//冒泡排序
void SelectSort(SeqList* seq);//选择排序
int BinarySearch(SeqList* seq, DATA_TYPE x);//二分法
void CheckCapacity(SeqList* seq);//容量检查
void Swap(DATA_TYPE *i, DATA_TYPE *j)
{
DATA_TYPE tmp = *i;
*i = *j;
*j = tmp;
}
void CheckCapacity(SeqList* seq)
{
assert(seq);
if (seq->_capacity == seq->_size)
{
DATA_TYPE* tmp;
seq->_capacity = MAX_CAPACITY;
//法一
//seq->_array =(DATA_TYPE*)realloc(seq->_array, sizeof(DATA_TYPE)*seq->_capacity);
//法二
tmp = (DATA_TYPE*)malloc((seq->_size + seq->_capacity)*sizeof(DATA_TYPE));
memcpy(tmp, seq->_array, sizeof(DATA_TYPE)*seq->_size);
free(seq->_array);
seq->_array = tmp;
}
}
void InitSeqList(SeqList* seq)
{
seq->_array = NULL;
seq->_capacity = 0;
seq->_size = 0;
}
void PrintSeqList(SeqList* seq)
{
size_t index = 0;
assert(seq);
for (; index < seq->_size; index++)
{
printf("%d ", seq->_array[index]);
}
printf("\n");
}
void PushBack(SeqList* seq, DATA_TYPE x)
{
assert(seq);
CheckCapacity(seq);
seq->_array[seq->_size++] = x;
}
void PopBack(SeqList* seq)
{
assert(seq);
--seq->_size;
}
void PushFront(SeqList* seq, DATA_TYPE x)
{
size_t index = seq->_size;
assert(seq);
CheckCapacity(seq);
for (; index > 0; index--)
{
seq->_array[index] = seq->_array[index - 1];
}
seq->_array[0] = x;
++seq->_size;
}
void PopFront(SeqList* seq)
{
size_t index = 0;
assert(seq);
for (; index <seq->_size - 1; index++)
{
seq->_array[index] = seq->_array[index + 1];
}
--seq->_size;
}
void Insert(SeqList* seq, size_t pos, DATA_TYPE x)
{
size_t index = 0;
assert(seq);
assert(pos <= seq->_size);
CheckCapacity(seq);
for (index = seq->_size; index > pos; index--)
{
seq->_array[index] = seq->_array[index - 1];
}
seq->_array[pos] = x;
++seq->_size;
}
void RemoveAll(SeqList* seq, DATA_TYPE x)
{
size_t begin = 0;
size_t index = 0;
size_t count = 0;
for (; begin < seq->_size;)
{
if (seq->_array[begin] == x)
{
begin++;
count++;
}
else
{
seq->_array[index++] = seq->_array[begin++];
}
}
seq->_size -= count;
}
void Erase(SeqList* seq, size_t pos)
{
size_t index = 0;
assert(seq);
assert(pos < seq->_size);
for (index = pos; index<seq->_size - 1; index++)
{
seq->_array[index] = seq->_array[index + 1];
}
--seq->_size;
}
void BubbleSort(SeqList* seq)
{
size_t i = 0;
size_t j = 0;
size_t tag = 0;
for (i = 0; i < seq->_size - 1; i++)
{
tag = 0;
for (j = 0; j < seq->_size - 1 - i; j++)
{
if (seq->_array[j]>seq->_array[j + 1])
{
Swap(&seq->_array[j], &seq->_array[j + 1]);
tag = 1;
}
}
if (tag == 0)
break;
}
}
void SelectSort(SeqList* seq)//选择排序:同时找两个,一个最大下标,一个最小下标
{
int begin = 0;
int end = seq->_size - 1;
size_t index = 0;
size_t minIndex = 0;
size_t maxIndex = 0;
while (begin < end)
{
minIndex = begin;
maxIndex = end;//对minIndex和maxIndex赋值
for (index = begin; index < end + 1; index++)//for循环中,开区间,助于理解
{
if (seq->_array[minIndex]>seq->_array[index])
{
minIndex = index;
}
else if (seq->_array[maxIndex] < seq->_array[index])
{
maxIndex = index;
}
}
if (minIndex != begin)
{
Swap(&seq->_array[minIndex], &seq->_array[begin]);
if (maxIndex == begin)
maxIndex = minIndex;
}
if (maxIndex != end)
{
Swap(&seq->_array[maxIndex], &seq->_array[end]);
}
begin++;
end--;
}
}
int BinarySearch(SeqList* seq, DATA_TYPE x)
{
size_t left = 0;
size_t right = seq->_size - 1;
size_t mid = 0;
while (left <= right)//注意边界查找
{
mid = (left + right) / 2;
if (seq->_array[mid] == x)
return mid;
else if (seq->_array[mid] > x)
right = mid - 1;
else if (seq->_array[mid] < x)
left = mid + 1;
}
return -1;
}
void Test1()
{
int ret = 0;
SeqList seq;
InitSeqList(&seq);
PushBack(&seq, 2);
PushBack(&seq, 3);
PushBack(&seq, 4);
PushBack(&seq, 5);
PushBack(&seq, 6);//尾插六次
//PrintSeqList(&seq);
//PopBack(&seq);
//PrintSeqList(&seq);
//PushFront(&seq,7);
//PushFront(&seq, 8);
//PushFront(&seq, 9);
//PrintSeqList(&seq);
//PopFront(&seq);
//PrintSeqList(&seq);
//Insert(&seq,1,10);
//PrintSeqList(&seq);
PushFront(&seq, 3);//插入7测试RemoveAll
PushFront(&seq, 7);
PushFront(&seq, 7);
PushFront(&seq, 4);
PushFront(&seq, 7);
PushFront(&seq, 10);
PushFront(&seq, 7);
PushBack(&seq, 1);
PrintSeqList(&seq);
printf("%d\n", seq._size);
RemoveAll(&seq, 7);
PrintSeqList(&seq);
//Erase(&seq,1);//删除pos位置的置
//PrintSeqList(&seq);
//BubbleSort(&seq);
printf("%d\n", seq._size);
SelectSort(&seq);
PrintSeqList(&seq);
/*ret = BinarySearch(&seq, 10);
if (ret == -1)
printf("未找到\n");
else
printf("10的下标为:%d\n", ret);*/
free(seq->_array);
}
int main()
{
Test1();
system("pause");
return 0;
}本文出自 “小止” 博客,请务必保留此出处http://10541556.blog.51cto.com/10531556/1703970
标签:动态链表的增删查改
原文地址:http://10541556.blog.51cto.com/10531556/1703970