线性表中元素地址逆置

#include <iostream>
#include <cmath>
#include<iomanip>
using namespace std;
enum  error_code1 { success, arrangeerror };
template<class T>
struct node
{
	T data;
	node* next;
	node* prior;

};
template<class T>
class list
{
public:
	int count;
	node<T>* head;
	list()
	{
		head=new node<T>;
		head->next = head;
		head->prior = head;
		count = 0;

	}
	int length()const
	{
		return count;
	}
	error_code1 getelement(const int i, T& x)//按序号取元素
	{
		node<T>* p = new node<T>;
		p = head->next;
		int j = 1;
		while (p != NULL && j != i)
		{
			p = p->next;
			j++;
		}
		if (p == NULL)
			return arrangeerror;
		else
		{
			x = p->data;
			return success;
		}
	}

	error_code1 insert( int i,T x)//插入算法
	{
		int j = 0;
		node<T>* p = head;
		while (j != i - 1 && p != NULL)
		{
			p = p->next;
			j++;
		}
		node<T>* s = new node<T>;
		s->data = x;
		s->prior = p->prior;
		s->next = p;
		p->prior = s;
		s->prior->next = s;
		count++;

			 return success;
	}
	error_code1 mydelete(const int i)//删除结点
	{
		node<T>* p = new node<T>;
		p = head;
		
			p->prior->next = p->next;
			p->next->prior = p->prior;
			delete p;
			count--;
			return success;
	}
	error_code1 nizhi()
	{
		node<T>* s = head;
		for (int i = 0; i <= count; i++)
		{
			node<T>* p = s->prior;
			node<T>* n = s->next;
			s->prior = n;
			s->next = p;
			s = s->prior;
		}
		return success;
	}
	error_code1 print()
	{
		cout << "头结点地址:" << head << endl;
		node<T>* p = head;
		if (count > 0)
		{
			for (int i = 1; i <= count; i++)
			{
				p = p->next;
				cout << "第" << i << "个结点的地址:" << p << ",结点数值:" << p->data << endl;
			}
		}
		return success;
	}
	
};
int main()
{
	list<int>l;
	cout << "请输入需要插入的五个数" << endl;
	for (int i = 0; i < 5; i++)
	{
		int m;
			cin >> m;
			l.insert(1, m);
	}
	l.print();
	l.nizhi();
	l.print();

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值