2016-04-18 18 views
-1

これについても同様の質問が表示されましたが、私の試行では間違っていたので再度尋ねます。だから今はまったく異なる質問です。

これはPayrollオブジェクトのリンクリストに関する学校プロジェクトの一部です。私の教授からのフィードバックは、リンクリストをの中で、 InList関数の中をたどる必要があると述べました。私は彼が言っていることを理解していますが、それを行う方法は理解していません。

InList関数では、リンクリストとintを渡し、Payrollオブジェクトを作成し、intを従業員番号変数として割り当てます。次に、nodeTypeカレント・ポインタを使用して、リストをトラバースし、見つかった場合はレコードを出力します。

パブリッククラス関数を使用してnodeTypeを初期化する<Type>ポインタC++

void InList(const orderedLinkedList<Payroll>& P, int employee_number) 
    { 
    int listCount = P.length(); 
    Payroll object; 
    object.setEmployeeNumber(employee_number); 
    nodeType<Payroll> *current; //how to initialize this? 
    if (P.isEmptyList()) 
     cout << "Sorry, there is nothing in the list yet." << endl; 
    else 
    { 
     while (current != NULL && listCount > 0) 
     { 
      if (current->info.getEmployeeNumber() == employee_number) 
      { 
       current->info.printPayroll(); 
      } 
      listCount--; 
      current = current->link; 
     } //end while 
    } //end else 
    } 

そして、ここでは、リンクされたリストのためのすべての機能です。このヘッダーファイルにはメンバ変数はなく、最初と最後のポインタを持つ仮想基本クラスから派生していることに注意してください。また、最初と最後は基本クラスで保護されています。

template <class Type> 
bool orderedLinkedList<Type>::search(const Type& searchItem) const 
{ 
    bool found = false; 
    nodeType<Type> *current; //pointer to traverse the list 

    current = first; //start the search at the first node 

    while (current != NULL && !found) 
     if (current->info >= searchItem) 
      found = true; 
     else 
      current = current->link; 

    if (found) 
     found = (current->info == searchItem); //test for equality 

    return found; 
}//end search 

template <class Type> 
void orderedLinkedList<Type>::insert(const Type& newItem) 
{ 
    nodeType<Type> *current; //pointer to traverse the list 
    nodeType<Type> *trailCurrent = NULL;//pointer just before current 
    nodeType<Type> *newNode; //pointer to create a node 

    bool found; 

    newNode = new nodeType<Type>; //create the node 
    newNode->info = newItem; //store newItem in the node 
    newNode->link = NULL;  //set the link field of the node 
           //to NULL 

    if (first == NULL) //Case 1 
    { 
     first = newNode; 
     last = newNode; 
     count++; 
    } 
    else 
    { 
     current = first; 
     found = false; 

     while (current != NULL && !found) //search the list 
      if (current->info >= newItem) 
       found = true; 
      else 
      { 
       trailCurrent = current; 
       current = current->link; 
      } 

     if (current == first)  //Case 2 
     { 
      newNode->link = first; 
      first = newNode; 
      count++; 
     } 
     else      //Case 3 
     { 
      trailCurrent->link = newNode; 
      newNode->link = current; 

      if (current == NULL) 
       last = newNode; 

      count++; 
     } 
    }//end else 
}//end insert 

template<class Type> 
void orderedLinkedList<Type>::insertFirst(const Type& newItem) 
{ 
    insert(newItem); 
}//end insertFirst 

template<class Type> 
void orderedLinkedList<Type>::insertLast(const Type& newItem) 
{ 
    insert(newItem); 
}//end insertLast 

template<class Type> 
void orderedLinkedList<Type>::deleteNode(const Type& deleteItem) 
{ 
    nodeType<Type> *current; //pointer to traverse the list 
    nodeType<Type> *trailCurrent = NULL; //pointer just before current 
    bool found; 

    if (first == NULL) //Case 1 
     cout << "Cannot delete from an empty list." << endl; 
    else 
    { 
     current = first; 
     found = false; 

     while (current != NULL && !found) //search the list 
      if (current->info >= deleteItem) 
       found = true; 
      else 
      { 
       trailCurrent = current; 
       current = current->link; 
      } 

     if (current == NULL) //Case 4 
      cout << "The item to be deleted is not in the " 
      << "list." << endl; 
     else 
      if (current->info == deleteItem) //the item to be 
              //deleted is in the list 
      { 
       if (first == current)  //Case 2 
       { 
        first = first->link; 

        if (first == NULL) 
         last = NULL; 

        delete current; 
       } 
       else       //Case 3 
       { 
        trailCurrent->link = current->link; 

        if (current == last) 
         last = trailCurrent; 

        delete current; 
       } 
       count--; 
      } 
      else       //Case 4 
       cout << "The item to be deleted is not in the " 
       << "list." << endl; 
    } 
}//end deleteNode 

そして、ここで仮想基底クラスである:彼は私がメインプログラムからのすべての公開機能にアクセスし、リストの最初のポインタを返す関数を使用することができると述べ

#pragma once 
#ifndef H_LinkedListType 
#define H_LinkedListType 
#include <iostream> 
#include <cassert> 
using namespace std; 
//Definition of the node 
template <class Type> 
struct nodeType 
{ 
    Type info; 
    nodeType<Type> *link; 
}; 
template <class Type> 
class linkedListIterator 
{ 
public: 
    linkedListIterator(); 
    linkedListIterator(nodeType<Type> *ptr); 
    Type operator*(); 
    linkedListIterator<Type> operator++(); 
    bool operator==(const linkedListIterator<Type>& right) const; 
    bool operator!=(const linkedListIterator<Type>& right) const; 
private: 
    nodeType<Type> *current; //pointer to point to the current 
}; 
template <class Type> 
linkedListIterator<Type>::linkedListIterator() 
{ 
    current = NULL; 
} 
template <class Type> 
linkedListIterator<Type>:: 
linkedListIterator(nodeType<Type> *ptr) 
{ 
    current = ptr; 
} 
template <class Type> 
Type linkedListIterator<Type>::operator*() 
{ 
    return current->info; 
} 
template <class Type> 
linkedListIterator<Type> linkedListIterator<Type>::operator++() 
{ 
    current = current->link; 
    return *this; 
} 
template <class Type> 
bool linkedListIterator<Type>::operator== 
(const linkedListIterator<Type>& right) const 
{ 
    return (current == right.current); 
} 
template <class Type> 
bool linkedListIterator<Type>::operator!= 
(const linkedListIterator<Type>& right) const 
{ 
    return (current != right.current); 
} 
//***************** class linkedListType **************** 
template <class Type> 
class linkedListType 
{ 
public: 
    const linkedListType<Type>& operator= 
     (const linkedListType<Type>&); 
    void initializeList(); 
    bool isEmptyList() const; 
    void print() const; 
    int length() const; 
    void destroyList(); 
    Type front() const; 
    Type back() const; 
    virtual bool search(const Type& searchItem) const = 0; 
    virtual void insertFirst(const Type& newItem) = 0; 
    virtual void insertLast(const Type& newItem) = 0; 
    virtual void deleteNode(const Type& deleteItem) = 0; 
    linkedListIterator<Type> begin(); 
    linkedListIterator<Type> end(); 
    linkedListType(); 
    linkedListType(const linkedListType<Type>& otherList); 
    ~linkedListType(); 
protected: 
    int count; //variable to store the number of elements in the list 
    nodeType<Type> *first; //pointer to the first node of the list 
    nodeType<Type> *last; //pointer to the last node of the list 
private: 
    void copyList(const linkedListType<Type>& otherList); 
}; 
template <class Type> 
bool linkedListType<Type>::isEmptyList() const 
{ 
    return(first == NULL); 
} 
template <class Type> 
linkedListType<Type>::linkedListType() //default constructor 
{ 
    first = NULL; 
    last = NULL; 
    count = 0; 
} 
template <class Type> 
void linkedListType<Type>::destroyList() 
{ 
    nodeType<Type> *temp; //pointer to deallocate the memory 
          //occupied by the node 
    while (first != NULL) //while there are nodes in the list 
    { 
     temp = first;  //set temp to the current node 
     first = first->link; //advance first to the next node 
     delete temp; //deallocate the memory occupied by temp 
    } 
    last = NULL; //initialize last to NULL; first has already 
       //been set to NULL by the while loop 
    count = 0; 
} 
template <class Type> 
void linkedListType<Type>::initializeList() 
{ 
    destroyList(); //if the list has any nodes, delete them 
} 
template <class Type> 
void linkedListType<Type>::print() const 
{ 
    nodeType<Type> *current; //pointer to traverse the list 

    current = first; //set current so that it points to 
         //the first node 
    while (current != NULL) //while more data to print 
    { 
     cout << current->info << " "; 
     current = current->link; 
    } 
}//end print 
template <class Type> 
int linkedListType<Type>::length() const 
{ 
    return count; 
} //end length 
template <class Type> 
Type linkedListType<Type>::front() const 
{ 
    assert(first != NULL); 

    return first->info; //return the info of the first node 
}//end front 
template <class Type> 
Type linkedListType<Type>::back() const 
{ 
    assert(last != NULL); 

    return last->info; //return the info of the last node 
}//end back 
template <class Type> 
linkedListIterator<Type> linkedListType<Type>::begin() 
{ 
    linkedListIterator<Type> temp(first); 

    return temp; 
} 
template <class Type> 
linkedListIterator<Type> linkedListType<Type>::end() 
{ 
    linkedListIterator<Type> temp(NULL); 

    return temp; 
} 
template <class Type> 
void linkedListType<Type>::copyList 
(const linkedListType<Type>& otherList) 
{ 
    nodeType<Type> *newNode; //pointer to create a node 
    nodeType<Type> *current; //pointer to traverse the list 
    if (first != NULL) //if the list is nonempty, make it empty 
     destroyList(); 
    if (otherList.first == NULL) //otherList is empty 
    { 
     first = NULL; 
     last = NULL; 
     count = 0; 
    } 
    else 
    { 
     current = otherList.first; //current points to the list to be copied 
     count = otherList.count; 
     //copy the first node 
     first = new nodeType<Type>; //create the node 
     first->info = current->info; //copy the info 
     first->link = NULL;  //set the link field of the node to NULL 
     last = first;    //make last point to the first node 
     current = current->link;  //make current point to 
     while (current != NULL) 
     { 
      newNode = new nodeType<Type>; //create a node 
      newNode->info = current->info; //copy the info 
      newNode->link = NULL;  //set the link of newNode to NULL 
      last->link = newNode; //attach newNode after last 
      last = newNode;  //make last point tothe actual last node 
      current = current->link; //make current point to the next node 
     }//end while 
    }//end else 
}//end copyList 
template <class Type> 
linkedListType<Type>::~linkedListType() //destructor 
{ 
    destroyList(); 
}//end destructor 
template <class Type> 
linkedListType<Type>::linkedListType 
(const linkedListType<Type>& otherList) 
{ 
    first = NULL; 
    copyList(otherList); 
}//end copy constructor 
//overload the assignment operator 
template <class Type> 
const linkedListType<Type>& linkedListType<Type>::operator= 
(const linkedListType<Type>& otherList) 
{ 
    if (this != &otherList) //avoid self-copy 
    { 
     copyList(otherList); 
    }//end else 

    return *this; 
} 
#endif 

、現在のポインタを初期化する。それ、どうやったら出来るの?

ありがとうございます!

+0

仮想基本クラスのコードはありますか?ここでは、 'orderedLinkedList :: first'を公開しているメンバ関数はありません。あなたの教授が述べた関数は、基本クラスになければなりません。 –

+0

@ChristopherOiclesはい、編集して追加します。それも私の問題でした。どこに最初に戻ったのか分かりませんでした。 – Cynthia

+0

あなたのリストクラスには、リスト内の最初の***ノード***を取得する方法がありません。 'Type * head(){最初に戻ります。 } 'と' const型* head()const {最初に戻ります。 } '。 – kfsone

答えて

0

ああ、リンクリストには標準のbegin()関数が使用されています。この関数はイテレータを提供します。あなたのコードでは、イテレータを使用して各ノードを処理する必要があります。ノードポインタを直接処理することはありません。

だからあなたInListはイテレータを取得することによって開始します:

linkedListIterator<Payroll> iter = P.begin(); 

あなたが++iter

と次のノードにイテレータを進めるしかし、前*iter

とイテレータノードの給与オブジェクトにアクセスこれらのことを行うには、あなたがリストの最後にいないことを確認する必要があります。最後にないかどうかを確認するには、if(iter != P.end()) {...}

私はそれらを一緒にまとめるのをやめます - あなたの楽しみを台無しにしたくありません!

編集:

*iterは給与のオブジェクトにアクセスするための最良の方法ではないかもしれないように見えます。給与計算メンバーにアクセスするには、->演算子を使用できる必要があります。

何か

iter->info.getEmployeeNumber()などの編集

これを使用するために良いアイデアのように見えるしていませんが、これはあなたがconst参照からfirstを取得するためにorderedLinkedListに追加できるメンバ関数です。

宣言(パブリックセクションでは、class orderedLinkedList ... {ブロック内):

const nodeType<Type>* GetHeadNode() const; 

定義:

template <class Type> 
const nodeType<Type>* orderedLinkedList<Type>::GetHeadNode() const { 
    return first; 
} 

あなたはおそらくこれを使用して、あなたの給与クラスにアクセスしてください任意のメンバ関数を作成する必要があります。ポインタもconstです。あなたが実際にそれを行う必要がある場合は、をconst nodeType<Type>*から削除すると、それでも機能するはずです。

+0

私はあなたが言っていることを理解しています。私はまだポインタで弱いです。私の教授はnodeTypeポインタを強調していました。クラスメイトは、returnメソッドとしてnodeTypeを使ってorderedLinkedListに関数を作成し、最初に返すというアイディアを思いついた。それはうまくいくようですが、私はリスト変数でconstを使うことはできません。私もconstと闘います。 – Cynthia

+0

コンテナオブジェクトのユーザは通常、内部の動作から隔離されています。特に、イテレータが提供されている場合は、ポインタを直接処理することは決してありません。とにかく、orderedLinkedListコードの編集が許可されていれば、 'const orderedLinkedList 'から 'first'を得る方法で私の答えを編集します。 –

+0

私は、割り当てられたヘッダーファイルの機能内にあるコードを変更しないように言われてきましたが、その理由は、クラスのメソッドからユーザーエントリを削除したいからです。まずそれを検証してから関数を入力したいからです。しかし、はい、私はconstリストを最初に取得する方法を見ていただければ幸いです。 – Cynthia

関連する問題