2014-04-24 2 views
0

내 클래스 멤버 함수를 테스트하여 스위치 외부의 연결된 목록에 추가하면 작동합니다. 그러나 스위치에서 사용하려고 할 때는 그렇지 않습니다. 목록을 표시 할 때 실제로 목록에 아무 것도 추가되지 않습니다.스위치의 연결된 목록에 추가 할 수 없습니다.

클래스 및 구현 :

#ifndef HEADER_H 
#define HEADER_H 

#include "stdafx.h" 
#include <iostream> 
#include <string> 

using namespace std; 

template <class T> 
class LinkedList{ 
     template<typename T> 
     class Node 
     { 
     public: 
       Node(T value = 0) : data(value), nextptr(NULL){}; 
       T retrieve() const{ return data; } 
       Node<T> *next() const{ return nextptr; } 
     private: 
       T data; 
       Node<T> *nextptr; 
       friend LinkedList<T>; 
     }; 
public: 
     LinkedList(); 
     LinkedList(const T& x); //Copy Constructor 
     ~LinkedList(); 
     void DisplayList(); 
     void ReverseList(); 
     LinkedList<T> operator= (const LinkedList<T> & x); //Assignment Operator 
     bool isEmpty() const{ return Size() == 0; } 
     int Size() const{ return n; } 
     bool ElementAt(int k, T& x) const; 
     LinkedList<T>& RemoveAt(int k, T& x) 
     { 
       if (k < 0 || k >= Size()) 
       { 
         cout << "Index not in list."; 
       } 

       Node<T> *del = NULL; 
       if (k == 0) 
       { 
         del = list_head; 
         list_head = del->nextptr; 
       } 
       else 
       { 
         Node<T> *prev = list_head; 
         del = list_head->nextptr; 
         for (int i = 1; i< k; i++) 
         { 
           prev = del; 
           del = del->nextptr; 
         } 
         prev->nextptr = del->nextptr; 
       } 
       n--; x = del->data; delete del; 
       return *this; 
     } 

     LinkedList<T>& Add(const T& x) 
     { 
       Node<T> 
         *node = new Node<T>(x); 
       if (Size() == 0) 
         list_head = node; 
       else 
       { 
         Node<T> *temp = list_head; 
         while (temp->nextptr) 
         { 
           temp = temp->nextptr; 
         } 
         temp->nextptr = node; 
       } 
       n++; 
       return *this; 
     } 


private: 
     Node<T> *list_head; 
     int n; 
}; 

//Constructor 
template<class T> 
LinkedList<T>::LinkedList() 
{ 
     list_head = NULL; 
     n = 0; 
} 

//Copy Constructor 
template<class T> 
LinkedList<T>::LinkedList(const T& x) 
{ 
     list_head = x.listhead; 
     n = x.n; 
} 

//Destructor 
template<class T> 
LinkedList<T>::~LinkedList() 
{ 
     Node<T> *temp = list_head, *del, *nextptr; 

     while (temp != NULL) 
     { 
       del = temp->nextptr; 
       delete temp; 
       temp = del; 
     } 
} 

template<class T> 
bool LinkedList<T>::ElementAt(int k, T& x) const 
{ 
     if (k < 0 || k >= Size()) 
       return false; 

     Node<T> *temp = list_head; 

     for (int i = 0; i< k; i++) 
     { 
       temp = temp->next; 
     } 
     x = temp->data; 
     return true; 
} 

// Assignment Operator 
template<class T> 
LinkedList<T> LinkedList<T>::operator=(const LinkedList<T> & x) 
{ 
     list_head = x.list_head; 
     n = x.n; 

     return *this; 
} 

template<class T> 
void LinkedList<T>::DisplayList() 
{ 
     Node<T> *temp = list_head; 

     while (temp != NULL) 
     { 
       cout << temp->data << endl; 
       temp = temp->nextptr; 
     } 
} 

template<class T> 
void LinkedList<T>::ReverseList() 
{ 
     Node<T> *t, *y = list_head, *r = NULL, *listhead; 

     while (y != NULL) 
     { 
       t = y->nextptr; 
       y->nextptr = r; 
       r = y; 
       y = t; 
     } 
     list_head = r; 
} 

#endif 

드라이버 :

#include "stdafx.h" 
#include <iostream> 
#include "Header.h" 

using namespace std; 

int main() 
{ 
     int choice; 
     string item; 
     do 
     { 
       LinkedList<string> list; 


       int num; 

       cout << "1. Add new record to the file" << endl; 
       cout << "2. Delete a record in the file (by index)" << endl; 
       cout << "3. Display entire list of items" << endl; 
       cout << "4. Display entire list of items backwards" << endl; 
       cout << "5. Exit" << endl; 
       cin >> choice; 

       switch (choice) 
       { 
       case 1: 
       { 
            cout << "1. Add new record to the file" << endl; 
            cout << "Enter the Item Description:" << endl; 
            cin.ignore(); 
            getline(cin, item); 
            list.Add(item); 
       } 
         break; 
       case 2: 
       { 
            cout << "2. Delete a record in the file" << endl; 
            cout << "Enter the index number of the item you want to delete" << endl; 
            cin >> num; 
            //list.RemoveAt(num); 
       } 
         break; 
       case 3: 
       { 
            cout << "3. Display entire list of items" << endl; 
            list.DisplayList(); 
       } 
         break; 
       case 4: 
       { 
            cout << "4. Display entire list of items backwards" << endl; 
            list.ReverseList(); 
            list.DisplayList(); 
            list.ReverseList(); 
       } 
         break; 
       case 5: 
       { 
            return 0; 
       } 
         break; 
       } 
     } while (0 < choice < 6); 


     return 0; 
} 

어떤 아이디어?

+0

스위치 케이스 내부의 'cout' 텍스트가 화면에 인쇄됩니까? 그렇다면 스위치 케이스가 트리거되고 switch 문 내부에있는 것이 중요하지 않습니다. ** 정확한 ** 일련의 호출을 스위치 외부에 추가해야하는데, 이는 재현해야합니다. 그런 다음 호출되지 않는 코드를 제거하여 코드를 줄여야합니다. 그런 다음 입력을 하드 코딩해야 입력을 제공 할 필요없이 프로그램을 실행할 수 있습니다. 그리고 원하는 결과와 실제 결과를 보여줘야합니다. – Dukeling

답변

1

list 선언을 do..while 루프 외부로 이동하십시오. 현재 루프가 반복 될 때마다 다시 초기화됩니다.

+0

mg. 이 일을 처음으로하는 것은 아닙니다. 나는 너무 부끄럽다. 감사합니다 형제 <3 – user3374108

관련 문제