Pergunta

I tested my class member function to add to a linked list outside of my switch and it works. But when I try to use it in my switch, it doesn't. It won't actually add to the list cause when I display the list nothing is there.

Class and implementation:

#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

Driver:

#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;
}

Any ideas?

Foi útil?

Solução

Move your list declaration outside of your do..while loop. At the moment it is being reinitialized each time round the loop.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top