最简单的利用数组实现
#include<iostream>
using namespace std;
const int arraysize=30;
int main() {
int n;
cin >> n;
int a[arraysize];
for (int i = 0; i < n; i++)
cin >> a[i];
int temp;
if (n >= arraysize)
cerr << "参数错误!" << endl;
for (int i = 0; i <= n / 2; i++) {
temp = a[i];
//a[i]=a[n-i]; //注意数组越界
a[i] = a[n - 1-i];
a[n -1- i] = temp;
}
for (int i = 0; i <n; i++)
cout << a[i] << "\t";
system("pause");
return 0;
}
用结构体实现
#include<iostream>
#include<malloc.h>
using namespace std;
#define listsize 30
//函数状态码定义
#define true 1
#define false 0
#define ok 1
#define error 0
#define INFEASIBLE -1 //不可行
//#define OVERFLOW -2 //溢出
typedef int ElementType;
typedef int Status;
typedef struct {
ElementType data[listsize]; //
int length;//顺序表中元素个数
}Seqlist;
Status CreateList(Seqlist*l,int length);
void PrintSeqlist(Seqlist*l) {
if (CreateList(l, l->length) == true) {
for (int i = 0; i < l->length; i++) {
cout << l->data[i] << "\t";
}
cout << endl;
}
}
Status CreateList(Seqlist*l,int length) {
//注意 必须将length赋值给l->length 否则,死循环
l->length = length;
if (l->data == NULL) {
cerr << "内存分配错误!" << endl;
cout << "创建失败!" << endl;
return false;
}
else {
if (l->length > listsize)
return OVERFLOW;
else {
for (int i = 0; i < l->length; i++)
cin >> l->data[i];
}
PrintSeqlist(l);
cout << "创建成功!" << endl;
return true;
}
}
void Reverse_Seqlist(Seqlist*l) {
if (CreateList(l,l->length)==true) {
for (int i = 0; i < l->length / 2; i++) {
int temp = l->data[i];
l->data[i] = l->data[l->length - 1 - i];
l->data[l->length - 1 - i] = temp;
}
cout << "原地逆置结果:" << endl;
PrintSeqlist(l);
}
}
int main() {
Seqlist *L;
int length;
cout << "请输入顺序表中元素个数:" << endl;
cin >> length;
L = (Seqlist*)malloc(sizeof(ElementType)*listsize);
CreateList(L,length);
Reverse_Seqlist(L);
system("pause");
return 0;
}
用类模板实现
头文件“SeqList.h”
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
const int DefaultSize = 100;
#define true 1
#define false 0
typedef int Status;
typedef int Element;
template<class T>
class SeqList {
public:
//SeqList<T>();
SeqList()
;//构造函数
void SetValue(SeqList<T>&l,int m, int n) {
l.Maxsize=m;
l.size = n;
}
Status CreateSeqList(SeqList<T>& l,T m, T n);
void PrintSeqList(SeqList<T> &l);
void ReverseSeqList(SeqList<T>&l);
private:
Element*data;
T Maxsize;//数组总的存储空间
T size; //数据元素个数
};
template<class T>
Status SeqList<T>::CreateSeqList(SeqList<T> &l, T m,T n) {
l.size = n;
l.Maxsize = m;
if (m > DefaultSize)
return false;
else {
if (m < n)
return false;
else
{
data = new T [l.Maxsize];
for (int i = 0; i <l.size; i++)
cin >> data[i];
cout << "创建成功!" << endl;
}
}
PrintSeqList(l);
return true;
}
template<class T>
void SeqList<T>::PrintSeqList(SeqList<T> &l) {
for (int i = 0; i < l.size; i++)
cout << l.data[i] << "\t";
cout << endl;
}
template<class T>
void SeqList<T>::ReverseSeqList(SeqList<T>&l) {
for (int i = 0; i < l.size / 2; i++) {
int temp = l.data[i];
l.data[i] = l.data[size - 1 - i];
l.data[size - 1 - i] = temp;
}
cout << "原地逆置成功!" << endl;
cout << "结果:" << endl;
PrintSeqList(l);
}
#pragma once
源.cpp
#include"SeqList.h"
int main() {
int Maxsize, size;
cout << "请依次输入顺序表的内存量和数据元素个数:" << endl;
cin >> Maxsize >> size;
SeqList<int> L;
L.SetValue( L,Maxsize, size);
L.CreateSeqList(L, Maxsize,size);
L.ReverseSeqList(L);
system("pause");
return 0;
}
第三种实现时 遇到一个超级坑的bug:
c++中的类的成员函数和成员变量不能用->来访问(需要定义类成员访问函数operator ->)必须用成员运算符"."来访问。。。害得我想了半天。。。
还好今天总算 把类模板给搞清楚了。