数据结构之线性表
来源:互联网 发布:大数据脱敏 编辑:程序博客网 时间:2024/06/03 22:00
// 线性表
#include<iostream>
#define MAXSIZE 100
using namespace std;
// 结构体
typedef struct
{
int elem[MAXSIZE];
int last;
}SeqList;
void InitList(SeqList *L) // 初始化线性表
{
for (int i = 0; i < L->last; i++)
{
L->elem[i] = 0;
}
}
int ListLength(SeqList *L) // 返回线性表的长度;
{
return L->last;
}
int GetData(SeqList *L, int i) // 返回线性表中第i个元素的值;
{
if (i<1 || i>L->last)
{
return -1;
}
for (int j = 0; j < L->last; j++)
{
if (j == i)
{
return L->elem[j];
}
}
}
bool InsList(SeqList *L, int i, int e) // 在线性表的第i个位置之前插入元素 e;
{
if (i<1 || i >L->last - 1 || i>L->last + 2)
{
return false;
}
int j;
for (j = L->last; j >i - 1; j--)
{
L->elem[j] = L->elem[j - 1];
}
L->elem[j] = e;
L->last++;
return true;
}
bool DelList(SeqList *L, int i, int *e) // 删除线性表的第i个位置的元素,并返回其值;
{
if (i < 1 || i >= L->last)
{
return false;
}
int j;
for (j=i;j<L->last-1;j++)
{
if (j == i)
{
*e = L->elem[j];
L->elem[j] = L->elem[j+1];
}
else
L->elem[j] = L->elem[j+1];
}
L->elem[j] = 0;
L->last--;
return true;
}
int GetLocate(SeqList *L, int e) // 返回线性表中元素值为*e的位置
{
int i;
for (i = 0; i < L->last; i++)
{
if (L->elem[i] == e)
{
return i;
}
}
if (i == L->last)
{
return -1;
}
}
void DestoryList(SeqList *L,int *a) // 销毁线性表;
{
delete L;
L = NULL;
delete a;
a = NULL;
}
void ClearList(SeqList *L,int *a) // 将线性表置为空表;
{
for (int i = 0; i < L->last; i++)
{
L->elem[i] = 0;
}
L->last = 0;
}
bool EmptyList(SeqList *L) // 判断线性表是否空
{
if (ListLength(L) != 0)
{
return false;
}
else
{
return true;
}
// L->last = 0;
}
void Traverse(SeqList *L)
{
//int count = 0;
for (int i = 0; i < L->last; i++)
{
cout << L->elem[i]<<" ";
/* count++;
if (count % 5 == 0)
cout << endl;*/
}
cout << endl;
}
void UseSeqList(SeqList *L,int len,int *a)
{
// 初始化线性表
L->last = len;
InitList(L);
for (int i = 0; i < len; i++)
{
int t = 0;
cin >> t;
L->elem[i] = t;
}
Traverse(L);
// 获得某个位置元素的值;
int m;
cout << "请输入要获得第几个元素的值:" << endl;
cin >> m;
int t = GetData(L, m-1);
if (t == -1)
{
cout << "第"<<m<<"个数不存在!" << endl;
}
else
{
cout << "线性表中第" << m << "个元素的值是:" << t << endl;
}
Traverse(L);
// 插入元素
int x = 0,y=0;
cout << "请输入要插入的位置和元素的值:" << endl;
cin >> x>>y;
if (true == InsList(L, x, y))
{
cout << "插入成功!" << endl;
}
else
{
cout << "插入失败!" << endl;
}
Traverse(L);
// 获得某个元素的 下标
int xx = 0;
cout << "请输入要查找元素的值:" <<endl;
cin >> xx;
int temp = GetLocate(L, xx);
cout << "元素值为"<<xx<<"的下标为:" << temp+1 << endl;;
Traverse(L);
// 删除某个元素;
int b = 0;
cout << "请输入要删除元素的下标:" <<endl;
cin >> b;
if (true == DelList(L, b-1,a))
{
cout << "删除的第" << b << "个元素的值为:" << *a << endl;
}
else
{
cout << "删除操作失败!" << endl;
}
Traverse(L);
// 判断链表是否为空;
if (EmptyList(L) == true)
{
cout << "线性表为空!" << endl;
}
else
{
cout << "线性表不为空!" << endl;
}
Traverse(L);
// 将链表置为空;
ClearList(L,a);
Traverse(L);
// 判断链表是否为空;
if (EmptyList(L) == true)
{
cout << "线性表为空!" << endl;
}
else
{
cout << "线性表不为空!" << endl;
}
Traverse(L);
}
int main(void)
{
// 1 2 3 4 5 6 7 8 9 10
int len=0;
int *a = new int;
cout << "请输入结构体数组的大小:" << endl;
cin >> len;
SeqList *L = new SeqList;
UseSeqList(L,len,a);
// 销毁链表;
DestoryList(L,a);
return 0;
}
#include<iostream>
#define MAXSIZE 100
using namespace std;
// 结构体
typedef struct
{
int elem[MAXSIZE];
int last;
}SeqList;
void InitList(SeqList *L) // 初始化线性表
{
for (int i = 0; i < L->last; i++)
{
L->elem[i] = 0;
}
}
int ListLength(SeqList *L) // 返回线性表的长度;
{
return L->last;
}
int GetData(SeqList *L, int i) // 返回线性表中第i个元素的值;
{
if (i<1 || i>L->last)
{
return -1;
}
for (int j = 0; j < L->last; j++)
{
if (j == i)
{
return L->elem[j];
}
}
}
bool InsList(SeqList *L, int i, int e) // 在线性表的第i个位置之前插入元素 e;
{
if (i<1 || i >L->last - 1 || i>L->last + 2)
{
return false;
}
int j;
for (j = L->last; j >i - 1; j--)
{
L->elem[j] = L->elem[j - 1];
}
L->elem[j] = e;
L->last++;
return true;
}
bool DelList(SeqList *L, int i, int *e) // 删除线性表的第i个位置的元素,并返回其值;
{
if (i < 1 || i >= L->last)
{
return false;
}
int j;
for (j=i;j<L->last-1;j++)
{
if (j == i)
{
*e = L->elem[j];
L->elem[j] = L->elem[j+1];
}
else
L->elem[j] = L->elem[j+1];
}
L->elem[j] = 0;
L->last--;
return true;
}
int GetLocate(SeqList *L, int e) // 返回线性表中元素值为*e的位置
{
int i;
for (i = 0; i < L->last; i++)
{
if (L->elem[i] == e)
{
return i;
}
}
if (i == L->last)
{
return -1;
}
}
void DestoryList(SeqList *L,int *a) // 销毁线性表;
{
delete L;
L = NULL;
delete a;
a = NULL;
}
void ClearList(SeqList *L,int *a) // 将线性表置为空表;
{
for (int i = 0; i < L->last; i++)
{
L->elem[i] = 0;
}
L->last = 0;
}
bool EmptyList(SeqList *L) // 判断线性表是否空
{
if (ListLength(L) != 0)
{
return false;
}
else
{
return true;
}
// L->last = 0;
}
void Traverse(SeqList *L)
{
//int count = 0;
for (int i = 0; i < L->last; i++)
{
cout << L->elem[i]<<" ";
/* count++;
if (count % 5 == 0)
cout << endl;*/
}
cout << endl;
}
void UseSeqList(SeqList *L,int len,int *a)
{
// 初始化线性表
L->last = len;
InitList(L);
for (int i = 0; i < len; i++)
{
int t = 0;
cin >> t;
L->elem[i] = t;
}
Traverse(L);
// 获得某个位置元素的值;
int m;
cout << "请输入要获得第几个元素的值:" << endl;
cin >> m;
int t = GetData(L, m-1);
if (t == -1)
{
cout << "第"<<m<<"个数不存在!" << endl;
}
else
{
cout << "线性表中第" << m << "个元素的值是:" << t << endl;
}
Traverse(L);
// 插入元素
int x = 0,y=0;
cout << "请输入要插入的位置和元素的值:" << endl;
cin >> x>>y;
if (true == InsList(L, x, y))
{
cout << "插入成功!" << endl;
}
else
{
cout << "插入失败!" << endl;
}
Traverse(L);
// 获得某个元素的 下标
int xx = 0;
cout << "请输入要查找元素的值:" <<endl;
cin >> xx;
int temp = GetLocate(L, xx);
cout << "元素值为"<<xx<<"的下标为:" << temp+1 << endl;;
Traverse(L);
// 删除某个元素;
int b = 0;
cout << "请输入要删除元素的下标:" <<endl;
cin >> b;
if (true == DelList(L, b-1,a))
{
cout << "删除的第" << b << "个元素的值为:" << *a << endl;
}
else
{
cout << "删除操作失败!" << endl;
}
Traverse(L);
// 判断链表是否为空;
if (EmptyList(L) == true)
{
cout << "线性表为空!" << endl;
}
else
{
cout << "线性表不为空!" << endl;
}
Traverse(L);
// 将链表置为空;
ClearList(L,a);
Traverse(L);
// 判断链表是否为空;
if (EmptyList(L) == true)
{
cout << "线性表为空!" << endl;
}
else
{
cout << "线性表不为空!" << endl;
}
Traverse(L);
}
int main(void)
{
// 1 2 3 4 5 6 7 8 9 10
int len=0;
int *a = new int;
cout << "请输入结构体数组的大小:" << endl;
cin >> len;
SeqList *L = new SeqList;
UseSeqList(L,len,a);
// 销毁链表;
DestoryList(L,a);
return 0;
}
0 0
- 数据结构之线性表
- 数据结构之线性表
- 数据结构之线性表
- 数据结构之线性表
- 数据结构之线性表
- 数据结构之线性表
- 数据结构之线性表
- 数据结构之线性表
- 数据结构之线性表
- 数据结构之线性表
- 数据结构之线性表
- 数据结构之线性表
- 数据结构之线性表
- 数据结构之线性表
- 数据结构之线性表
- 数据结构之线性表
- 数据结构之线性表
- 数据结构之线性表
- java 获取系统时间方式
- svn 日常问题处理
- Linux进程间关系之终端与终端登录
- java实战,一个记事本
- Jsp
- 数据结构之线性表
- java实战,一个记事本
- 笔记-Python基础教程(第二版)第二章
- find命令
- Linux基础——创建和删除目录
- 五.Kafka消息的时间戳
- Cookie和Session学习
- OLAP的12条准则
- application loader 跨平台版本application uploader使用经验