2013-03-04 75 views
0

我需要重载Node类中的++运算符,以便在我调用它时返回下一个节点的指针。以及 - 返回前一个。我根本不熟悉重载操作符,所以我对如何解决这个问题非常失望。我认为我的语法有点正确,但我不确定如何实现它。我是否使用它的会员功能?任何帮助将不胜感激!Node类中的重载运算符

我的节点类:

#ifndef NODE_H 
#define NODE_H 
class Node 
{ 
public: 
    Node(Node* n = NULL, int v =0) 
    { 
     next = n; 
     value = v; 
    } 

    LinkedList& operator++(const LinkedList) 

    Node* next; 
    Node* prev; 
    int value; 
}; 

#endif 

不知道是否有帮助,但这里是我LinkedList文件:

#ifndef LinkedList_H 
#define LinkedList_H 
#include "Node.h" 
#include <iostream> 

using namespace std; 
class LinkedList{ 

public: 
    LinkedList() 
    { 
     front = NULL; 
     back = NULL; 
     size = 0; 
    } 

    void push_front(int item) 
    { 
     if (front == NULL) 
     { 
      front = new Node(NULL, item); 
      back = front; 
      size++; 
      return; 
     } 

     Node* newNode = new Node(NULL, item); 
     front->prev = newNode; 
     newNode->next = front; 
     front = newNode; 
     size++; 
    } 

    int pop_front() 
    { 
     if (front == NULL){ 
      cout<<"No item to pop "<<endl; 
      return 0; 
     } 

     Node *temp = front; 
     int value = front->value; 
     if(front->next){ 
      front = front->next; 
      front->prev = NULL; 
      size--; 
      delete temp; 
      return value; 
     } 

     front = NULL; 
     back = NULL; 
     return value; 
    } 

    int pop_back() 
    { 
     if(front == NULL){ 
      cout<<"Nothing to pop! "; 
      return 0; 
     } 
     else{ 
      Node *prev = front; 
      Node *succ = front->next; 
      while(succ->next != NULL){ 
       succ = succ->next; 
       prev = prev->next; 
      } 
      int value = succ->value; 
      prev->next = NULL; 
      back = prev; 

      delete succ; 
      size--; 
      return value; 
     } 
    } 

    void push_back(int item) 
    { 
     if (front == NULL) 
     { 
      front = new Node(NULL, item); 
      size++; 
      return; 
     } 
     else { 

      Node* newnode = new Node(NULL, item); 
      Node *succ = front; 
      while(succ->next != NULL){ 
       succ = succ->next; 
      } 
      succ->next = newnode; 
      newnode->next = NULL; 
      newnode->prev = succ; 
      back = newnode; 
      size++; 
     } 
    } 

    void print() 
    { 
      if (front == NULL){ 
      cout<<"Nothing to print! "<<endl; 
     } 

     Node *p = front; 
     while(p){ 
      cout<<p->value<<" "; 
      p=p->next; 
     } 

     cout<<endl<<endl; 
    } 

    bool removeNode(int i){ 

     if (i < 0 || i >= size) //assume size is a data member of double linked 
      return false; 
     if (front == NULL) 
      return false; 
     if (i == 0) 
     { 
      Node* p = front; //assume front is a data member of double linked list 
      front = front->next; 
      front-> prev = NULL; 
      delete p; 
      size--; 
      if (size == 1) 
       back = front; 
      return true; 
     } 

     int temp=0; 
     Node* curr = front; 
     Node* p = NULL; 
     while (temp!= (i-1) && curr->next !=NULL) 
     { 
      p = curr; 
      curr = curr->next; 
      temp++; 
     } 

     p->next = curr->next; 
     curr->next->prev = p; 
     delete curr; 
     size--; 
     if (size == 1) 
      back = front; 
     return true; 
    } 

    LinkedList& operator++(const LinkedList) 

    Node* front; //front of a linked list 
    Node* back; 
    int size; //# of nodes in a linked list 
}; 

#endif 

回答

0

我认为,它看起来应该像下面这样:

#ifndef NODE_H 
#define NODE_H 
class Node 
{ 
public: 
    Node(Node* n = NULL, int v =0) 
    { 
     // Where do you set prev? 
     next = n; 
     value = v; 
    } 

    Node * operator ++ (int) 
    { 
     return next; 
    } 

    Node * operator -- (int) 
    { 
     return prev; 
    } 

    Node* next; 
    Node* prev; 
    int value; 
}; 

#endif 

进一步了解运营商在C++ FAQ上超载。

+0

如果我的答案解决了您的问题,请不要忘记接受它(单击答案投票计数下方的“V”标记)。在SO上这样做通常是一个好习惯。 – Spook 2013-03-04 06:36:31

+0

将在4分钟内完成:) – user2130537 2013-03-04 06:37:29

+0

只是一个简单的问题。我如何在我的LL课中打电话给我?我所尝试的一切似乎都会使我的程序崩溃 – user2130537 2013-03-04 06:46:19