Visual c++ 反转链表-迭代

Visual c++ 反转链表-迭代,visual-c++,linked-list,reverse,Visual C++,Linked List,Reverse,需要反转列表,然后打印出来,现在它以相同的顺序打印列表,我一直在到处搜索,但我仍然卡住了。我甚至不知道倒车是否正常。非常感谢您的帮助 #include <string> #include <iostream> #include <cassert> #include <list> using namespace std; class List; class Iterator; class Node{ public: /**

需要反转列表,然后打印出来,现在它以相同的顺序打印列表,我一直在到处搜索,但我仍然卡住了。我甚至不知道倒车是否正常。非常感谢您的帮助

#include <string>
#include <iostream>
#include <cassert>
#include <list>

using namespace std;

class List;
class Iterator;

class Node{
public:
   /** 
      Constructs a node with a given data value.
      @param s the data to store in this node
   */
   Node(string s);
private:
   string data;
   Node* previous;
   Node* next;
friend class List;
friend class Iterator;
};

class List{
public:
   /**
      Constructs an empty list;
   */
   List();
   /**
      Appends an element to the list.
      @param data the value to append
   */
   void push_back(string data);
   /**
      Inserts an element into the list.
      @param iter the position before which to insert
      @param s the value to append
   */
   void insert(Iterator iter, string s);
   /**
      Reverses the order of the elements in the list.
   */
   void reverse(Node *&head);
   /**
      Removes an element from the list.
      @param iter the position to remove
      @return an iterator pointing to the element after the
      erased element
   */
   Iterator erase(Iterator iter);
   /**
      Gets the beginning position of the list.
      @return an iterator pointing to the beginning of the list
   */
   Iterator begin();
   /**
      Gets the past-the-end position of the list.
      @return an iterator pointing past the end of the list
   */
   Iterator end();
private:
   Node* first;
   Node* last;
friend class Iterator;
};

class Iterator{
public:
   /**
      Constructs an iterator that does not point into any list.
   */
   Iterator();
   /**  
      Looks up the value at a position.
      @return the value of the node to which the iterator points
   */
   string get() const;
   /**
      Advances the iterator to the next node.
   */
   void next();
   /**
      Moves the iterator to the previous node.
   */
   void previous();
   /**
      Compares two iterators
      @param b the iterator to compare with this iterator
      @return true if this iterator and b are equal
   */
   bool equals(Iterator b) const;
private:
   Node* position;
   List* container;
friend class List;
};

Node::Node(string s){  
   data = s;
   previous = NULL;
   next = NULL;
}

List::List(){  
   first = NULL;
   last = NULL;
}

void List::push_back(string data){  
   Node* new_node = new Node(data);
   if (last == NULL) // List is empty
   {  
      first = new_node;
      last = new_node;
   }
   else{  
      new_node->previous = last;
      last->next = new_node;
      last = new_node;
   }
}

void List::insert(Iterator iter, string s){  
   if (iter.position == NULL)   {  
      push_back(s);
      return;
   }

   Node* after = iter.position;
   Node* before = after->previous;
   Node* new_node = new Node(s);
   new_node->previous = before;
   new_node->next = after;
   after->previous = new_node;
   if (before == NULL) // Insert at beginning
      first = new_node;
   else
      before->next = new_node;
}

void List::reverse(Node *&head){
    if((head == NULL)||(head->next=NULL))
        return;
    Node *top = head;
    Node *tail = NULL;
    Node *curr = head;
    Node *temp = NULL;

    while(curr != NULL){
        temp = curr->next;
        curr->next = curr->previous;
        curr->previous = temp;
        if(temp == NULL){
            tail = curr;
        }
        curr = temp;
    } 
    temp = head;
    head = tail;
    tail = temp;
}

Iterator List::erase(Iterator iter){  
   assert(iter.position != NULL);
   Node* remove = iter.position;
   Node* before = remove->previous;
   Node* after = remove->next;
   if (remove == first)
      first = after;
   else
      before->next = after;
   if (remove == last)
      last = before;
   else
      after->previous = before;
   delete remove;
   Iterator r;
   r.position = after;
   r.container = this;
   return r;
}

Iterator List::begin(){  
   Iterator iter;
   iter.position = first;
   iter.container = this;
   return iter;
}

Iterator List::end(){  
   Iterator iter;
   iter.position = NULL;
   iter.container = this;
   return iter;
}

Iterator::Iterator(){  
   position = NULL;
   container = NULL;
}

string Iterator::get() const{  
   assert(position != NULL);
   return position->data;
}

void Iterator::next(){  
   assert(position != NULL);
   position = position->next;
}

void Iterator::previous(){  
   assert(position != container->first);
   if (position == NULL)
      position = container->last;
   else 
      position = position->previous;
}

bool Iterator::equals(Iterator b) const{  
   return position == b.position;
}

int main(){

   List staff;

   // Add orginal values to staff
   staff.push_back("Tom");
   staff.push_back("Dick");
   staff.push_back("Harry");
   staff.push_back("Juliet");

   Iterator pos;

   // Print all values
   for (pos = staff.begin(); !pos.equals(staff.end()); pos.next())
      cout << pos.get() << "\n";

   // Reverse the values
    Node* head = NULL;
    staff.reverse(head);

   // Print all values to show reverse
   cout << endl;
   for (pos = staff.begin(); !pos.equals(staff.end()); pos.next())
      cout << pos.get() << "\n";

   return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
班级名单;
类迭代器;
类节点{
公众:
/** 
使用给定的数据值构造节点。
@param是要存储在此节点中的数据
*/
节点(字符串s);
私人:
字符串数据;
节点*先前;
节点*下一步;
好友类列表;
友元类迭代器;
};
班级名单{
公众:
/**
构造一个空列表;
*/
List();
/**
将元素追加到列表中。
@param data要附加的值
*/
void push_back(字符串数据);
/**
在列表中插入一个元素。
@参数插入前的位置
@param是要附加的值
*/
void insert(迭代器iter,字符串s);
/**
反转列表中元素的顺序。
*/
无效反向(节点*&头部);
/**
从列表中删除一个元素。
@参数设置要删除的位置
@返回一个迭代器,该迭代器指向
擦除元件
*/
迭代器擦除(迭代器iter);
/**
获取列表的开始位置。
@返回指向列表开头的迭代器
*/
迭代器begin();
/**
获取超过列表结束位置的值。
@返回指向列表末尾的迭代器
*/
迭代器end();
私人:
节点*第一;
节点*最后;
友元类迭代器;
};
类迭代器{
公众:
/**
构造一个不指向任何列表的迭代器。
*/
迭代器();
/**  
查找某个位置的值。
@返回迭代器指向的节点的值
*/
字符串get()常量;
/**
将迭代器前进到下一个节点。
*/
下一步无效();
/**
将迭代器移动到上一个节点。
*/
作废先前的();
/**
比较两个迭代器
@param b用于与此迭代器进行比较的迭代器
@如果此迭代器和b相等,则返回true
*/
布尔等于(迭代器b)常数;
私人:
节点*位置;
清单*集装箱;
好友类列表;
};
Node::Node(字符串s){
数据=s;
previous=NULL;
next=NULL;
}
列表::列表(){
第一个=空;
last=NULL;
}
void List::push_back(字符串数据){
节点*新节点=新节点(数据);
如果(last==NULL)//列表为空
{  
第一个=新的_节点;
last=新的_节点;
}
否则{
新建节点->上一个=最后一个;
last->next=新建_节点;
last=新的_节点;
}
}
void List::insert(迭代器iter,字符串s){
如果(iter.position==NULL){
推回;
返回;
}
节点*after=iter.position;
节点*before=after->previous;
节点*新节点=新节点;
新建节点->上一个=之前;
新建节点->下一步=之后;
after->previous=新建_节点;
if(before==NULL)//在开头插入
第一个=新的_节点;
其他的
before->next=新建_节点;
}
无效列表::反向(节点*&头部){
如果((head==NULL)| |(head->next=NULL))
返回;
节点*顶部=头部;
Node*tail=NULL;
节点*curr=头部;
节点*temp=NULL;
while(curr!=NULL){
温度=当前->下一步;
当前->下一步=当前->上一步;
当前->先前=温度;
if(temp==NULL){
尾=电流;
}
电流=温度;
} 
温度=水头;
头=尾;
尾=温度;
}
迭代器列表::擦除(迭代器iter){
断言(iter.position!=NULL);
节点*remove=iter.position;
节点*before=remove->previous;
节点*after=删除->下一步;
如果(删除==第一个)
第一个=之后;
其他的
之前->下一步=之后;
如果(删除==最后一次)
最后=之前;
其他的
之后->之前=之前;
删除删除;
迭代器r;
r、 位置=之后;
r、 容器=这个;
返回r;
}
迭代器列表::begin(){
迭代器iter;
iter.position=第一;
iter.container=this;
返回iter;
}
迭代器列表::end(){
迭代器iter;
iter.position=NULL;
iter.container=this;
返回iter;
}
迭代器::迭代器(){
位置=空;
container=NULL;
}
字符串迭代器::get()常量{
断言(位置!=NULL);
返回位置->数据;
}
void迭代器::next(){
断言(位置!=NULL);
位置=位置->下一步;
}
void迭代器::previous(){
断言(位置!=容器->第一);
如果(位置==NULL)
位置=容器->最后一个;
其他的
位置=位置->上一个;
}
布尔迭代器::等于(迭代器b)常量{
返回位置==b位置;
}
int main(){
工作人员名单;
//将原始值添加到staff
工作人员。推回(“汤姆”);
工作人员。推回(“迪克”);
工作人员。推回(“哈利”);
工作人员。推回(“朱丽叶”);
迭代器位置;
//打印所有值
对于(pos=staff.begin();!pos.equals(staff.end());pos.next())

cout好的,
List::reverse
方法的第一条语句检查
head==NULL
,如果是,则返回而不修改列表。而您正在传入
NULL
。这显然是个问题。我不明白为什么您认为需要传入
head
参数,但如果需要,y您可能希望将其初始化为列表的
第一个

一方面,您的代码在进入反向方法时未通过测试,因为(head==NULL)并立即返回您没有更改
列表的
第一个
最后一个
指针。