要求:使用封装工程代码完成顺序表的定义、创建、添加、显示、插入、查找、替换、删除、清空、销毁等功能。
其定义为:
(1)创建顺序表:
seqlist_t *create_seqlist(int len);
(2)顺序表添加元素:
int append_seqlist(seqlist_t *ptmplist, datatype tmpdata)
(3)顺序表元素显示
int show_seqlist(seqlist_t *ptmplist)
(4)指定位置插入元素
int insert_pos_seqlist(seqlist_t *ptmplist, int pos, datatype tmpdata)
(5)顺序表元素查找
int delete_seqlist(seqlist_t *ptmplist, datatype tmpdata)
(6)顺序表元素替换
int replace_seqlist(seqlist_t *ptmplist, datatype olddata, datatype newdata)
(7)删除顺序表
int delete_seqlist(seqlist_t *ptmplist, datatype tmpdata)
(8)清空顺序表
int clear_seqlist(seqlist_t *ptmplist)
(9)销毁顺序表
int destroy_seqlist(seqlist_t *ptmplist)
要实现以下函数需要创建3个文件,分别为:main.c , seqlist.c , seqlist.h 。
使用此格式可以更方便阅读和编写。
主函数 main.c ↓↓↓↓↓
#include<stdio.h>
#include"seqlist.h"
int main(void)
{
seqlist_t *plist = NULL;//定义顺序表plist。
plist = create_seqlist(10);//顺序表内有10个元素。
append_seqlist(plist, 1);//对顺序表赋值1~5。
append_seqlist(plist, 2);
append_seqlist(plist, 3);
append_seqlist(plist, 4);
append_seqlist(plist, 5);
if (IS_EMPTY_SEQLIST(plist))//检测顺序表是否写入成功。
{
printf("顺序表为空\n");
}
else
{
printf("顺序表不为空\n");
}
printf("插入后:\n");
insert_pos_seqlist(plist, 0, 0);//在第0处插入元素0。
append_seqlist(plist, 5);//在顺序表后面添加元素5.
show_seqlist(plist);//打印显示顺序表内元素。
printf("\n");
printf("删除后:\n");
delete_seqlist(plist, 5);//删除元素5.
show_seqlist(plist);//打印显示顺序表内元素。
printf("\n");
is_exist_seqlist(plist, 2);//查找元素2所在位置。
replace_seqlist(plist, 2, 1);//替换元素2为元素1。
show_seqlist(plist);//打印显示顺序表内元素。
printf("\n");
clear_seqlist(plist);//清空顺序表
if (IS_EMPTY_SEQLIST(plist))
{
printf("顺序表为空\n");
}
else
{
printf("顺序表不为空\n");
}
printf("清空后:\n");
show_seqlist(plist);//打印显示顺序表内元素。
printf("\n");
destroy_seqlist(plist);//销毁顺序表
return 0;
}
封装函数 seqlist.c ↓↓↓↓↓
#include<stdio.h>
#include"seqlist.h"
#include<string.h>
#include<stdlib.h>
/**************************
*函數名:create_seqlist
*功 能:創建順序表
*參 數:
* len:順序表存放元素個數
*返回值:
* 成功返回順序表首地址;
* 失敗返回NULL;
*
*注意事項:
* 無;
**************************/
seqlist_t *create_seqlist(int len)
{
seqlist_t *ptmplist = NULL;
ptmplist = malloc(sizeof(seqlist_t));
if(NULL == ptmplist)
{
return NULL;
}
ptmplist->pdata = malloc(len * sizeof(datatype));
if(NULL == ptmplist->pdata)
{
return NULL;
}
ptmplist->tlen = len;
ptmplist->clen = 0;
memset(ptmplist->pdata, 0, len * sizeof(datatype));
return ptmplist;
}
/**************************
*函數名:append_seqlist
*功 能:順序表添加元素
*參 數:
* ptmplist:順序表地址;
* tmpdata:要添加的元素;
*返回值:
* 成功返回 0;
* 失敗返回 -1;
*
*注意事項:
* 無;
**************************/
int append_seqlist(seqlist_t *ptmplist, datatype tmpdata)
{
if(IS_FULL_SEQLIST(ptmplist))
{
return -1;
}
ptmplist->pdata[ptmplist->clen] = tmpdata;
(ptmplist->clen)++;
return 0;
}
/**************************
*函數名:show_seqlist
*功 能:顯示順序表
*參 數:
* ptmplist:順序表地址
*返回值:
* 成功返回 0;
*注意事項:
* 無;
**************************/
int show_seqlist(seqlist_t *ptmplist)
{
int i = 0;
for(i = 0; i < ptmplist->clen; i++)
{
printf("%d ", ptmplist->pdata[i]);
}
printf("\n");
return 0;
}
/**************************
*函數名:insert_pos_seqlist
*功 能:指定位置插入元素
*參 數:
* ptmplist:順序表地址;
* pos:插入位置;
* tmpdata:要插入的元素;
*返回值:
* 成功返回 0;
* 失敗返回
* -1(順序表已滿);
* -2(插入位置錯誤);
*注意事項:
* 無;
**************************/
int insert_pos_seqlist(seqlist_t *ptmplist, int pos, datatype tmpdata)
{
int i = 0;
if(IS_FULL_SEQLIST(ptmplist))
{
return -1;
}
if(pos < 0 || pos > ptmplist->clen)
{
return -2;
}
for(i = ptmplist->clen; i > pos; i--)
{
ptmplist->pdata[i] = ptmplist->pdata[i-1];
}
ptmplist->pdata[pos] = tmpdata;
(ptmplist->clen)++;
return 0;
}
/**************************
*函數名:delete_seqlist
*功 能:刪除指定元素
*參 數:
* ptmplist:順序表地址;
* tmpdata:要刪除的元素;
*返回值:
* 成功返回 0;
* 失敗返回
* -1(順序表空);
*注意事項:
* 無;
**************************/
int delete_seqlist(seqlist_t *ptmplist, datatype tmpdata)
{
int i = 0;
int n = 0;
if(IS_EMPTY_SEQLIST(ptmplist))
{
return -1;
}
for(i = 0; i < ptmplist->clen; i++)
{
while(tmpdata == ptmplist->pdata[i] && i < ptmplist->clen)
{
for(n = i; n < ptmplist->clen-1; n++)
{
ptmplist->pdata[n] = ptmplist->pdata[n+1];
}
(ptmplist->clen)--;
}
}
return 0;
}
/**************************
*函數名:clear_seqlist
*功 能:清空順序表
*參 數:
* ptmplist:順序表地址;
*返回值:
* 成功返回 0;
*注意事項:
* 無;
**************************/
int clear_seqlist(seqlist_t *ptmplist)
{
ptmplist->clen = 0;
return 0;
}
/**************************
*函數名:destroy_seqlist
*功 能:銷燬順序表
*參 數:
* ptmplist:順序表地址;
*返回值:
* 成功返回 0;
*注意事項:
* 無;
**************************/
int destroy_seqlist(seqlist_t *ptmplist)
{
free(ptmplist->pdata);
free(ptmplist);
return 0;
}
/**************************
*函數名:is_exist_seqlist
*功 能:順序表元素查找
*參 數:
* ptmplist:順序表地址;
* tmpdata:要查找的元素;
*返回值:
* 成功返回 0;
* 失敗返回 -1;
*注意事項:
* 無;
**************************/
c
{
int i = 0;
int n = 0;
for(i = 0; i < ptmplist->clen; i++)
{
if(tmpdata == ptmplist->pdata[i])
{
printf("此元素在第 %d 個\n", i);
n++;
}
}
if(n == 0)
{
printf("未找到此元素\n");
return -1;
}
return 0;
}
/**************************
*函數名:is_exist_seqlist
*功 能:順序表元素替换
*參 數:
* ptmplist:順序表地址;
* olddata:要替換的老元素;
* newdata;要替換的新元素;
*返回值:
* 成功返回 0;
* 失敗返回 -1;
*注意事項:
* 無;
**************************/
int replace_seqlist(seqlist_t *ptmplist, datatype olddata, datatype newdata)
{
int i = 0;
int n = 0;
for(i = 0; i < ptmplist->clen; i++)
{
if(olddata == ptmplist->pdata[i])
{
ptmplist->pdata[i] = newdata;
n++;
}
}
if(n == 0)
{
printf("未找到此 %d 元素\n", olddata);
return -1;
}
printf("替換完成\n");
return 0;
}
结构体函数 seqlist.h ↓↓↓↓↓
typedef int datatype;
typedef struct seqlist
{
datatype *pdata;
int tlen;
int clen;
}seqlist_t;
#define IS_FULL_SEQLIST(ptmplist) (ptmplist->tlen == ptmplist->clen ? 1 : 0)//定义宏
#define IS_EMPTY_SEQLIST(ptmplist) (0 == ptmplist->clen ? 1 : 0)//定义宏
//声明函数
extern seqlist_t *create_seqlist(int len);
extern int append_seqlist(seqlist_t *ptmplist, datatype tmpdata);
extern int show_seqlist(seqlist_t *ptmplist);
extern int insert_pos_seqlist(seqlist_t *ptmplist, int pos, datatype tmpdata);
extern int delete_seqlist(seqlist_t *ptmplist, datatype tmpdata);
extern int clear_seqlist(seqlist_t *ptmplist);
extern int destroy_seqlist(seqlist_t *ptmplist);
extern int is_exist_seqlist(seqlist_t *ptmplist, datatype tmpdata);
extern int replace_seqlist(seqlist_t *ptmplist, datatype olddata, datatype newdata);
以上就是顺序表的基本结构,同学们可以参照此文章练习理解。
1539

被折叠的 条评论
为什么被折叠?



