码迷,mamicode.com
首页 > 编程语言 > 详细

reverse a linked-list(C++)

时间:2014-06-27 20:02:00      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:class   ext   os   name   c++   c   

#include<iostream>
using namespace std;
class Node
{
public:
Node(int value) : value(value), next(NULL) {}
public:
int value;
Node* next;
};
Node* reverseList(Node* head)
{
Node* newList = NULL;
Node* current = head;
while (current)
{
Node* next = current->next;
current->next = newList;
newList = current;
current = next;
}
return newList;
}
void printList(Node* head);
void listTests()
{
Node* one = new Node(1);
Node* two = new Node(2);
Node* three = new Node(3);
Node* four = new Node(4);
Node* five = new Node(5);
one->next = two;
two->next = three;
three->next = four;
four->next = five;
Node* head = one;
printList(head);
head = reverseList(head);
printList(head);
head = reverseList(head);
printList(head);
// cleanup memory
Node* current = head;
while (current)
{
Node* next = current->next;
delete current;
current = next;
}
}
void printList(Node* head)
{
Node* current = head;
while (current)
{
cout << current->value;
current = current->next;
}
cout << endl;
}

int main(){
listTests();
return 0;
}

reverse a linked-list(C++),布布扣,bubuko.com

reverse a linked-list(C++)

标签:class   ext   os   name   c++   c   

原文地址:http://www.cnblogs.com/strong-jeffrey/p/3809514.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!