• 动态顺序表C++实现


    Sqlist.h头文件

    #include 
    #include 
    #include 
    #include 
    #include 
    using namespace std;
    #define SIZE 10
    typedef int ElemType;
    
    typedef struct SqList
    {
    	ElemType* data;
    	int size;
    	int capacity;//数组空间大小
    }SL;
    
    //初始化
    void InitList(SL& L);
    
    //扩容
    void ListNewCapacity(SL& L);
    
    //数据输入到顺序表
    void DataEntry(SL& L);
    
    //打印链表
    void PrintList(const SL& L);
    
    //取数据
    void GetElem(const SL& L, int i, ElemType& e);
    
    //查找数据
    int LocateElem(const SL& L, ElemType e);
    
    //插入数据
    void ListInsert(SL& L, int i, ElemType e);
    
    //修改数据
    void ListRevise(SL& L, int i, ElemType e);
    
    //删除数据
    void ListDelete(SL& L, int i);
    
    //销毁顺序表
    void DestroyList(SL& L);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    SqList.cpp功能函数实现

    #include "SqList.h"
    
    //初始化
    void InitList(SL& L)
    {
    	L.data = NULL;
    	L.size = 0;
    	L.capacity = 0;
    }
    
    //扩容
    void ListNewCapacity(SL& L)
    {
    	if (L.size == L.capacity)
    	{
    		int NewCapacity = L.capacity == 0 ? SIZE : L.capacity * 2;
    		ElemType* ret = (ElemType*)realloc(L.data, NewCapacity * sizeof(ElemType));
    		if (ret == NULL)
    		{
    			cout << strerror(errno) << endl;//创建失败打印错误信息
    			exit(-1);
    		}
    		else
    		{
    			L.data = ret;
    			L.capacity = NewCapacity;
    		}
    	}
    }
    
    //数据输入到顺序表
    void DataEntry(SL& L)
    {
    	ListNewCapacity(L);
    	int i = 0;
    	int n = 0;
    	ElemType e;
    	cout << "请输入要顺序表中存放数据个数: ";
    	cin >> n;
    	for (i; i < n; i++)
    	{
    		cin >> e;
    		L.data[i] = e;
    		L.size++;
    	}
    
    }
    
    //打印链表
    void PrintList(const SL& L)
    {
    	if (L.size == 0)
    	{
    		cout << "顺序表中无数据" << endl;
    	}
    	else
    	{
    		for (int i = 0; i < L.size; i++)
    		{
    			cout << L.data[i] << " ";
    		}
    		cout << endl;
    	}
    }
    
    //取数据
    void GetElem(const SL& L, int i, ElemType& e)
    {
    	assert(i > 0 && i <= L.size);//判断位置的合法性
    	e = L.data[i - 1];
    }
    
    //查找数据
    int LocateElem(const SL& L, ElemType e)
    {
    	int i = 0;
    	for (i; i < L.size; i++)
    	{
    		if (L.data[i] == e)
    		{
    			return i + 1;//查找成功返回下标i+1 
    		}
    	}
    	return 0;//查找失败返回0 
    }
    
    //插入数据
    void ListInsert(SL& L, int i, ElemType e)
    {
    	assert(i > 0 && i <= L.size + 1);//判断位置的合法性
    
    	ListNewCapacity(L);//检查空间是否够用,不够用扩容
    
    	int ret = L.size - 1;
    	for (ret; ret >= i - 1; ret--)
    	{
    		L.data[ret + 1] = L.data[ret];
    	}
    	L.data[i - 1] = e;
    	L.size++;
    }
    
    //修改数据
    void ListRevise(SL& L, int i, ElemType e)
    {
    	assert(i > 0 && i <= L.size);//判断位置的合法性
    	L.data[i - 1] = e;
    }
    
    //删除数据
    void ListDelete(SL& L, int i)
    {
    	assert(i > 0 && i <= L.size);//判断位置的合法性
    	int ret= i - 1;
    	for (ret; ret < L.size - 1; ret++ )
    	{
    		L.data[ret] = L.data[ret + 1];
    	}
    
    	L.size--;
    }
    
    //销毁顺序表
    void DestroyList(SL& L)
    {
    	free(L.data);
    	L.data = NULL;
    	L.size = L.capacity = 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
  • 相关阅读:
    学完就干的Linux常用命令
    Linux入门之使用 systemctl 管理 systemd 服务
    传输层 HTTP 的报文结构
    liunx集成jmeter进行压测实践
    Qt model/view 理解 2
    vue自定义指令directives
    使用containerd搭建MinIO集群服务
    技术面经总结
    Amazon云计算AWS之[2]弹性计算云EC2
    跨境电商卖家只青睐亚马逊?其实你不知道,“备胎”早已选好!(Starday)
  • 原文地址:https://blog.csdn.net/weixin_74814064/article/details/133772149