数组元素原地逆置

最简单的利用数组实现

#include
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 

用结构体实现

#include
#include
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
#include
#include
using namespace std;
const int DefaultSize = 100;
#define true 1
#define false 0
typedef int Status;
typedef int Element;
template
class SeqList {
public:
//SeqList();
	SeqList()
	;//构造函数
	void SetValue(SeqList&l,int m, int n) {
		l.Maxsize=m;
		l.size = n;
	}
	Status CreateSeqList(SeqList& l,T m, T n); 
	void PrintSeqList(SeqList &l);
	void ReverseSeqList(SeqList&l);
private:
	Element*data;
	T Maxsize;//数组总的存储空间
	T size;  //数据元素个数
};
template
Status SeqList::CreateSeqList(SeqList &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 > data[i];
			cout << "创建成功!" << endl;
		}
	}
	PrintSeqList(l);
	return true;
}
template
void SeqList::PrintSeqList(SeqList &l) {
	for (int i = 0; i < l.size; i++)
		cout << l.data[i] << "\t";
	cout << endl;
}
template
void SeqList::ReverseSeqList(SeqList&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 L;
	L.SetValue( L,Maxsize, size);
	L.CreateSeqList(L, Maxsize,size);
	L.ReverseSeqList(L);


	system("pause");
	return 0;
}

数组元素原地逆置_第1张图片
第三种实现时 遇到一个超级坑的bug:
c++中的类的成员函数和成员变量不能用->来访问(需要定义类成员访问函数operator ->)必须用成员运算符"."来访问。。。害得我想了半天。。。

还好今天总算 把类模板给搞清楚了。

你可能感兴趣的:(数组元素原地逆置)