一、main
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include "com.h"
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
// 线性表 主函数
SqList L ;
int i , cur_e , n;
ElemType e , pre_e , next_e;
int choice = 1; // 可供选择
void initlist()
{
if(L.length == -1) // 说明还未初始化过
{
InitList(L) ; cout << endl;
cout << "\t\t\t初始化完成!" << endl << endl;
}
else cout << "线性表已存在,不能重复执行该操作" << endl ;
}
void putelem()
{
printf("\t\t\t请输入你想置换的元素序号:") ;
cin >> i ;
printf("\t\t\t元素置换成:");
cin >> e ;
if(PutElem(L , i , e) == ERROR) cout << "\t\t\t输入有误" << endl ;
else
{
cout << "\t\t\t置换后的线性表为:" ;
for(i = 0 ; i < L.length ; i ++)
cout << L.elem[i] << " " ;
cout << endl << endl;
}
}
void getelem()
{
cout << "\t\t\t请输入你想查看的元素序号i:" ;
cin >> i ;
if(GetElem(L , i , e) == ERROR) cout << "\t\t\t输入有误!" << endl ;
else cout << "\t\t\t该线性表第" << i << "个元素为" << e << endl;
}
void locateelem()
{
cout << "\t\t\t请输入在表中要查找的元素e:" ;
cin >> e ;
if(LocateElem(L , e))
cout << "\t\t\t" << e << "是表中的第" << LocateElem(L , e) << "个位置" << endl ;
else
cout << "\t\t\t元素" << e << "不存在" << endl ;
}
void priorelem()
{
cout << "\t\t\t请输入查找其前驱的元素:" ;
cin >> cur_e ;
if(PriorElem(L , cur_e , pre_e) == ERROR)
cout << "\t\t\t元素" << cur_e << "不存在在表中" << endl ;
else if(PriorElem(L , cur_e , pre_e) == OVERFLOW)
cout << "\t\t\t第一元素不存在直接前驱" << endl ;
else
cout << "\t\t\t元素" << cur_e << "的直接前驱为" << pre_e << endl ;
}
void nextelem()
{
cout << "\t\t\t请输入查找其后继的元素:" ;
cin >> cur_e ;
if(NextElem(L , cur_e , next_e) == ERROR)
cout << "\t\t\t元素" << cur_e << "不存在在表中" << endl ;
else if(NextElem(L , cur_e , next_e) == OVERFLOW)
cout << "\t\t\t最后元素不存在直接后继" << endl ;
else
cout << "\t\t\t元素" << cur_e << "的直接后继为" << next_e << endl ;
}
void listinsert()
{
cout << "\t\t\t请输入要插入的位置i:" ;
cin >> i ; cout << endl ;
cout << "\t\t\t请输入要插入的元素e:" ;
cin >> e ; cout << endl ;
if(ListInsert(L , i , e) == ERROR)
{
cout << "\t\t\ti值不合法或列表已满" << endl ;
}
else
{
cout << "\t\t\t已在表中i=" << i << "位置插入元素" << e << endl ;
cout << "\t\t\t现在的列表为:" ;
for(i = 0 ; i < L.length ; i ++)
cout << L.elem[i] << " " ;
cout << endl ;
}
}
void listdelete()
{
cout << "\t\t\t请输入要删除的位置i:" ;
cin >> i ; cout << endl ;
if(ListDelete(L , i) == ERROR)
{
cout << "\t\t\ti值不合法" << endl ;
}
else
{
cout << "\t\t\t现在的列表为:" ;
for(i = 0 ; i < L.length ; i ++)
cout << L.elem[i] << " " ;
cout << endl << endl;
}
}
void unionunordered(int t) // 两个表的合并
{
SqList La , Lb , Lc ;
cout << "\t\t\t表A的元素个数:" ;
cin >> n ;
InitList(La) ;
cout << "\t\t\t表A元素:" ;
assignment(La , n) ;
cout << "\t\t\t表B的元素个数:" ;
cin >> n ;
InitList(Lb) ;
cout << "\t\t\t表B元素:" ;
assignment(Lb , n) ;
if(t == 15) UnionUnordered(La , Lb) ; //15.两个表的合并(无序)
else if(t == 16) MergeList(La , Lb , Lc) ;//16.两个表的合并(有序)
cout << "\t\t\t合并后的线性表为:" << endl;
if(t == 15) ListTraverse(La) ;
else if(t == 16) ListTraverse(Lc) ;
cout << endl ;
}
void Menu()
{
printf("\t\t\t==========================================================\n");
printf("\t\t\t| |\n");
printf("\t\t\t| 线性表基本操作 |\n");
printf("\t\t\t| |\n");
printf("\t\t\t|========================================================|\n");
printf("\t\t\t| 请选择要操作的命令 |\n");
printf("\t\t\t|--------------------------------------------------------|\n");
printf("\t\t\t| |\n");
printf("\t\t\t| 0.退出整个系统 1.初始化线性表 |\n");
printf("\t\t\t| 2.销毁线性表 3.置换第i个元素 |\n");
printf("\t\t\t| 4.重置线性表为空表 5.判断线性表是否为空表 |\n");
printf("\t\t\t| 6.查看表中元素个数 7.查看表中第i个元素 |\n");
printf("\t\t\t| 8.查找表中元素e的位置 9.查找元素的直接前驱 |\n");
printf("\t\t\t| 10.查找元素的直接后继 11.在位置i前插入新元素 |\n");
printf("\t\t\t| 12.删除第i个元素 13.遍历线性表 |\n");
printf("\t\t\t| 14.给线性表赋值 |\n");
printf("\t\t\t| 15.两个表的合并(无序) 16.两个表的合并(有序) |\n");
printf("\t\t\t| |\n");
printf("\t\t\t==========================================================\n");
printf("\t\t\t选择(0---13):\t");
cin >> choice ;
switch (choice)
{
case 0 : // 0.退出整个系统
exit(0) ;
break ;
case 1: // 1.初始化线性表
initlist() ;
break ;
case 2: // 2.销毁线性表
DestroyList(L) ;
cout << "\t\t\t线性表已销毁" << endl << endl;
break ;
case 3 : //3.置换第i个元素
putelem() ;
break ;
case 4 : // 4.重置线性表为空表
ClearList(L) ;
cout << "\t\t\t线性表已清空" << endl ;
break ;
case 5 : // 5.判断线性表是否为空表
if(ListEmpty(L)) cout << "\t\t\t该线性表是空表" << endl ;
else cout << "\t\t\t该线性表不是空表" << endl ;
break ;
case 6 : // 6.查看表中元素个数
if(L.length == -1)
cout << "\t\t\t该线性表不存在,需要先初始化" << endl ;
else
cout <<"\t\t\t线性表中有" << ListLength(L) << "个元素" << endl ;
break ;
case 7 : // 7.查看表中第i个元素
getelem() ;
break ;
case 8 : // 8.查找表中元素e的位置
locateelem() ;
break ;
case 9 : // 9.查找元素的直接前驱
priorelem() ;
break ;
case 10 : // 10.查找元素的直接后继
nextelem() ;
break ;
case 11 : // 11.在位置i前插入新元素
listinsert() ;
break ;
case 12 : //12.删除第i个元素
listdelete() ;
break ;
case 13 : // 13.遍历线性表
if(L.length == -1)
cout << "线性表不存在,需要先初始化" << endl ;
else ListTraverse(L) ;
break ;
case 14:
cout << "\t\t\t请输入赋值个数:" ;
cin >> n ; cout << endl << "\t\t\t";
assignment(L , n) ;
break ;
case 15 : // 15.两个表的合并(无序)
unionunordered(15) ;
break ;
case 16 : // 15.两个表的合并(无序)
unionunordered(16) ;
break ;
default :
cout << "\t\t\t输入有误" << endl ;
}
Menu() ;
}
int main(int argc, char** argv)
{
L.length = -1 ; // 0 初始时,还没有任何元素,设为-1,表不存在
Menu() ;
return 0;
}
二、function
#include <stdlib.h>
#include "com.h"
#include <iostream>
using namespace std;
// 相应的线性表操作
// 111 初始化
Status InitList(SqList &L)
{
int n , i ;
L.elem = (ElemType *)malloc(sizeof(ElemType) * MAXSIZE) ; //申请最大空间
if(!L.elem) exit(OVERFLOW) ;
L.length = 0 ; // 仅仅是初始化,此时表中的元素仍为零个,所以length=0
// cout << "\t\t\t请输入初始化元素个数:" ;
// cin >> n ; cout << endl << "\t\t\t";
// for(i = 0 ; i < n ; i ++) cin >> L.elem[i] ;
// L.length = n ;
return OK ;
}
//Status InitList(SqList &L)
//{
// L.elem = (ElemType *)malloc(sizeof(ElemType) * MAXSIZE) ; //申请最大空间
// if(L.length != -1) return ERROR ; // 该操作不写也行,main中判断过
// L.length = 0 ;
// return OK ;
//}
// 222 销毁线性表
void DestroyList(SqList &L)
{
if(L.elem) delete[] L.elem ; // 释放存储空间
//return OK ;
}
// 333 置换第i个元素
Status PutElem(SqList &L , int i , ElemType e)
{
if(i < 1 || i > L.length) return ERROR ;
L.elem[i-1] = e ; // 置换
return OK ;
}
// 444 将L重置为空表
void ClearList(SqList &L)
{
L.length = 0 ; // 长度置为零,表中就没有元素了,清空为零了
}
// 555 判断线性表是否为空表
Status ListEmpty(SqList L)
{
if(L.length == 0) return 1 ; // 空表
else return 0 ; // 非空
}
// 666 表中元素个数(表长)
int ListLength(SqList L) //
{
if(L.elem) return L.length ;
}
// 777 获取表中第i个元素
Status GetElem(SqList L , int i , ElemType &e)
{
if(i < 1 || i > L.length) return ERROR ; // i越界了
e = L.elem[i-1] ;
return OK ;
}
// 888 查找表中元素e的位置
int LocateElem(SqList L , ElemType e)
{
int i ;
for(i = 0 ; i < L.length ; i ++)
if(L.elem[i] == e)
return i+1 ;
return 0 ; // 不存在
}
// 999 查找某元素的直接前驱
Status PriorElem(SqList L , int cur_e , ElemType &pre_e)
{
int i , flag = 0 ;
if(!L.elem) return ERROR ; // 空表,不存在元素cur_e
for(i = 0 ; i < L.length ; i ++)
{
if(L.elem[i] == cur_e)
{
flag = 1 ; // 找得到元素 cur_e
break ;
}
}
if(flag == 0) return ERROR ;
if(i == 0) return OVERFLOW ; // 第一元素
else pre_e = L.elem[i-1] ;
return OK ;
}
// 10 查找某元素的直接后继
Status NextElem(SqList L , int cur_e , ElemType &next_e)
{
int i , flag = 0 ;
if(!L.elem) return ERROR ; // 空表,不存在元素cur_e
for(i = 0 ; i < L.length ; i ++)
{
if(L.elem[i] == cur_e)
{
flag = 1 ; // 找得到元素 cur_e
break ;
}
}
if(flag == 0) return ERROR ; // 没找到
if(i == L.length-1) return OVERFLOW ; // 最后元素
else next_e = L.elem[i+1] ;
return OK ;
}
// 11 在第i个位置前插入新元素
Status ListInsert(SqList &L , int i , ElemType e)
{
if(i < 1 || i > L.length+1) return ERROR ;//不太理解为什么要 L.length+1
if(L.length == MAXSIZE) return ERROR ;
int j ;
for(j = L.length-1 ; j >= i-1 ; j --)
L.elem[j+1] = L.elem[j] ;
L.length ++ ;
L.elem[i-1] = e ;
return OK ;
}
// 12 删除第i个元素
Status ListDelete(SqList &L , int i)
{
if(i < 1 || i > L.length) return ERROR ;
int j ;
for(j = i ; j < L.length ; j ++)
L.elem[j-1] = L.elem[j] ;
L.length -- ;
return OK ;
}
// 13 遍历线性表
void ListTraverse(SqList L)
{
int i ;
cout << "\t\t\t" ; // 不要在函数里输出
if(L.length> 0)
{
for(i = 0 ; i < L.length ; i ++)
cout << L.elem[i] << " " ;
}
else cout<< "空表" ;
cout << endl ;
}
// 14 assignment 给线性表赋值
Status assignment (SqList &L , int n )
{
if(!L.elem) exit(OVERFLOW) ;
int i ;
for(i = 0 ; i < n ; i ++) cin >> L.elem[i] ;
L.length = n ;
return OK ;
}
// 15.两个表的合并(无序)
void UnionUnordered(SqList &La , SqList &Lb)
{
int La_len , Lb_len , i;
ElemType e ;
La_len = ListLength(La) ;
Lb_len = ListLength(Lb) ;
for(i = 1 ; i <= Lb_len ; i ++)
{
GetElem(Lb , i , e) ;// 查找B表中第i个元素并把它赋给e
if(!LocateElem(La , e)) // 如果e元素不存在在A表中,插入
ListInsert(La , ++La_len , e) ; // 在La_len+1位置插入
}
}
// 16.两个表的合并(有序)
void MergeList(SqList La , SqList Lb , SqList &Lc)
{
int La_len , Lb_len , Lc_len ;
La_len = ListLength(La) ;
Lb_len = ListLength(Lb) ;
InitList(Lc) ;
// Lc.elem = (ElemType *)malloc(sizeof(ElemType) * (La_len+Lb_len)) ; 问题!!!!!!!
int i = 1 , j = 1 , k = 0; // i、j、k分别是指向A、B、C、两个表位置的指针
ElemType ai , bi ;
while((i <= La_len) && (j <= Lb_len))
{
GetElem(La , i , ai) ; // 查找A表中第i个元素ai
GetElem(Lb , j , bi) ;// 查找B表中第i个元素bi
if(ai <= bi) // 把A中元素ai插入到C表中
{
ListInsert(Lc , ++k , ai) ; //放在 k+1位置
++ i ; // i指针后移
}
else // 把B中元素bi插入到C表中
{
ListInsert(Lc , ++k , bi) ;
++ j ;// j指针后移
}
}
// 接下来是A或B中元素已全部放入C表中,另一个顺位放入C中
while(i <= La_len)
{
GetElem(La , i , ai) ;
ListInsert(Lc , ++k , ai) ; //放在 k+1位置
++ i ; // i指针后移
}
while(j <= Lb_len)
{
GetElem(Lb , j , bi) ;
ListInsert(Lc , ++k , bi) ;
++ j ;// j指针后移
}
}
三、com
#ifndef _FUNC_H
#define _FUNC_H
#define OK 0
#define ERROR -1
#define OVERFLOW -2
typedef int Status ;
typedef int ElemType ;
#define MAXSIZE 100 // 表的最大长度 可变
typedef struct{
ElemType *elem ; // 指向数据元素的地址
int length ; // 表的长度
}SqList;
// 操作函数
extern Status InitList(SqList &L) ; // 初始化
extern void DestroyList(SqList &L) ; // 销毁存储表
Status PutElem(SqList &L , int i , ElemType e) ; // 置换第i个元素
extern void ClearList(SqList &L) ; // 将L重置为空表
extern Status ListEmpty(SqList L) ; // 判断线性表是否为空表
extern int ListLength(SqList L) ; // 6.表中元素个数(表长)
extern Status GetElem(SqList L , int i , ElemType &e) ; // 7.获取表中第i个元素
extern int LocateElem(SqList L , ElemType e) ; //查找表中元素e的位置
extern Status PriorElem(SqList L , int cur_e , ElemType &pre_e) ; // 查找某元素的直接前驱
extern Status NextElem(SqList L , int cur_e , ElemType &next_e) ;// 查找某元素的直接后继
extern Status ListInsert(SqList &L , int i , ElemType e) ; //在第i个位置前插入新元素
extern Status ListDelete(SqList &L , int i) ; // 12 删除第i个元素
extern void ListTraverse(SqList L) ; // 遍历线性表
extern Status assignment (SqList &L , int n) ;
extern void UnionUnordered(SqList &La , SqList &Lb) ;// 15.两个表的合并(无序)
extern void MergeList(SqList La , SqList Lb , SqList &Lc) ;
#endif