顺序表疑难杂症解除

今天的我来安利一下顺序表,以前没写过动态的,今天来写一个动态的,写的马马虎虎,但是如果你是一个初学者,我想对你来说还是有一点点帮助的,毕竟我写的很全,基本上都包括了,应付你的作业一定没有问题。

首先你要将结构体的知识学的差不多,置于什么样的才算差不多,你自己掂量掂量。如果你没学懂,那就多学几遍,如果还是不懂,请把这句话再读一遍。当然,你学的很菜也能看懂我的代码。

#pragma once

typedef int DataType;
#define DEFAULT_CAPACITY 3
typedef struct SeqList
{
DataType* _a;
size_t _size; // 有效数据个数 
size_t _capacity; // 容量 
}SeqList;
    


void SeqPrint(SeqList* pSeq);              //打印链表
void SeqInit(SeqList* pSeq);               //初始化链表
void SeqDestory(SeqList* pSeq);        //销毁链表
void CheckExpandCapacity(SeqList* pSeq);   // 检查容量


void SeqPushBack(SeqList* pSeq, DataType x);//尾插法
void SeqPopBack(SeqList* pSeq);       //尾删法
void SeqPushFront(SeqList* pSeq, DataType x);  //头插法
void SeqPopFront(SeqList* pSeq);     //头插法


void SeqInsert(SeqList* pSeq, size_t pos, DataType x);  //任意位置添加;
void SeqErase(SeqList* pSeq, size_t pos);    // 任意位置删除


int SeqFind(SeqList* pSeq, DataType x);   //找到x所在的位置;
void SeqAt(SeqList* pSeq, size_t pos, DataType x);  //修改某一位置的值


void swap(DataType*left, DataType*right);//交换值
void BubbleSort(SeqList* pSeq);//冒泡排序
void SelectSort(SeqList* pSeq);//选择排序
int BinarySearch(SeqList* pSeq,DataType x);//二分查找
void Test();
//正文开始;
void SeqPrint(SeqList* pSeq)
{
assert(pSeq);
int i = 0;
for (; (size_t)i < pSeq->_size; i++)
{
printf("%5d", pSeq->_a[i]);
}
printf("\n");
}
void SeqInit(SeqList* pSeq)
{


pSeq->_a = (DataType*)malloc(sizeof(DataType)*DEFAULT_CAPACITY);
assert(pSeq);
pSeq->_size = 0;
pSeq->_capacity = DEFAULT_CAPACITY;
}
void CheckExpandCapacity(SeqList* pSeq)
{
assert(pSeq);
if (pSeq->_size == pSeq->_capacity)
{
DataType* temp = \
(DataType*)malloc(pSeq->_capacity * 2 * sizeof(DataType));
memcpy(temp, pSeq->_a, sizeof(DataType)*pSeq->_size);
free(pSeq->_a);
pSeq->_a = temp;
pSeq->_capacity = pSeq->_capacity * 2;
}
}
void SeqDestory(SeqList* pSeq)
{
if (pSeq)
{
free(pSeq->_a);
pSeq->_capacity = 0;
pSeq->_size = 0;
}
pSeq = NULL;
}


void SeqPushBack(SeqList* pSeq, DataType x)
{
assert(pSeq);
CheckExpandCapacity(pSeq);
pSeq->_a[pSeq->_size++] = x;

}
void SeqPopBack(SeqList* pSeq)
{
if (pSeq->_size == 0)
return;
else
pSeq->_size--;
}
void SeqPushFront(SeqList* pSeq, DataType x)
{
int i = pSeq->_size;
assert(pSeq);
CheckExpandCapacity(pSeq);
for (; i >0 ; i--)
{
pSeq->_a[i] = pSeq->_a[i-1];

}
pSeq->_a[0] = x;
pSeq->_size++;


}//要学会跳出循环;
void SeqPopFront(SeqList* pSeq)
{
int i = 0;
if (pSeq->_size == 0)
return;
for (; (size_t)i < pSeq->_size; i++)
{
pSeq->_a[i] = pSeq->_a[i + 1];

}
pSeq->_size--;


}


void SeqInsert(SeqList* pSeq, size_t pos, DataType x)
{
assert(pSeq);
int i = pSeq->_size;
for (; (size_t)i >pos; i--)
{
pSeq->_a[i] = pSeq->_a[i-1];

}
pSeq->_a[pos] = x;
pSeq->_size++;
}
void SeqErase(SeqList* pSeq, size_t pos)
{
int i = pos;
assert(pSeq);
assert(pos < pSeq->_size);
for (; (size_t)i < pSeq->_size; i++)
{
pSeq->_a[i] = pSeq->_a[i+1 ];
}
pSeq->_size--;
}


int SeqFind(SeqList* pSeq, DataType x)
{

int i = 0;
for (;(size_t) i < pSeq->_size; i++)
{
if (pSeq->_a[i] = x)
return i;

}
}


void SeqAt(SeqList* pSeq, size_t pos, DataType x)
{
assert(pSeq);
if (pos > pSeq->_size)
return;
else
pSeq->_a[pos] = x;

}




void BubbleSort(SeqList* pSeq)
{
int i = 0;

int cur=0;
int flag = 0;
for (; i < pSeq->_size; i++)
{
int j = 0;
for (; j < pSeq->_size -1- i; j++)
{
if (pSeq->_a[j]>pSeq->_a[j + 1])
{
cur = pSeq->_a[j];
pSeq->_a[j] = pSeq->_a[j + 1];
pSeq->_a[j + 1] = cur;
flag = 1;
}
if (flag = 0)
return;
}
}
}
void swap(DataType*left, DataType*right)
{
DataType temp = *left;
*left = *right;
*right = temp;
}


void SelectSort(SeqList* pSeq)
{
int i = 0;
int j = 0;
int min = 0;
for (; i < pSeq->_size; i++)
{
min = i;
for (j=i+1; j <pSeq->_size; j++)
{
if (pSeq->_a[min]>pSeq->_a[j])
//min = j;
swap(&pSeq->_a[j], &pSeq->_a[min]);
//min = j;

}
}
}


int BinarySearch(SeqList* pSeq, DataType x)
{
int left = 0;
int right = pSeq->_size-1;
int mid = (left + right)/2;


while (left <= right)
{
mid = (left + right) / 2;


if (pSeq->_a[mid]==x)
return mid;
else if (pSeq->_a[mid] < x)
left = mid + 1;
else  
right=mid-1;

}
return -1;

}
void Test()
{
SeqList s;
SeqInit(&s);
SeqPushBack(&s, 3);
SeqPushBack(&s, 4);
SeqPushBack(&s, 5);
SeqPushBack(&s, 6);
SeqPushBack(&s, 7);

SeqPopBack(&s);
SeqPopBack(&s);
SeqPopBack(&s);
SeqPushFront(&s, 2);
SeqPushFront(&s, 1); 
SeqPopFront(&s);


SeqPrint(&s);
}
void Test2()
{
SeqList s;
int k = 0;
SeqInit(&s);

SeqPushBack(&s, 3);
SeqPushBack(&s, 4);
SeqPushBack(&s, 5);
SeqPushBack(&s, 6);
SeqPushBack(&s, 7);
SeqErase(&s, 3);
SeqInsert(&s, 3, 20);
k = SeqFind(&s, 2);
printf("您搜索的数字下标是:%d\n", k);

SeqPrint(&s);



}
void Test3()
{
SeqList s;

SeqInit(&s);


SeqPushBack(&s, 8);
SeqPushBack(&s, 10);
SeqPushBack(&s, 5);
SeqPushBack(&s, 0);
SeqPushBack(&s, -1);
SeqPushBack(&s, 3);
SeqPushBack(&s, 6);
SeqPushBack(&s, 7);
BubbleSort(&s);
SeqPrint(&s);


}
void Test4()
{
SeqList s;
int m = 0;


SeqInit(&s);

SeqPushBack(&s, 0);
SeqPushBack(&s, 1);
SeqPushBack(&s, 2);
SeqPushBack(&s, 3);
SeqPushBack(&s, 4);
SeqPushBack(&s, 5);
SeqPushBack(&s, 6);
SeqPushBack(&s, 7);
//BubbleSort(&s);
m=BinarySearch(&s, 0);
printf("%d\n", m);
m=BinarySearch(&s, 1);
m=BinarySearch(&s, 2);
m=BinarySearch(&s, 3);
m=BinarySearch(&s, 4);
m=BinarySearch(&s, 6);
m=BinarySearch(&s, 7);
m=BinarySearch(&s, 8);
SeqAt(&s, 2, 30);




SeqPrint(&s);


}
void Test5()
{
SeqList s;


SeqInit(&s);
SeqPushBack(&s, 8);
SeqPushBack(&s, 10);
SeqPushBack(&s, 5);
SeqPushBack(&s, 0);
SeqPushBack(&s, -1);
SeqPushBack(&s, 3);
SeqPushBack(&s, 6);
SeqPushBack(&s, 7);
SelectSort(&s);


SeqPrint(&s);

}



好了,你把我的代码看了,我也应该对你负责一下

首先我要告诉你一个道理,明白一个东西有什么作用往往比知道怎么实现它来的更加重要:

动态顺序表的意义在于可以便于我们任意的调整顺序表的长度。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值