?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???? ?? ??
??個(gè)人主頁 :阿然成長日記 ??點(diǎn)擊可跳轉(zhuǎn)
?? 個(gè)人專欄: ??數(shù)據(jù)結(jié)構(gòu)與算法??C語言進(jìn)階
?? 不能則學(xué),不知?jiǎng)t問,恥于問人,決無長進(jìn)
?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??
前言
我在上一篇博客中,詳細(xì)講解啦每一個(gè)函數(shù)的實(shí)現(xiàn)思路和代碼展現(xiàn),在這一篇博客中,我將像是做項(xiàng)目一樣,去實(shí)現(xiàn)順序表的總體實(shí)現(xiàn)。
一、項(xiàng)目源文件構(gòu)成
該項(xiàng)目由三部分組成
1?? 用來存放庫函數(shù),宏定義,函數(shù)申明等的一個(gè)頭文件:SqList.h
2?? 主函數(shù)的所在文件 test.c
3??各個(gè)函數(shù)的實(shí)現(xiàn),我們主要在此完成函數(shù)的代碼編寫:SqList.c
二、菜單
建立一個(gè)菜單是很重要的,菜單能夠?qū)崿F(xiàn)和用戶的交互,以便于用戶的操作。
代碼如下:
void meun()
{
printf("**************************************************\n");
printf("* 1.順序表尾插 2.順序表尾刪 *\n");
printf("* 3.順序表頭插 4.順序表頭刪 *\n");
printf("* 5.順序表的查找 6.在pos位置插入x *\n");
printf("* 7.刪除pos位置的值 8.順序表的打印 *\n");
printf("* 0.退出 *\n");
printf("**************************************************\n");
}
三、順序表結(jié)構(gòu)體
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* array;
int size;//當(dāng)前存儲(chǔ)個(gè)數(shù)
int capacity;//空間大小
}SL;
四、源文件展示
下面是整個(gè)項(xiàng)目的代碼:
1.SqList.h
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#define INT_SIZE 2
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* array;
int size;//當(dāng)前存儲(chǔ)個(gè)數(shù)
int capacity;//空間大小
}SL;
//順序表的初始化
void SeqListInit(SL* ps);
// 順序表尾插
void SeqListPushBack(SL* ps, SLDataType x);
// 順序表尾刪
void SeqListPopBack(SL* ps);
// 順序表頭插
void SeqListPushFront(SL* p, SLDataType x);
// 順序表頭刪
void SeqListPopFront(SL* p);
// 順序表查找
int SeqListFind(SL* p, SLDataType x);
// 順序表在pos位置插入x
void SeqListInsert(SL* p, size_t pos, SLDataType x);
// 順序表刪除pos位置的值
void SeqListErase(SL* p, size_t pos);
// 順序表銷毀
void SeqListDestory(SL* p);
// 順序表打印
void SeqListPrint(SL* p);
2.SqList.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"SqList.h"
//順序表的初始化
void SeqListInit(SL* ps)
{
ps->array = (SLDataType*)malloc(sizeof(SLDataType)*4);
if (ps->array == NULL)
{
perror("malloc failed\n");
exit(-1);
}
ps->size = 0;
ps->capacity = 4;
}
// 檢查空間,如果滿了,進(jìn)行增容
int CheckCapacity(SL* ps)
{
if (ps->size == ps->capacity)
{
SLDataType* tmp = (SLDataType*)realloc(ps->array, (ps->capacity + INT_SIZE)*sizeof(SLDataType));
if (tmp == NULL)
{
perror("擴(kuò)容失敗!\n");
return 0;
}
else
{
ps->array = tmp;
ps->capacity += INT_SIZE;
printf("擴(kuò)容成功\n");
return 1;
}
}
return 1;
}
// 順序表銷毀
void SeqListDestory(SL* ps)
{
free(ps->array);
ps->array = NULL;
ps->capacity = 0;
ps->size = 0;
}
// 順序表尾插
void SeqListPushBack(SL* ps, SLDataType x)
{
if (CheckCapacity(ps) == 0)
{
printf("空間已滿,插入失?。n");
}
if (CheckCapacity(ps) == 1)
{
ps->array[ps->size] = x;
ps->size ++;
}
}
// 順序表尾刪
void SeqListPopBack(SL* ps)
{
if (ps->size < 1)
{
printf("已經(jīng)為空,無元素可刪除\n");
return;
}
ps->array[ps->size - 1] = 0;
ps->size--;
}
// 順序表頭插
void SeqListPushFront(SL* ps, SLDataType x)
{
//判斷空間是否夠。
//先挪動(dòng)
if (CheckCapacity(ps) == 0)
{
printf("空間已滿,頭插入失?。n");
}
if (CheckCapacity(ps) == 1)
{
int end = ps->size - 1;
while (end>=0)
{
ps->array[end + 1] = ps->array[end];
end--;
}
ps->array[0] = x;
ps->size++;
}
}
// 順序表頭刪
void SeqListPopFront(SL* ps)
{
int n = 1;
while (n < ps->size)
{
ps->array[n - 1] = ps->array[n];
n++;
}
ps->size--;
}
// 順序表打印
void SeqListPrint(SL* ps)
{
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->array[i]);
}
printf("\n");
}
// 順序表查找
int SeqListFind(SL* ps, SLDataType x)
{
int i = 0,count=0;
for (i = 0; i < ps->size - 1; i++)
{
if (x == ps->array[i])
{
printf("找到啦!下標(biāo)是%d\n",i);
count++;
return i;
}
}
if (count == 0)
{
printf("沒找到!\n");
return;
}
}
// 順序表在pos位置插入x
void SeqListInsert(SL* ps, size_t pos, SLDataType x)
{
assert(pos>=0&&pos<ps->size);
if (CheckCapacity(ps) == 0)
{
printf("空間已滿,插入失??!\n");
}
if (CheckCapacity(ps) == 1)
{
int end = ps->size - 1 ;
while (end >= pos)
{
ps->array[end + 1] = ps->array[end];
end--;
}
ps->array[pos] = x;
ps->size++;
}
}
// 順序表刪除pos位置的值
void SeqListErase(SL* ps, size_t pos)
{
assert(pos >= 0 && pos < ps->size);
int n = pos + 1;
while (n <= ps->size - 1)
{
ps->array[n - 1] = ps->array[n];
n++;
}
ps->size--;
}
3.test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"SqList.h"
void meun()
{
printf("**************************************************\n");
printf("* 1.順序表尾插 2.順序表尾刪 *\n");
printf("* 3.順序表頭插 4.順序表頭刪 *\n");
printf("* 5.順序表的查找 6.在pos位置插入x *\n");
printf("* 7.刪除pos位置的值 8.順序表的打印 *\n");
printf("* 0.退出 *\n");
printf("**************************************************\n");
}
int main()
{
int x,pos;
SL s;
//初始化
SeqListInit(&s);
int input = 0;
do
{
meun();
printf("請(qǐng)輸入你的選擇:\n");
scanf("%d", &input);
switch (input)
{
case 1:
printf("請(qǐng)輸入要尾插的值:");
scanf("%d", &x);
SeqListPushBack(&s, x);
break;
case 2:
SeqListPopBack(&s);
break;
case 3:
printf("請(qǐng)輸入要頭插的值:");
scanf("%d", &x);
SeqListPushFront(&s,x);
break;
case 4:SeqListPopFront(&s);
break;
case 5:
printf("請(qǐng)輸入要查找的值:");
scanf("%d", &x);
SeqListFind(&s,x);
break;
case 6:
{
printf("請(qǐng)輸入要插入的位置和數(shù)據(jù)(空格分開):");
scanf("%d %d", &pos,&x);
SeqListInsert(&s, pos, x);
break;
}
case 7:
printf("請(qǐng)輸入要?jiǎng)h除的位置:");
scanf("%d", &pos);
SeqListErase(&s,pos);
break;
case 8:
SeqListPrint(&s);
break;
case 0:
SeqListDestory(&s);
break;
default:printf("輸入有誤,請(qǐng)重新輸入:\n");
}
}while (input);
}
五、運(yùn)行截圖
1.順序表尾插,頭插展示
2.順序表的頭刪
3.順序表的尾刪
4.順序表的查找
5.在pos位置插入x
6.在pos位置刪除元素
文章來源:http://www.zghlxwxcb.cn/news/detail-614541.html
總結(jié):
本次項(xiàng)目當(dāng)中遇到許多之氣沒有注意到的問題,尤其是數(shù)組越界問題等等,在接下來學(xué)習(xí)數(shù)據(jù)結(jié)構(gòu)預(yù)算法是非常重要的,??相信自己,踏踏實(shí)實(shí)走好每一步,夢(mèng)想終會(huì)成為現(xiàn)實(shí)! ??文章來源地址http://www.zghlxwxcb.cn/news/detail-614541.html
到了這里,關(guān)于【C語言數(shù)據(jù)結(jié)構(gòu)】模擬·順序表·總項(xiàng)目實(shí)現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!