您的位置:首页 > 编程语言 > C语言/C++

学习笔记——线性表的动态分配顺序存储结构基本操作(C语言实现)

2018-02-12 15:54 766 查看
相关资料:
(1)http://blog.csdn.net/zhengnianli/article/details/79174773
(2)http://blog.csdn.net/flueky/article/details/52711668/*----------------------------------------------------------------------------------------

Program Explain:线性表的静态分配顺序存储结构基本操作
Create Date:2018.2.12 by lzn

----------------------------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>

#define ERROR 1
#define OK 0

//顺序表的结构定义
typedef struct Sqlist
{
int *data; //声明一个动态数组
int length; //表的长度
int size; //存储容量,基本单位为sizeof(int)
}Sqlist;

//操作函数声明
Sqlist InitList(int maxsize); //初始化顺序表
void PrintfList(Sqlist L); //打印顺序表
int Serch(Sqlist L, int elem); //在表中寻找元素的位置
Sqlist ReplaceByPos(Sqlist L, int pos, int elem); //替换第pos个位置的元素成elem
Sqlist ReplaceByElem(Sqlist L,int oldElem,int newElem);//替换oldElem元素w为newElem
Sqlist Insert(Sqlist L, int pos, int elem); //在表的pos位置插入元素elem
Sqlist Delete(Sqlist L,int pos); //删除pos位置元素
int MenuSelect(void);
//测试函数声明
void Test1(Sqlist L); //测试"Serch"函数
void Test2(Sqlist L); //测试"ReplaceByPos"函数
void Test3(Sqlist L); //测试"ReplaceByElem"函数
void Test4(Sqlist L); //测试"Insert"函数
void Test5(Sqlist L); //测试"Delete"函数
Sqlist Test6(void); //测试"InitList"函数

/*********************************************************************************
* Function Name : main主函数
* Parameter : NULL
* Return Value : 0
* Function Explain :
* Create Date : 2018.2.12 by lzn
**********************************************************************************/
int main(void)
{
int ma
bd31
xsize = 0;
int cmd;
Sqlist L;

printf("Please input list length:");
scanf("%d",&maxsize);
L = InitList(maxsize);
PrintfList(L);
while(1)
{
cmd = MenuSelect();
switch(cmd)
{
case 1: Test1(L); break; //测试"Serch"函数
case 2: Test2(L); break; //测试"ReplaceByPos"函数
case 3: Test3(L); break; //测试"ReplaceByElem"函数
case 4: Test4(L); break; //测试"Insert"函数
case 5: Test5(L); break; //测试"Delete"函数
case 6: L=Test6(); break; //测试"InitList"函数
case 7: system("cls"); break; //清屏
case 8: exit(0); break; //退出
}
}
return 0;
}

/*********************************************************************************
* Function Name : InitList,初始化顺序表
* Parameter : maxsize:顺序表最大存储容量
* Return Value : 初始化成功的顺序表
* Function Explain :
* Create Date : 2018.2.12 by lzn
**********************************************************************************/
Sqlist InitList(int maxsize)
{
Sqlist L;

L.data = (int*)malloc(maxsize*sizeof(int));
if(!L.data) //如果申请失败,作出提示并直接退出程序
{
printf("%s:Init error!\n",__FUNCTION__);
exit(0);
}
L.length = 0;
L.size = maxsize;
for(int i=1;i<=maxsize;i++)
{
L.data[i-1] = i;
L.length++;
}

return L;
}

/*********************************************************************************
* Function Name : PrintfList,打印顺序表
* Parameter : L:要打印的顺序表
* Return Value : NULL
* Function Explain :
* Create Date : 2018.2.12 by lzn
**********************************************************************************/
void PrintfList(Sqlist L)
{
printf("List length is %d\n",L.length);
for(int i=1;i<=L.length;i++)
{
printf("%d\t",L.data[i-1]);
if(i%10==0) //每10个元素作为一行
{
printf("\n");
}
}
printf("\n");
}

/*********************************************************************************
* Function Name : Serch,顺序表的搜索操作
* Parameter : L:顺序表 elem:要搜索的元素
* Return Value : pos:搜索到的元素的位置 ERROR:elem不在顺序表L中
* Function Explain :
* Create Date : 2018.2.12 by lzn
**********************************************************************************/
int Serch(Sqlist L, int elem)
{
int pos = 0;

for(int i=0;i<L.length;i++)
{
if(elem==L.data[i])
{
pos = i + 1;
printf("The %d position in the list is %d\n",elem,pos);
return pos;//返回elem元素在顺序表中的位置
}
}
printf("Serch error!\n");
return ERROR; //搜索失败
}

/*********************************************************************************
* Function Name : ReplaceByPos,顺序表的替换操作(通过位置的方式替换)
* Parameter : L:顺序表 pos:要替换的位置 elem:要替换的元素
* Return Value : 替换后的顺序表
* Function Explain :
* Create Date : 2018.2.12 by lzn
**********************************************************************************/
Sqlist ReplaceByPos(Sqlist L, int pos, int elem)
{
L.data[pos-1] = elem;
return L;
}

/*********************************************************************************
* Function Name : ReplaceByElem,顺序表的替换操作(通过元素查找的方式替换)
* Parameter : L:顺序表 pos:要替换的位置 elem:要替换的元素
* Return Value : 执行替换操作后的顺序表
* Function Explain :
* Create Date : 2018.2.12 by lzn
**********************************************************************************/
Sqlist ReplaceByElem(Sqlist L,int oldElem,int newElem)
{
int pos = Serch(L, oldElem);
L.data[pos-1] = newElem;

return L;
}

/*********************************************************************************
* Function Name : Insert,顺序表的插入操作
* Parameter : L:顺序表 pos:要插入的位置 elem:要插入的元素
* Return Value : 执行插入操作后的顺序表
* Function Explain :
* Create Date : 2018.2.12 by lzn
**********************************************************************************/
Sqlist Insert(Sqlist L, int pos, int elem)
{
/* 插入的位置不存在 */
if( (pos>L.length+1)||(pos<1) )
{
printf("%s:Insert position error!\n",__FUNCTION__);
return L;
}
L.data = (int*)realloc(L.data, (L.size+1)*sizeof(int));
if(!L.data) //如果申请失败,作出提示并直接退出程序
{
printf("%s:Init error!\n",__FUNCTION__);
exit(0);
}
L.size++;
L.length++;
/* 从插入位置开始,后面的元素依次往后移动一个元素长度,要从最
右边的元素开始移,若从左边开始,则右边的元素会被左边的覆盖*/
for(int i=L.length-1;i>=pos-1;i--)
{
L.data[i+1] = L.data[i];
}
L.data[pos-1] = elem;

return L;
}

/*********************************************************************************
* Function Name : Delete,顺序表的删除操作
* Parameter : L:顺序表 pos:要删除的位置
* Return Value : 执行删除操作后的顺序表
* Function Explain :
* Create Date : 2018.2.12 by lzn
**********************************************************************************/
Sqlist Delete(Sqlist L,int pos)
{
/* 删除的位置不存在 */
if( (pos>L.length)||(pos<1) )
{
printf("%s:Delete position error!\n",__FUNCTION__);
return L;
}
/* 从删除位置开始,后面的元素依次往前移动一个元素的单位 */
for(int i=pos-1;i<=L.length-1;i++)
{
L.data[i] = L.data[i+1];
}
L.length--;

return L;
}

/*********************************************************************************
* Function Name : MenuSelect,菜单
* Parameter : void
* Return Value : cmd
* Function Explain :
* Create Date : 2018.2.12 by lzn
**********************************************************************************/
int MenuSelect(void)
{
int cmd;

printf("1.Serch test\n");
printf("2.ReplaceByPos test\n");
printf("3.ReplaceByElem test\n");
printf("4.Insert test\n");
printf("5.Delete test\n");
printf("6.Init test\n");
printf("7.Clear\n");
printf("8.Exit\n");
do
{
printf("Enter your choice: ");
scanf("%d",&cmd);
}while(cmd<0||cmd>8);

return cmd;
}

/* ====================================以下是测试函数============================== */
/*********************************************************************************
* Function Name : Test1, 测试"Serch"函数
* Parameter : L:顺序表
* Return Value : void
* Function Explain :
* Create Date : 2018.2.12 by lzn
**********************************************************************************/
void Test1(Sqlist L)
{
int serchElem = 0; //存储要搜索的元素

printf("--------------------%s start!--------------------\n",__FUNCTION__);
printf("Please input the element you want to serch:");
scanf("%d",&serchElem);
Serch(L,serchElem);
printf("--------------------%s end!--------------------\n",__FUNCTION__);
printf("\n");
}

/*********************************************************************************
* Function Name : Test2, 测试"ReplaceByPos"函数
* Parameter : L:顺序表
* Return Value : void
* Function Explain :
* Create Date : 2018.2.12 by lzn
**********************************************************************************/
void Test2(Sqlist L)
{
int replacePos = 0, replaceElem = 0; //存储替换的位置,替换的元素

printf("--------------------%s start!--------------------\n",__FUNCTION__);
printf("Please input the position and the element you want replace(example:10,33):");
scanf("%d,%d",&replacePos,&replaceElem);
L = ReplaceByPos(L,replacePos,replaceElem);
printf("After replace by position,list is:\n");
PrintfList(L);
printf("--------------------%s end!--------------------\n",__FUNCTION__);
printf("\n");
}

/*********************************************************************************
* Function Name : Test3, 测试"ReplaceByElem"函数
* Parameter : L:顺序表
* Return Value : void
* Function Explain :
* Create Date : 2018.2.12 by lzn
**********************************************************************************/
void Test3(Sqlist L)
{
int oldElem = 0, newElem = 0; //存储旧的元素,新的元素

printf("--------------------%s start!--------------------\n",__FUNCTION__);
printf("Please input the oldElem and the newElem you want replace(example:10,33):");
scanf("%d,%d",&oldElem,&newElem);
L = ReplaceByElem(L, oldElem, newElem);
printf("After replace by element,list is:\n");
PrintfList(L);
printf("--------------------%s end!--------------------\n",__FUNCTION__);
printf("\n");
}

/*********************************************************************************
* Function Name : Test4, 测试"Insert"函数
* Parameter : L:顺序表
* Return Value : void
* Function Explain :
* Create Date : 2018.2.12 by lzn
**********************************************************************************/
void Test4(Sqlist L)
{
int insertPos = 0, insertElem = 0; //存储插入的位置,插入的元素

printf("--------------------%s start!--------------------\n",__FUNCTION__);
printf("Please input the position and the element you want insert(example:10,33):");
scanf("%d,%d",&insertPos,&insertElem);
L = Insert(L,insertPos,insertElem);
printf("After insert,list is:\n");
PrintfList(L);
printf("--------------------%s end!--------------------\n",__FUNCTION__);
printf("\n");
}

/*********************************************************************************
* Function Name : Test5, 测试"Delete"函数
* Parameter : L:顺序表
* Return Value : void
* Function Explain :
* Create Date : 2018.2.12 by lzn
**********************************************************************************/
void Test5(Sqlist L)
{
int deletePos = 0; //存储要删除的位置

printf("--------------------%s start!--------------------\n",__FUNCTION__);
printf("Please input the position of the element you want to delete(example:10):");
scanf("%d",&deletePos);
L = Delete(L,deletePos);
printf("After delete,list is:\n");
PrintfList(L);
printf("--------------------%s end!--------------------\n",__FUNCTION__);
printf("\n");
}

/*********************************************************************************
* Function Name : Test6, 测试"Init"函数
* Parameter : void
* Return Value : 初始化成功的顺序表
* Function Explain :
* Create Date : 2018.2.12 by lzn
**********************************************************************************/
Sqlist Test6(void)
{
Sqlist L;
int maxsize = 0;

printf("--------------------%s start!--------------------\n",__FUNCTION__);
scanf("%d",&maxsize);
L = InitList(maxsize);
PrintfList(L);
printf("--------------------%s end!--------------------\n",__FUNCTION__);
printf("\n");

return L;
}程序运行结果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐