赞
踩
目录
顺序表——用顺序存储的方式实现线性表顺序存储
把逻辑上相邻的元素存储在物理位置上也相邻的存储单元中,元素之间的关系由存储单元的邻接关系来体现,是具有相同数据类型的n(n>0)个数据元素的有限序列。
主要操作
其他常用操作
- //顺序表的建立
- //结构体封装data数据数组和数组长度
- struct SqList
- {
- int data[MaxSize];
- int Length; //当前长度
- };
- //顺序表的初始化
- //将长度设置为零
- void InitList(SqList& sq)
- {
- sq.Length = 0;
- cout << "顺序表初始化完成" << endl;
- }
- //顺序表的查找
- int GetList(SqList sq)
- {
- int i,k;
- cout << "请输入你要查找元素的位序" << endl;
- cin >> i;
- if (i<0 || i>sq.Length - 1)
- {
- return 0;
- }
- else
- {
- k = sq.data[i - 1];
- cout << "你要查找的元素为" << k << endl;
- }
- }
- int Listlnsert(SqList& sq)
- {
- int i, e;
- cout << "请输入您要插入的位序及数字"<<endl;
- cin >> i >> e;
- if (i<0 || i>sq.Length)
- {
- cout << "插入地址不合法" << endl;
- return 0;
- }
- for (int j = sq.Length; j >= i ; j--)
- {
- sq.data[j] = sq.data[j - 1];
- }
- sq.data[i - 1] = e;
- sq.Length++;
- cout << "插入成功" << endl;
- }
- bool Deletelist(SqList &sq,int &m)
- {
- int i;
- cout << "请输入您要删除的位序" << endl;
- cin >> i;
- if (i<1 || i>sq.Length)
- {
- return false;
- }
- m = sq.data[i - 1];
- for (int j = i; j < sq.Length; j++)
- {
- sq.data[j - 1] = sq.data[j];
- }
- sq.Length--;
- return true;
- }
注:利用m将第i位要删除的数据带出去,接着用for循环将data[i-1]后面的数依次向前移位
- #include<iostream>
- #include<algorithm>
- using namespace std;
- #define MaxSize 10
-
- //顺序表的建立
- struct SqList
- {
- int data[MaxSize];
- int Length; //当前长度
- };
-
- //顺序表的初始化
- void InitList(SqList& sq)
- {
- sq.Length = 0;
- cout << "顺序表初始化完成" << endl;
- }
-
- //顺序表传值
- void Chuan(SqList& sq, int n)
- {
- cout << "请传入数值" << endl;
- for (int i = 0; i < n; i++)
- {
- cin >> sq.data[i];
- sq.Length++;
- }
- }
-
- //打印顺序表
- int dayin(SqList sq)
- {
- cout << "当前顺序表为" << endl;
- if (sq.Length == 0)
- {
- return 0;
- }
- for (int k = 0; k < sq.Length; k++)
- {
- cout << sq.data[k] << "\t";
- }
- cout << endl;
- }
-
- //顺序表的查找
- int GetList(SqList sq)
- {
- int i,k;
- cout << "请输入你要查找元素的位序" << endl;
- cin >> i;
- if (i<0 || i>sq.Length)
- {
- return 0;
- }
- else {
- k = sq.data[i - 1];
- cout << "你要查找的元素为" << k << endl;
- }
- }
-
- //顺序表的插入
- int ListCha(SqList& sq)
- {
- int i, e;
- cout << "请输入您要插入的位序及数字"<<endl;
- cin >> i >> e;
- if (i<0 || i>sq.Length)
- {
- cout << "插入地址不合法" << endl;
- return 0;
- }
- for (int j = sq.Length; j >= i ; j--)
- {
- sq.data[j] = sq.data[j - 1];
- }
- sq.data[i - 1] = e;
- sq.Length++;
- cout << "插入成功" << endl;
- }
-
- //顺序表删除
- bool Deletelist(SqList &sq,int &m)
- {
- int i;
- cout << "请输入您要删除的位序" << endl;
- cin >> i;
- if (i<1 || i>sq.Length)
- {
- return false;
- }
- m = sq.data[i - 1];
- for (int j = i; j < sq.Length; j++)
- {
- sq.data[j - 1] = sq.data[j];
- }
- sq.Length--;
- return true;
- }
-
- int main()
- {
- int n,e;
- cout << "请输入顺序表长度"<<endl;
- cin >> n;
- int m = -1;
- SqList sq; //创建对象
- InitList(sq);//顺序表初始化
- Chuan(sq, n);//顺序表传值
- dayin(sq);
- GetList(sq);
- ListCha(sq);
- dayin(sq);
- if (Deletelist(sq, m))
- {
- cout << "已删除" << endl;
- }
- else
- {
- cout << "删除失败" << endl;
-
- }
- dayin(sq);
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。