单链表逆序

实现一:

#include "stdafx.h"
#include <iostream>
using namespace std;

template <typename T>
struct MyNode
{
T id;
MyNode * next;
MyNode(T _id)
{
id = _id;
next = NULL;
}
};

template <typename T>
class MyList
{
MyNode<T> * head;
MyNode<T> * current;
public:
MyList() : head(NULL) {}
void add(T value)
{
MyNode<T>* node = new MyNode<T>(value);
if(head == NULL)
{
head = node;
current = head;
}
else
{
current->next = node;
current = node;
}
}
void showAll()
{
MyNode<T> *go = head;
while(go != NULL)
{
cout<<go->id<<endl;
go = go->next;
}

}
void reverse()
{
MyNode<T> *preNode = NULL;
MyNode<T> *currentNode = head;
MyNode<T> *nextNode = head->next;
while(true)
{
nextNode = currentNode->next;
currentNode->next = preNode;
preNode = currentNode;
currentNode = nextNode;
if(currentNode->next == NULL)
{
head = currentNode;
head->next = preNode;
break;
}
}
}
};


int main(int argc, char * argv[])
{
MyList<int> *list = new MyList<int>();
list->add(123);
list->add(456);
list->add(789);
list->showAll();
list->reverse();
cout<<"after reverse:"<<endl;
list->showAll();
system("pause");
return 0;
}

实现二:

#include<iostream>
#include<conio.h>
#include<list>
using namespace std;

template<class T>
struct Node{
T value;
struct Node<T>* next;
Node(const T& v):value(v),next(NULL){}
~Node(){
delete next;
next=NULL;
cout<<"~Node()"<<endl;
}
};

template<class T,class N>
class Iterator{
public:
Iterator(T v=NULL):t(v){}
T& operator++()const{
t=t->next;
return t;
}
const N& operator*()const{
return t->value;
}
N&operator*(){
return t->value;
}
const T& getT()const{
return t;
}
Iterator& operator=(Iterator& other){
t=other.getT();
return *this;
}
const Iterator& operator=(Iterator& other)const{
t=other.getT();
return *this;
}
private:
mutable T t;
};

template<class T,class N>
bool operator!=(Iterator<T,N> i1,Iterator<T,N> i2){
return i1.getT()!=i2.getT();
}


template<class T>
class NList{
public:
typedef Iterator<Node<T>*,T> iterator;
typedef const Iterator<Node<T>*,T> const_iterator;
NList(const T& value){
head=tail=new Node<T>(value);
}
void addNode(const T& value){
tail->next=new Node<T>(value);
tail=tail->next;
}
iterator begin(){
return iterator(head);
}

iterator end(){
return iterator(tail->next);
}
void reverse(){
if(head==tail)
return ;
Node<T>* end=head,*begin=tail;
Node<T>* p1=head,*p2,*p3;
p2=p1->next;
p1->next=NULL;
while(p2!=tail){
p3=p2->next;
p2->next=p1;
p1=p2;
p2=p3;
}
p2->next=p1;
head=begin,tail=end;
}
~NList(){
delete head;
head=NULL;
tail=NULL;
cout<<"~NList()"<<endl;
}
private:
Node<T>* head;
Node<T>* tail;
};


int main(){
NList<int> nlist(1);
nlist.addNode(2);
nlist.addNode(3);
NList<int>::const_iterator p=nlist.begin(),end=nlist.end();
for(p=nlist.begin();p!=end;++p)
cout<<*p<<endl;
nlist.reverse();
end=nlist.end();
for(p=nlist.begin();p!=end;++p)
cout<<*p<<endl;
getch();
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值