linkedList.h
#include <iostream>
#include <assert.h>
using namespace std;
template <class T>
class linkedList
{
private:
struct listNode // declare structure for nodes
{
T value;
listNode * next;
listNode(T value1, listNode * next1 = NULL)
{
value = value1; // for info in node
next = next1; // for link in node
}
};
listNode * head; // List head pointer
listNode * last; // list last pointer
int count;
public:
linkedList() { head = NULL; } //Constructor
~linkedList(); // Destructor
void addNode(T value);
void displayFirst() const;
void displayLast() const;
};
template<class T>
void linkedList<T>::displayFirst() const
{
listNode *nodePtr; // to move through the list
nodePtr = head;
cout << "The first value is " << head->value;
}
template <class T>
void linkedList<T>::displayLast() const
{
//listNode * nodePtr = head; // pointer at head
listNode *nodePtr; // to move through the list
nodePtr = head;
while (nodePtr != NULL)
{
nodePtr = nodePtr->next; // move to next node
if (nodePtr->next = (NULL))
{
nodePtr = last; // <------ skips here!!
}
cout << "The last value is " << last->value;
} // end while
}
//----------------------------------------------
// Adds a new element to the end of the list.
//----------------------------------------------
template <class T>
void linkedList<T>::addNode(T value)
{
if (head == NULL) // is the list is empty
head = new listNode(value);
else // The list is not empty.
{
// Use nodePtr to traverse the list
listNode * nodePtr = head;
while (nodePtr->next != NULL)
nodePtr = nodePtr->next;
// nodePtr->next is NULL so nodePtr points to the last node.
// Create a new node and put it after the last node.
nodePtr->next = new listNode(value);
} // end else
} // end addNode
//------------------------------------------------------
// Destructor deallocates the memory used by the list.
//------------------------------------------------------
template <class T>
linkedList<T>::~linkedList()
{
count = 0;
listNode * nodePtr = head; // Start at head of list
while (nodePtr != NULL)
{
// trash keeps track of node to be deleted
listNode * trash = nodePtr;
nodePtr = nodePtr->next; // Move to the next node
delete trash;
} // end while
} // end destructor
main.cpp
#include <iostream>
#include <cstdlib>
using namespace std;
#include "linkedList.h"
void repeat(); // function to repeat program
int main()
{
linkedList<string> list;
linkedList<string> otherList;
string name;
cout << "Add 5 names to the list:\n";
for (int count = 0; count < 5; count++)
{
cout << "Name #" << count + 1 << ": ";
getline(cin, name);
list.addNode(name);
}
list.displayFirst();
cout << endl;
list.displayLast();
// list.~linkedList();
repeat();
return 0;
}
//--------------- repeat --------------------------------------
// function to repeat program
//-------------------------------------------------------------
void repeat()
{
char x;
cout << "Would you like to run the program again? [y/n]: ";
cin >> x;
cin.ignore(10, '\n');
cout << endl;
if (x == 'y' || x == 'Y')
main();
else if (x == 'n' || x == 'N')
{
cout << "Goodbye";
exit(0);
}
else if (x != 'n' || x != 'N' || x != 'y' || x != 'Y')
{
cout << "Invalid choice. ";
repeat(); // function call to repeat to repeat
}
} // end void repeat

New Topic/Question
Reply



MultiQuote






|