c++实现顺序表的操作

20 篇文章 0 订阅
15 篇文章 0 订阅

c++实现顺序表的一些操作,有三个文件。

#include"Sequence.h"

#include<iostream>
using namespace std;
int main()
{
SeqList *L,*L1,*L2,*L3;
L = Init_SeqList();
L1 = Init_SeqList();
L2 = Init_SeqList();
L3 = Init_SeqList();
Define_SeqList(L, 6);
Define_SeqList(L1, 7);
Define_SeqList(L2, 8);
Define_SeqList(L3, 20);
Display_SeqList(L);
Display_SeqList(L1);
Display_SeqList(L2);
Display_SeqList(L3);
Insert_SeqList(L, 4, 3);
Insert_SeqList(L, 6, 21);
Insert_SeqList(L, 2, 35);
Delete_SeqList(L, 4);
Delete_SeqList(L, 3);
Delete_SeqList(L, 12);
Locate_SeqList(L, 6);
Intersection(L1,L2,L3);//交集
Display_SeqList(L3);
clearall(L3, L3->SeqLength);
Union_SeqList(L1,L2,L3);//并集
system("pause");// 必须在return之前才有效果。
return 0;

}


#include <iostream>
using namespace std;
#define MAXSIZE 35
typedef int DataType;
typedef struct  //定义一个线性表
{
DataType data[MAXSIZE];
int SeqLength;
}SeqList;


SeqList *Init_SeqList();//初始化
void Define_SeqList(SeqList *L, int n);//定义
void Display_SeqList(SeqList *L);//显示
int Insert_SeqList(SeqList *L, int i, DataType x);//插入
int Delete_SeqList(SeqList *L, int i);//删除
int Locate_SeqList(SeqList *L, int i);//定位查找
int Intersection(SeqList *L1, SeqList *L2, SeqList *L3);//交集
int Union_SeqList(SeqList *L1, SeqList *L2, SeqList *L3);//并集
int clearall(SeqList *L, int n);


#include "Sequence.h"
#include <iostream>
using namespace std;
SeqList *Init_SeqList()//顺序表初始化,
{
SeqList *L;   //SeqList是一种数据类型,L既是表名又是指向表名的指针,和数组很像。
L = new SeqList;
L->SeqLength = 0;
return L;
}


void Define_SeqList(SeqList *L, int n)// 定义算法
{
cout << "请输入顺序表中的元素:" << endl;
for (int i = 0; i < n; i++)
{
cin >> L->data[i];
L->SeqLength++;
}
}


void Display_SeqList(SeqList *L)//输出算法
{
cout << "顺序表储存元素为:" << endl;
int i;
for (i = 0; i <= L->SeqLength - 1; i++)
{
cout << L->data[i] << endl;
}
cout << endl;
}


int Insert_SeqList(SeqList *L, int i, DataType x)
{
cout << "把元素" << x << "插入到" << i << "位置上" << endl;
int j;
if (L->SeqLength == MAXSIZE - 1)
{
cout << "表满了" << endl;
return -1;
}
if (i<1 || i>L->SeqLength + 1)
{
cout << "位置错" << endl;
return 0;
}
for(j = L->SeqLength - 1; j >= i-1; j--)
{
L->data[j + 1] = L->data[j];
}
L->data[i-1] = x;  //插入这个位置
L->SeqLength++;
cout << "插入成功" << endl;
Display_SeqList(L);
return 1;
}


int Delete_SeqList(SeqList*L, int i)
{
cout << "把位置为" << i << "的元素删除" << endl;
int j;
if (i<1 || i>L->SeqLength)
{
cout << "不存在" << i << "位置元素" << endl;
return 0;
}
for (j = i; j <= L->SeqLength - 1; j++)
{
L->data[j - 1] = L->data[j];
}
L->SeqLength--;
cout << "删除成功" << endl;
Display_SeqList(L);
return 1;
}


int clearall(SeqList *L, int n)
{
n = L->SeqLength;
int i;
for (i = 0; i < L->SeqLength; i++)
L->data[i] = 0;
return 0;


}


int Locate_SeqList(SeqList *L, DataType x)
{
int i;
for (i = 0; i <= L->SeqLength - 1;i++)
if (L->data[i] == x)
cout << "位置是" << i << endl;
else cout << "表中没有该元素" << endl;
return 1;
}
int Intersection(SeqList *L1, SeqList *L2, SeqList *L3)
{
int tem = 0;
L3->SeqLength = L1->SeqLength;
int i;
//将L1元素给L3
//for (i = 0; i <= L1->SeqLength - 1; i++)
// L3->data[i] = L1->data[i];
//和L2的元素比较
for (int j = 0; j <= L2->SeqLength-1; j++)
{
for (int i = 0; i <= L1->SeqLength-1; i++)
{
if (L1->data[i] == L2->data[j]) //
tem++;
L3->data[j] == L2->data[j];
}
}
return tem;
}


int  Union_SeqList(SeqList *L1, SeqList *L2, SeqList *L3)
{
int tem = 0;
int i;
L3->SeqLength = L1->SeqLength;
//将L1元素给L3
for (int i = 0; i <= L1->SeqLength - 1; i++)
L3->data[i] = L1->data[i];
for (int j = 0; j <= L2->SeqLength-1; j++)
{
for (int i = 0; i <= L1->SeqLength-1; i++)
{
if (L1->data[i] == L2->data[j])
tem++;
else


L3->data[L3->SeqLength] = L2->data[j];//合并L1,L2
L3->SeqLength = L3->SeqLength+1;
}
}
if (tem == 0)
{
cout << "L1,L2没共同元素" << endl;
}
for (i = 0; i <= L3->SeqLength-1; i++)
{
cout << "合并后的顺序表元素为:" << L3->data[i] << endl;
}
return 1;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值