数据结构三要素
对线性表的操作:创建、删除、增、删、改、查。
//动态分配内存空间线性表
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define max 51 //数据项最大存储空间
//线性表最大
#define initSize 100
//定义 People结构,这是线性表中存储的数据类型
struct People
{
char name[max];
char ID[max];
};
//定义,并且声明 List结构
typedef struct
{
People* people;//第一个元素的指针
int length; //线性表的大小
}List;
void initList(List &list);//线性表初始化
bool add(List& list, People people);//表尾添加元素
bool insert(List& list,int i , People people );//在第i角标处插入元素
bool del(List& list, int i);//删除第i个元素
bool modify(List& list, People initialPeople , People targetPeople);//修改元素
int lookup(List list, People people);//查找元素
bool JudgeEq(People peo1 ,People peo2);//判断两个字符数组是否相等
void main()
{
List list;
initList(list);//线性表初始化
People peopel[3] = { {"李参政" , "0418 " } , {"参政", "0418"} , {"政" , "0418"} };
//添加元素
for (int i = 0 ; i < 3 ; i++)
{
add(list, peopel[i]);
}
for (int i = 1 ; i <= list.length ; i++)
{
printf("%s\t %s\t %d \n " , list.people[i].name , list.people[i].ID , list.length);
}
printf("---------------------------------------------\n");
People peo1 = { "李参政" ,"0418" };
//查找元素,输出其所在位置
printf("%d\n" , lookup(list, peo1 ));
printf("---------------------------------------------\n");
People peo2 = { "李参政" ,"0419" };
//在特定位置插入元素
insert(list, 2, peo2);
for (int i = 1; i <= list.length; i++)
{
printf("%s\t %s\t %d \n ", list.people[i].name, list.people[i].ID, list.length);
}
printf("--------------------------------------------\n");
People peo3 = { "李参政" ,"0420" };
//修改元素值
modify(list, peo1, peo3);
for (int i = 1; i <= list.length; i++)
{
printf("%s\t %s\t %d \n ", list.people[i].name, list.people[i].ID, list.length);
}
printf("---------------------------------------------\n");
//删除特定位置元素
del(list, 1);
for (int i = 1; i <= list.length; i++)
{
printf("%s\t %s\t %d \n ", list.people[i].name, list.people[i].ID, list.length);
}
printf("---------------------------------------------\n");
free(list.people);
}
//线性表初始化
void initList(List& list)
{
list.people = (People*)malloc(sizeof(People) * initSize);//动态分配initSize个大小与People相等的空间
list.length = 0;//初始化线性表,表长为0
}
bool add(List& list, People people)//表尾添加元素
{
//线性表满
if (list.length >= initSize)
return false;
list.people[list.length + 1] = people;
++list.length ;
return true;
}
//在第i角标处插入元元素
bool insert(List& list, int i, People people)
{
if (list.length >= initSize)//线性表已满
return false;
if (i > list.length || i < 1)//i无效
return false;
//第i个及其以后的元素全部向后移动一位
for (int j = list.length; j >= i; j--)
{
list.people[j+1] = list.people[j];
}
list.people[i] = people;//加入元素
++list.length;//表长加一
return true;
}
//删除下角标为i的元素
bool del(List& list, int i)//删除第i个元素
{
if (list.length ==0 )//线性表为空
return false;
if (i > list.length || i < 1)//i无效
return false;
//删除第i个元素,其后面元素全部向前移动一位
for (int j = i; j < list.length; j++)
{
list.people[j] = list.people[j + 1 ];
}
//表长减一
--list.length;
return true;
}
//查找元素
int lookup(List list, People people)//查找元素
{
for (int i = 1; i <= list.length; i++)
{
if (JudgeEq(list.people[i] , people))
return i;
}
return 0;
}
//修改元素值
bool modify(List& list, People initialPeople, People targetPeople)
{
if (list.length == 0)
return false;
for (int i = 1 ; i <= list.length ; i++ )
{
if (JudgeEq(list.people[i], initialPeople))
{
list.people[i] = targetPeople;
return true;
}
}
return 0;
}
//判断两个people 是否相等
bool JudgeEq(People peo1, People peo2)
{
//strcmp(str1 , str2)判断两个字符串是否相等,若相等则输出0
//判断两个People是否相等
if (strcmp(peo1.name, peo2.name) == 0 && strcmp(peo1.ID, peo2.ID) == 0)
return true;
}