#include "stdafx.h"
#include <iostream>
using namespace std;
typedef int ElemType;
typedef int Status;
#define INIT_LIST_SIZE 100
#define LIST_INCREMENT 10
struct Linear {
ElemType *elem; //指向基地址,未赋值时其为NULL
ElemType length; //当前列表长度
ElemType listsize; //当前存储长度
};
Linear list1, list2, list3; //Linear是一种自定义的类型,list1、list2都是变量
bool initList(Linear &L) { //传递引用
//构造线性表
L.elem = (ElemType *)malloc(INIT_LIST_SIZE * sizeof(Linear)); //开辟了100*ElemType的长度的存储空间,并将首地址返回给L.elem
if (!L.elem) exit(1); //L.elem的长度为0,退出程序,程序返回的返回码是OVERFLOW
L.length = 0; //空表长度为0
L.listsize = INIT_LIST_SIZE;
return true;
}
//判断线性表是否为空
bool isEmpty(Linear &L) {
if (!L.elem)
{
cout << "该线性表不存在!" << endl;
exit(1);
}
if (L.length == 0)
return true;
else
return false;
}
//在第i个之前插入元素
bool insertList(Linear &L, ElemType i, ElemType a) { //传引用,传递的是地址
ElemType *newbase;
if ((i < 1) || (i > L.length+1 )) return false;
if (L.length>=L.listsize)
{
//realloc函数重新分配内存,返回新分配内存的首地址,新分配内存的首地址和原地址相同。
newbase = (ElemType *)realloc(L.elem,(L.listsize+LIST_INCREMENT)*sizeof(Linear));
if (!newbase) exit(1); //存储分配失败!
L.elem = newbase;
L.listsize += LIST_INCREMENT;
}
//元素后移
ElemType *p, *q;
q = &(L.elem[i - 1]);
for (p = &(L.elem[L.length - 1]); p >= q; --p) //因为为顺序存储结构,这里可以进行地址的比较
{
*(p + 1) = *p;
}
*q = a;
++L.length;
return true;
}
void visit(ElemType e) {
cout << e << " ";
}
//输出线性表
void listTraverse(Linear &L) {
if (!L.elem)
{
exit(1);
}
if (isEmpty(L))
{
cout << "该线性表为空!" << endl;
exit(0);
}
for (ElemType i = 0; i < L.length; i++) {
visit(L.elem[i]);
}
cout << endl;
}
//删除元素
bool List_Delete(Linear &L, ElemType i) {
if ((i < 1) || (i > L.length)) return false;
//元素前移
ElemType *p, *q;
p = &(L.elem[i - 1]);
q = &(L.elem[L.length-1]);
for (p; p<q; p++)
{
*p = *(p+1);
}
--L.length;
return true;
}
//修改元素
bool modifyList(Linear &L, ElemType i, ElemType a) {
if ((i < 1) || (i > L.length)) return false;
ElemType *p;
p = &(L.elem[i - 1]);
*p = a;
return true;
}
//查找某一元素在线性表中的位置
Status getLocate(Linear &L, ElemType a) {
if (isEmpty(L))
{
cout << "该线性表为空!"<<endl;
exit(0);
}
ElemType j=1;
while (j <= L.length) {
if (L.elem[j-1] == a) break;
else
{
++j;
}
}
if (j <= L.length)
return j;
else
return false;
}
//合并两个线性表
Linear mergeList(Linear &L1, Linear &L2, Linear &L3) {
for (ElemType i = 0; i < L1.length; i++) //复制第一个线性表
{
L3.elem[i] = L1.elem[i];
}
for (ElemType i = 0; i < L2.length; i++)
{
L3.elem[i + L1.length] = L2.elem[i];
}
return L3;
}
int main()
{
initList(list1); //初始化线性表
cout << "初始化后线性表长度: " << list1.length << endl;
cout << "在list1中插入元素2后list1为:" ;
insertList(list1, 1, 2);
listTraverse(list1);
cout << "在list1中的1改为3" << endl;;
modifyList(list1, 1, 3);
cout << "在list1中第二个位置插入5" << endl;
insertList(list1, 2, 5);
cout << "在list1中第二个位置插入4" << endl;
insertList(list1, 2, 4);
cout << "遍历list1:";
listTraverse(list1);
cout << "在list1中5的位置为:";
cout << getLocate(list1, 5) << endl;
cout << "删除list1中第二个元素" << endl;;
List_Delete(list1, 2);
cout << "遍历list1:";
listTraverse(list1);
cout <<"lsit1的长度为:"<< list1.length << endl;
//初始化第二个链表
cout << "初始化第二个链表" << endl;
initList(list2);
cout << "在list2中插入元素7,8后list1为:";
insertList(list2, 1, 7);
insertList(list2, 2, 8);
listTraverse(list2);
//合并两个链表'
cout << "初始化list3:"<<endl;
initList(list3);
cout << "合并list1、list2为list3" << endl;
list3.length = list1.length + list2.length;
cout << "list3的长度为:" << list3.length << endl;
mergeList(list1, list2, list3);
cout << "合并后的线性表为: ";
listTraverse(list3);
system("pause");
return 0;
}
线性表初始化、判空、插入、删除、遍历、修改、查值、合并
最新推荐文章于 2021-05-22 16:26:33 发布