仅供参考!
先放要求
后放代码
#include <cstdio> //相当于C语言里的#include <stdio.h>
#include <iostream> //用cin、cout进行输入输出时需要用到该头文件
#include <algorithm> //用C++里面现成的排序函数时要用到此头文件
#define MAX 100
using namespace std; //用cin、cout进行输入输出时需要加上此句
typedef struct // 定义一个结构体
{
int * p;
int n;
}SqList;
SqList L; //定义顺序表L
void init()//初始化
{
L.p = new int[MAX]; //新开辟空间为100个整型
if(!L.p)//如果开辟失败就报错退出
{
cout << "ERROR!" << endl;
exit(0);
}
L.n = 0;
}
void create() //对里面元素进行赋值
{
for(int i = 0 ; i < L.n ; i++)
{
cin >> L.p[i];
}
}
int find_max()//找最大值
{
int maxn = L.p[0];
for(int i = 1; i < L.n; i++)
{
if( L.p[i] > maxn )
maxn = L.p[i];
}
return maxn;
}
void insert(int a , int b) //插入元素
{
for(int i = L.n ; i >= b ; i--)
{
L.p[i] = L.p[i-1];
}
L.p[b-1] = a;
}
void del(int x) //删除元素
{
for(int i = x-1 ; i < L.n ; i++)
{
L.p[i] = L.p[i+1];
}
}
void print() //输出
{
cout << "Now the order table elements are:";
for(int i = 0; i < L.n ;i++)
{
cout<< L.p[i] ;
if(i != L.n-1 )cout << " ";
}
cout << endl;
}
void reverse() //逆序排列
{
int tmp;
for(int i = 0 ; i <= (L.n-1)/2 ; i++)
{
tmp = L.p[i];
L.p[i] = L.p[L.n-1-i];
L.p[L.n-1-i] = tmp;
}
}
int main()
{
init();
int num;
cout << "Please enter the number of data elements:" ;
cin >> num;
L.n = num;
create();
cout << "The elements in the order table are : ";
print();
int ans = find_max();
cout << "The maximum number is: " << ans << endl;
int i , x;
cout << "Please enter the location of the element to insert : i = ";
cin >> i;
cout << "Please enter this number : ";
cin >> x;
insert(x,i);
L.n++;
print();
cout << "Please enter the location you want to delete: ";
cin >> i;
del(i);
L.n--;
print();
cout << "Do you want to sort?(y/n)";
char ch;
cin >> ch;
if(ch == 'y'|| ch=='Y')sort(L.p,L.p+L.n);//C++里面现成的排序函数
print();
cout << "Do you want to reverse?(y/n)";
cin >> ch;
if(ch == 'y'|| ch=='Y')reverse();
print();
return 0;
}
更规范版的代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAXSIZE 100
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define LIST_INIT_SIZE 5
#define LISTINCREMENT 1
using namespace std;
typedef int Status;
typedef int ElemType; //表内元素类型为 int, 可更改
typedef struct
{
ElemType elem[MAXSIZE]; //课本上这里是指针, 一样做, 后面的 L.elem[i] 改成 *L.elem[i]就好了
int length;
}SqList;
int n, i, j; //全局变量
ElemType maxelem, x;
Status InitList(SqList &L) //建立
{
L.length = 0;
return OK;
}
Status CreatList(SqList &L, int n) //建立一个顺序表,输入n个元素并输出
{
printf("Please enter the number of elements:\n");
scanf("%d", &n);
if(n <= 0 || n > MAXSIZE)
{
printf("Transboundary\n");
return ERROR;
}
printf("Please enter the elements:\n");
for(int i = 0; i < n; ++i)
{
cin >> L.elem[i]; //也可以通过插入操作建立
++L.length;
}
for(int i = 0; i < L.length; ++i)
cout << L.elem[i] << ' ';
cout << endl;
return OK;
}
Status Find_MaxElem(SqList L, ElemType &maxelem) //查找线性表中的最大元素并输出
{
if(!L.length)
{
printf("Non-existent\n");
return ERROR;
}
maxelem = L.elem[0];
for(int i = 0; i < L.length; ++i)
if(L.elem[i] > maxelem)
maxelem = L.elem[i];
printf("The largest element in the linear table is:\n");
cout << maxelem << endl;
return OK;
}
Status ListInsert(SqList &L, int i, ElemType x) //在线性表的第i个元素前插入一个正整数x
{
printf("Please enter the insertion position and value:\n");
cin >> i >> x;
if((i < 1) || (i > L.length+1))
{
printf("Transboundary\n");
return ERROR;
}
if(L.length == MAXSIZE)
return ERROR;
for(int j = L.length-1; j >= i-1; --j)
L.elem[j+1] = L.elem[j];
L.elem[i-1] = x;
++L.length;
printf("The linear table after inserting is as follow:\n");
for(int i = 0; i < L.length; ++i)
cout << L.elem[i] << ' ';
cout << endl;
return OK;
}
Status ListDelete(SqList &L, int j) //删除线性表中的第j个元素
{
printf("Enter the position of the element to delete:\n");
scanf("%d", &j);
if((j < 1) || (j > L.length))
{
printf("Transboundary\n");
return ERROR;
}
for(int i = j; i <= L.length; ++i)
L.elem[i-1] = L.elem[i];
--L.length;
printf("The linear table after deleting is as follow:\n");
for(int i = 0; i < L.length; ++i)
cout << L.elem[i] << ' ';
cout << endl;
return OK;
}
Status SortList(SqList L) //将线性表中的元素按升序排列
{
if(!L.length)
{
printf("Transboundary\n");
return ERROR;
}
int i, j, k;
ElemType t;
for(i = 0; i < L.length-1; ++i)
{
k = i;
for(j = i+1; j < L.length; ++j)
if(L.elem[j] < L.elem[k])
k = j;
if(k != i)
{
t = L.elem[i];
L.elem[i] = L.elem[k];
L.elem[k] = t;
}
}
printf("The linear table after ascending is as follow:\n");
for(i = 0; i < L.length; ++i)
cout << L.elem[i] << ' ';
cout << endl;
return OK;
}
Status ReverseList(SqList L) //将线性表中的元素就地逆序(只允许用一个暂存单元)
{
if(!L.length)
{
printf("Transboundary\n");
return ERROR;
}
int l, r;
ElemType t;
l = 0;
r = L.length-1;
while(l < r)
{
t = L.elem[l];
L.elem[l] = L.elem[r];
L.elem[r] = t;
++l;
--r;
}
printf("The linear table after local reverse order is as follow:\n");
for(int i = 0; i < L.length; ++i)
cout << L.elem[i] << ' ';
cout << endl;
return OK;
}
int main()
{
SqList L;
InitList(L);
CreatList(L, n);
Find_MaxElem(L, maxelem);
ListInsert(L, i, x);
ListDelete(L, j);
SortList(L);
ReverseList(L);
return 0;
}
实验报告怎么写
请参考这个