有关线性表的基本操作

本文只有代码,介绍了有关线性表的基本操作。

已经过调试没有很大问题。
如有错误,还请批评指正。

头文件——结构体定义、函数声明:

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>

#define MaxSize 50

typedef int ElemType;
typedef struct {
	ElemType data[MaxSize];//数组
	int length;//数组长度
}SqList;

//声明函数
void InitList(SqList** L);//初始化线性表InitList(&L)  
void DestroyList(SqList** L);//销毁线性表DestroyList(&L) 
void ClearList(SqList** L);//重置线性表为空表ClearList(&L)
bool ListEmpty(SqList* L);//判定是否为空表ListEmpty(L) 
int  ListLength(SqList* L); //求线性表的长度ListLength(L)  
bool GetElem(SqList* L, int i, ElemType* e); //求某个数据元素值GetElem(L, i, &e) 
int  LocateElem(SqList* L, ElemType e); //按元素值查找LocateElem(L, e)
void PriorElem(SqList* L, ElemType cur_e, ElemType* pre_e);//返回该元素的前驱PriorElem(L,         
                                                           //cur_e, &pre_e)
void NextElem(SqList* L, ElemType cur_e, ElemType* next_e);//返回该元素的后继NextElem(L, 
                                                           //cur_e, &next_e)
bool ListInsert(SqList** L, int i, ElemType e);//插入数据元素ListInsert(&L, i, e)   
bool ListDelete(SqList** L, int i, ElemType* e); //删除数据元素ListDelete(&L, i, &e)  
void ListTraverse(SqList* L);//遍历线性表ListTraverse(L)
void DispList(SqList* L);//输出线性表DispList(L) 
void CreateList(SqList** L, ElemType a[], int n);//用数组创建线性表CreateList(&L, a, len)
void ListModifyElem(SqList** L, int i, ElemType e);//将线性表中第i个的元素值修改成参数e的值

测试主程序 :

#define _CRT_SECURE_NO_WARNINGS
#include"Basic_linear_list.h"

int main()
{
	SqList* A = NULL;
	ElemType a[10] = { 0,1,2,3,4,9,8,7,6,5 };
	ElemType e = 0;
	int index = 0;
	int len = sizeof(a) / sizeof(a[0]);
	CreateList(&A, a, len);
	DispList(A);//输出线性表内容
	printf("线性表的长度:%d\n", ListLength(A));//测试求长度

	if (GetElem(A, 3, &e))  //测试在范围内的情形
		printf("找到了第3个元素值为:%d\n", e);
	else
		printf("第3个元素超出范围!\n");

	if (GetElem(A, 15, &e))  //测试不在范围内的情形
		printf("找到了第15个元素值为:%d\n", e);
	else
		printf("第15个元素超出范围!\n");

	if ((index = LocateElem(A, 8)) > 0)  //测试能找到的情形
		printf("找到了,值为8的元素是第 %d 个\n", index);
	else
		printf("值为8的元素木有找到!\n");

	if ((index = LocateElem(A, 17)) > 0)  //测试不能找到的情形
		printf("找到了,值为17的元素是第 %d 个\n", index);
	else
		printf("值为17的元素木有找到!\n");

	ListInsert(&A, 1, 5);
	ListInsert(&A, 2, 3);
	ListInsert(&A, 1, 4);
	printf("插入元素后的序列为:\n");
	DispList(A);

	ListDelete(&A, 1, &e);
	ListDelete(&A, 1, &e);
	ListDelete(&A, 1, &e);
	printf("删除元素后的序列为:\n");
	DispList(A);

	PriorElem(A, 2, &e);
	printf("数字2的前驱元素是:%d\n", e);

	NextElem(A, 4, &e);
	printf("数字4的后继元素是:%d\n", e);

	ListModifyElem(&A, 1, 99);//修改线性表

	printf("输出当前的线性列表:\n");
	ListTraverse(A);

	ClearList(&A);//清空线性表

	DestroyList(&A);//摧毁线性表

	system("pause");
	return 0;
}

功能实现 :

#define _CRT_SECURE_NO_WARNINGS
#include"Basic_linear_list.h"
/*---------------------------------------------------------------------------------------
功能:初始化线性表
参数:1、线性表
输出:空
*/
//初始化线性表InitList(L)
void InitList(SqList** L) 
{
    //分配存放线性表的空间
    SqList* tmp = (SqList*)malloc(sizeof(SqList));
    if (tmp)
    {
        tmp->length = 0;
    }
    else
    {
        printf("InitList err");
        system("pause");
        return;
    }
    *L = tmp;
}
/*---------------------------------------------------------------------------------------
功能:用数组创建线性表
参数:1、线性表指针 2、元素类型数组 3、数组长度
输出:空
*/
//用数组创建线性表CreateList(&L, a, len)
void CreateList(SqList** L, ElemType a[], int n)
{
    if (L == NULL)
    {
        printf("CreateList err");
        system("pause");
        return;
    }
    int i = 0;
    SqList* tmp = (SqList*)malloc(sizeof(SqList));
    if (tmp)
    {
        for (i = 0; i < n; i++)
            tmp->data[i] = a[i];
        tmp->length = n;
    }
    else
    {
        printf("CreateList err");
        system("pause");
        return;
    }
    *L = tmp;
}
/*---------------------------------------------------------------------------------------
功能:输出线性表
参数:
输出:
*/
//输出线性表DispList(L)
void DispList(SqList* L)
{
    if (L == NULL)
    {
        printf("DispList err");
        system("pause");
        return;
    }
    int i = 0;
    if (ListEmpty(L))
    {
        printf("数据为空");
        system("pause");
        return;
    }
    for (i = 0; i < L->length; i++)
    {
        printf("%d ", L->data[i]);
    }
    printf("\n");
}
/*---------------------------------------------------------------------------------------
功能:判定是否为空表
参数:1、线性表
输出:Bool型-True(为空) false(不为空)
*/
//判定是否为空表ListEmpty(L)
bool ListEmpty(SqList* L)
{
    return(L->length == 0);
}
/*---------------------------------------------------------------------------------------
功能:求线性表的长度
参数:1、线性表
输出:整型(线性表的长度)
*/
//求线性表的长度ListLength(L)
int ListLength(SqList* L)
{
    if (L == NULL)
    {
        printf("ListLength err");
        system("pause");
        return -1;
    }
    return(L->length);
}
/*---------------------------------------------------------------------------------------
功能:求某个数据元素值
参数:1、线性表 2、元素位置 3、元素值
输出:Bool型-True(找到) false(没找到)
*/
//求某个数据元素值GetElem(L,i,e)
bool GetElem(SqList* L, int i, ElemType* e)
{
    if (L == NULL || e == NULL)
    {
        printf("GetElem err");
        system("pause");
        return -1;
    }
    if (i < 1 || i > L->length)
    {
        return false;
    }
    *e = L->data[i - 1];
    return true;
}
/*---------------------------------------------------------------------------------------
功能:按元素值查找
参数:1、线性表 2、元素值
输出:整型(元素位置)
*/
//按元素值查找LocateElem(L,e)
int LocateElem(SqList* L, ElemType e)
{
    if (L == NULL)
    {
        printf("LocateElem err");
        system("pause");
        return -1;
    }
    int i = 0;
    while (i < L->length && L->data[i] != e)
    {
        i++;
    }
    if (i >= L->length)
    {
        return 0;
    }
    else  return i + 1;
}
/*---------------------------------------------------------------------------------------
功能:插入数据元素
参数:1、线性表指针 2、插入位置 3、插入元素
输出:Bool型-True(成功插入) False(插入失败)
*/
//插入数据元素ListInsert(&L,i,e)
bool ListInsert(SqList** L, int i, ElemType e)
{
    if (L == NULL)
    {
        printf("ListInsert err");
        system("pause");
        return -1;
    }
    int j;
    if (i < 1 || i >(*L)->length + 1)
    {
        return false;   //参数错误时返回false
    }
    i--;            //将顺序表逻辑序号转化为物理序号
    for (j = (*L)->length; j > i; j--) //将data[i..n]元素后移一个位置
    {
        (*L)->data[j] = (*L)->data[j - 1];
    }
    (*L)->data[i] = e;           //插入元素e
    (*L)->length++;            //顺序表长度增1
    return true;            //成功插入返回true
}
/*---------------------------------------------------------------------------------------
功能:删除数据元素
参数:1、线性表指针 2、删除位置 3、删除元素
输出:Bool型-True(成功删除) False(删除失败)
*/
//删除数据元素ListDelete(&L,i,&e)
bool ListDelete(SqList** L, int i, ElemType* e)
{
    if (L == NULL || e == NULL)
    {
        printf("ListDelete err");
        system("pause");
        return -1;
    }
    int j;
    if (i < 1 || i >(*L)->length)  //参数错误时返回false
    {
        return false;
    }
    i--;        //将顺序表逻辑序号转化为物理序号
    *e = (*L)->data[i];
    for (j = i; j < (*L)->length - 1; j++) //将data[i..n-1]元素前移
    {
        (*L)->data[j] = (*L)->data[j + 1];
    }
    (*L)->length--;              //顺序表长度减1
    return true;              //成功删除返回true
}
/*---------------------------------------------------------------------------------------
功能:销毁线性表
参数:1、线性表指针
输出:空
*/
//销毁线性表DestroyList(&L)   
void DestroyList(SqList** L)
{
    if (L == NULL)
    {
        printf("DestroyList err");
        system("pause");
        return -1;
    }
    int length = (*L)->length;
    free(*L);
    printf("线性表被释放!表长度:%d\n", length);
}
/*---------------------------------------------------------------------------------------
功能:重置线性表为空表
参数:1、线性表指针
输出:空
*/
//重置线性表为空表ClearList(&L)
void ClearList(SqList** L)
{
    if (L == NULL)
    {
        printf("ClearList err");
        system("pause");
        return -1;
    }
    (*L)->length = 0;
    printf("线性表被清空!\n");
}
/*---------------------------------------------------------------------------------------
功能:返回该元素的前驱
参数:1、线性表 2、当前元素值 3、前驱元素值
输出:空
*/
//返回该元素的前驱PriorElem(L, cur_e, &pre_e)
void PriorElem(SqList* L, ElemType cur_e, ElemType* pre_e)
{
    if (L == NULL || pre_e == NULL)
    {
        printf("PriorElem err");
        system("pause");
        return -1;
    }
    int i = 0;
    for (i = 0; i < L->length; i++)
    {
        if (L->data[i] == cur_e)
        {
            break;
        }
    }
    if (i == 0 || i == L->length)
    {
        printf("该元素木有前驱!\n");
        return;
    }
    *pre_e = (L->data)[i - 1];
}
/*---------------------------------------------------------------------------------------
功能:返回该元素的后继
参数:1、线性表 2、当前元素值 3、后驱元素值
输出:空
*/
void NextElem(SqList* L, ElemType cur_e, ElemType* next_e)
{
    if (L == NULL || next_e == NULL)
    {
        printf("NextElem err");
        system("pause");
        return -1;
    }
    int i = 0;
    for (i = 0; i < L->length; i++)
    {
        if (L->data[i] == cur_e)
        {
            break;
        }
    }
    if (i == L->length - 1 || i == L->length)
    {
        printf("该元素木有后继!\n");
        return;
    }
    *next_e = (L->data)[i + 1];
}
/*---------------------------------------------------------------------------------------
功能:遍历线性表,执行输出功能
参数:1、线性表
输出:空
*/
//遍历线性表ListTraverse(L)
void ListTraverse(SqList* L)
{
    if (L == NULL)
    {
        printf("ListTraverse err");
        system("pause");
        return -1;
    }
    int i = 0;
    for (i = 0; i < L->length; i++)
    {
        printf("%d ", L->data[i]);
    }
    printf("\n");
}
/*---------------------------------------------------------------------------------------
功能:修改线性表元素值
参数:1、线性表指针 2、修改元素位置 3、修改后的值
输出:空
*/
//将线性表中第i个的元素值修改成参数e的值
void ListModifyElem(SqList** L, int i, ElemType e)
{
    if (L == NULL)
    {
        printf("ListModifyElem err");
        system("pause");
        return -1;
    }
    if (i < 1 || i >(*L)->length)
    {
        printf("没找到该位置,修改失败");
        return;
    }
    (*L)->data[i - 1] = e;
}

猜你喜欢

转载自blog.csdn.net/absorb2601078490/article/details/124870581