#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;
}
线性表中元素地址逆置
最新推荐文章于 2024-10-08 00:02:21 发布