双循环链表就地逆置C++算法,急急急
// test2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
struct Node{
int data;
Node *pre;
Node *next;
Node(int d) :data(d),pre(NULL), next(NULL){
}
};
class NodeList{
private:
Node * head;
Node * tail;
public:
NodeList():head(NULL), tail(NULL){
}
virtual ~NodeList(){
Node * p = head;
while( p != NULL){
Node * temp = p->next;
delete p;
p = temp;
}
head = tail = NULL;
}
void add(Node const & node){
if(head == NULL){
head = new Node(node);
tail = head;
}else{
Node *p = new Node(node);
p->pre = tail;
p->next = NULL;
tail->next = p;
tail = p;
}
}
void display(){
Node * p = head;
while( p != NULL){
cout << p->data << endl;
p = p->next;
}
}
Node * revert(){
Node * p = head;
while( p != NULL){
Node * next = p->next;
Node * pre = p->pre;
p->pre = next;
p->next = pre;
p = next;
}
Node* temp = tail;
tail = head;
head = temp;
return head;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
NodeList list;
for( int i = 0; i < 10; ++i){
Node node(i);
//node.data = i;
list.add(node);
}
list.display();
list.revert();
list.display();
system("pause");
return 0;
}