Expand|Select|Wrap|Line Numbers
- // test-linked-list.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include "list1.h"
- using std::cout;
- using std::cin;
- using std::endl;
- void testIntegerList();
- void instructions();
- int _tmain(int argc, _TCHAR* argv[])
- {
- testIntegerList();
- return 0;
- }
- void testIntegerList()
- {
- cout << "Testing a List of integer values" << endl;
- List1<int> integerList;
- instructions();
- int choice, value;
- do {
- cout << "? ";
- cin >> choice;
- switch (choice) {
- case 1:
- cout << "Enter an integer: ";
- cin >> value;
- integerList.insertAtFront(value);
- integerList.print();
- break;
- case 2:
- cout << "Enter an integer: ";
- cin >> value;
- integerList.insertAtBack(value);
- integerList.print();
- break;
- case 3:
- if (integerList.removeFromFront(value))
- cout << "integer removed from front" << endl;
- integerList.print();
- break;
- case 4:
- if (integerList.removeFromBack(value))
- cout << "integer removed from back" << endl;
- integerList.print();
- break;
- }
- } while (choice != 5);
- cout << "End test of integer list" << endl;
- }
- void instructions()
- {
- cout << "Enter one of the following:" << endl
- << " 1 to insert at beginning of list" << endl
- << " 2 to insert at the end of list" << endl
- << " 3 to delete from beginning of list" << endl
- << " 4 to delete from end of list" << endl
- << " 5 to end list processing" << endl;
- }
- // list1.h
- #ifndef LIST1_H
- #define LIST1_H
- #include "listnd.h"
- template<typename T>
- class List1 {
- public:
- List1();
- ~List1();
- void insertAtFront(T &);
- void insertAtBack(T &);
- int removeFromFront(T &);
- int removeFromBack(T &);
- int isEmpty();
- void print();
- private:
- ListNode<T> *firstPtr;
- ListNode<T> *lastPtr;
- ListNode<T> *getNewNode(T &);
- };
- #endif
- // list1.cpp
- #include "stdafx.h"
- #include "list1.h"
- #include <iostream>
- #include <assert.h>
- using std::cout;
- using std::endl;
- template<typename T>
- List1<T>::List1() {firstPtr = lastPtr = 0;}
- template<typename T>
- List1<T>::~List1()
- {
- if (!isEmpty()) {
- cout << "Destroying nodes ... " << endl;
- ListNode<NODETYPE> *currentPtr = firstPtr, *tempPtr;
- while (currentPtr != 0) {
- tempPtr = currentPtr;
- cout << tempPtr->data << endl;
- currentPtr = currentPtr->nextPtr;
- delete tempPtr;
- }
- }
- cout << "All nodes destroyed" << endl << endl;
- }
- template<typename T>
- void List1<T>::insertAtFront(T &value)
- {
- ListNode<NODETYPE> *newPtr = getNewNode(value);
- newPtr->nextPtr = firstPtr;
- firstPtr = newPtr;
- }
- template<typename T>
- void List1<T>::insertAtBack(T &value)
- {
- ListNode<NODETYPE> *newPtr = getNewNode(value);
- lastPtr->nextPtr = newPtr;
- //newPtr->nextPtr = 0;
- lastPtr = newPtr;
- }
- template<typename T>
- int List1<T>::removeFromFront(T &value)
- {
- if (isEmpty())
- return 0;
- else {
- ListNode<NODETYPE> *tempPtr = firstPtr;
- if (firstPtr == lastPtr)
- firstPtr = lastPtr = 0;
- else
- firstPtr = firstPtr->nextPtr;
- value = tempPtr->data;
- delete tempPtr;
- return 1;
- }
- }
- template<typename T>
- int List1<T>::removeFromBack(T &value)
- {
- if (isEmpty())
- return 0;
- else {
- ListNode<NODETYPE> *tempPtr = lastPtr;
- if (firstPtr == lastPtr)
- firstPtr = lastPtr = 0;
- else {
- ListNode<NODETYPE> *currentPtr = firstPtr;
- while (currentPtr->nextPtr != lastPtr)
- currentPtr = currentPtr->nextPtr;
- lastPtr = currentPtr;
- currentPtr->nextPtr = 0;
- }
- value = tempPtr-data;
- delete tempPtr;
- return 1;
- }
- }
- template<typename T>
- int List1<T>::isEmpty() {return firstPtr == 0;}
- template<typename T>
- ListNode<NODETYPE> *List1<T>::getNewNode(T value)
- {
- ListNode<NODETYPE> *ptr = new ListNode<NODETYPE>(value);
- assert(ptr != 0);
- return ptr;
- }
- template<typename T>
- void List1<T>::print()
- {
- if (isEmpty()) {
- cout << "The list is empty" << endl << endl;
- return;
- }
- ListNode<NODETYPE> *currentPtr = firstPtr;
- cout << "The list is: ";
- while (currentPtr != 0) {
- cout << currentPtr->data << ' ';
- currentPtr = currentPtr->nextPtr;
- }
- cout << endl << endl;
- }
- // listnd.h
- #ifndef LISTND_H
- #define LISTND_H
- template<typename NODETYPE>
- class ListNode {
- friend class List1<T>;
- public:
- ListNode(NODETYPE &);
- ~ListNode();
- NODETYPE getData();
- private:
- NODETYPE data;
- ListNode *nextPtr;
- };
- #endif
- // listnd.cpp
- #include "stdafx.h"
- #include "listnd.h"
- template<typename NODETYPE>
- ListNode<NODETYPE>::ListNode(NODETYPE &info)
- {
- data = info;
- nextPtr = 0;
- }
- template<typename NODETYPE>
- NODETYPE ListNode<NODETYPE>::getData() {return data;}
That's the codes. I get errors like the following:
Expand|Select|Wrap|Line Numbers
- 1>Compiling...
- 1>list1.cpp
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\listnd.h(6) : error C2059: syntax error : '<'
- 1> e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\listnd.h(14) : see reference to class template instantiation 'ListNode<NODETYPE>' being compiled
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\listnd.h(6) : error C2238: unexpected token(s) preceding ';'
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\list1.h(22) : error C2989: 'List1' : class template has already been declared as a non-class template
- 1> e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\listnd.h(6) : see declaration of 'List1'
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\list1.h(7) : error C3857: 'List1': multiple template parameter lists are not allowed
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\list1.cpp(9) : error C2988: unrecognizable template declaration/definition
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\list1.cpp(9) : error C2059: syntax error : '<'
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\list1.cpp(12) : error C2588: '::~List1' : illegal global destructor
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\list1.cpp(12) : fatal error C1903: unable to recover from previous error(s); stopping compilation
- 1>listnd.cpp
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\listnd.h(6) : error C2059: syntax error : '<'
- 1> e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\listnd.h(14) : see reference to class template instantiation 'ListNode<NODETYPE>' being compiled
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\listnd.h(6) : error C2238: unexpected token(s) preceding ';'
- 1>test-linked-list.cpp
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\listnd.h(6) : error C2059: syntax error : '<'
- 1> e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\listnd.h(14) : see reference to class template instantiation 'ListNode<NODETYPE>' being compiled
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\listnd.h(6) : error C2238: unexpected token(s) preceding ';'
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\list1.h(22) : error C2989: 'List1' : class template has already been declared as a non-class template
- 1> e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\listnd.h(6) : see declaration of 'List1'
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\list1.h(7) : error C3857: 'List1': multiple template parameter lists are not allowed
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(25) : error C2143: syntax error : missing ';' before '<'
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(25) : error C2143: syntax error : missing ';' before '<'
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(41) : error C2065: 'integerList' : undeclared identifier
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(41) : error C2228: left of '.insertAtFront' must have class/struct/union
- 1> type is ''unknown-type''
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(42) : error C2065: 'integerList' : undeclared identifier
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(42) : error C2228: left of '.print' must have class/struct/union
- 1> type is ''unknown-type''
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(48) : error C2065: 'integerList' : undeclared identifier
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(48) : error C2228: left of '.insertAtBack' must have class/struct/union
- 1> type is ''unknown-type''
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(49) : error C2065: 'integerList' : undeclared identifier
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(49) : error C2228: left of '.print' must have class/struct/union
- 1> type is ''unknown-type''
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(53) : error C2065: 'integerList' : undeclared identifier
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(53) : error C2228: left of '.removeFromFront' must have class/struct/union
- 1> type is ''unknown-type''
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(55) : error C2065: 'integerList' : undeclared identifier
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(55) : error C2228: left of '.print' must have class/struct/union
- 1> type is ''unknown-type''
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(59) : error C2065: 'integerList' : undeclared identifier
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(59) : error C2228: left of '.removeFromBack' must have class/struct/union
- 1> type is ''unknown-type''
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(62) : error C2065: 'integerList' : undeclared identifier
- 1>e:\documents and settings\user\my documents\visual studio 2008\projects\test-linked-list\test-linked-list.cpp(62) : error C2228: left of '.print' must have class/struct/union
- 1> type is ''unknown-type''
- 1>Generating Code...
- 1>Build log was saved at "file://e:\Documents and Settings\user\My Documents\Visual Studio 2008\Projects\test-linked-list\Debug\BuildLog.htm"
- 1>test-linked-list - 32 error(s), 0 warning(s)
Thank you for taking time to look at this. I have been looking at this for a couple days now and really need some help here. Thank you!
Qiong