Created
September 25, 2011 18:10
-
-
Save rosonowski/1240915 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "list.h" //templated linked list class, provided by professor. | |
#include <fstream> | |
#include <string> | |
using namespace std; | |
string getFileName() | |
{ | |
string fileName; | |
cout << "Enter the file path and name."; | |
cin >> fileName; | |
return fileName; | |
} | |
int main() | |
{ | |
List<string> listOfStrings; | |
ifstream inFile; | |
inFile.open(getFileName()); | |
string tempString; | |
inFile >> tempString; | |
listOfStrings.insertAfter(tempString); | |
cout << listOfStrings.examine(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1>------ Build started: Project: CS_132_lab4, Configuration: Debug Win32 ------ | |
1>Build started 9/25/2011 4:13:38 PM. | |
1>InitializeBuildStatus: | |
1> Touching "Debug\CS_132_lab4.unsuccessfulbuild". | |
1>ClCompile: | |
1> lab4_client_driver.cpp | |
1> Generating Code... | |
1> Compiling... | |
1> list.cpp | |
1> Generating Code... | |
1>ManifestResourceCompile: | |
1> All outputs are up-to-date. | |
1>lab4_client_driver.obj : error LNK2019: unresolved external symbol "public: __thiscall List<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::~List<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(void)" (??1?$List@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAE@XZ) referenced in function _main | |
1>lab4_client_driver.obj : error LNK2019: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * __thiscall List<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::examine(void)" (?examine@?$List@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAEPAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function _main | |
1>lab4_client_driver.obj : error LNK2019: unresolved external symbol "public: void __thiscall List<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::insertAfter(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?insertAfter@?$List@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main | |
1>lab4_client_driver.obj : error LNK2019: unresolved external symbol "public: __thiscall List<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::List<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(void)" (??0?$List@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAE@XZ) referenced in function _main | |
1>C:\Users\David\Documents\Visual Studio 2010\Projects\CS_132_lab4\Debug\CS_132_lab4.exe : fatal error LNK1120: 4 unresolved externals | |
1> | |
1>Build FAILED. | |
1> | |
1>Time Elapsed 00:00:01.16 | |
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef LIST_H_ | |
#define LIST_H_ | |
#include <iostream> | |
using std:: cout; | |
using std::endl; | |
template <class BaseData> | |
class List | |
{ | |
protected: | |
struct ListNode | |
{ | |
public: | |
BaseData listData; | |
ListNode *link; | |
}; | |
public: | |
List (); | |
List (List &init); | |
~List (); | |
void first (); | |
void last (); | |
void makeCurrent(int position); | |
void prev (); | |
void next (); | |
int current (); | |
int count (); | |
void insertBefore(const BaseData &item); | |
void insertAfter(const BaseData &item); | |
void remove (); | |
void replace (BaseData &item); | |
BaseData * examine (); | |
List<BaseData>& operator = (List<BaseData> &source); | |
void destroy(); | |
protected: | |
ListNode *head, *currentNode, *previous; | |
int numNodes; | |
int currentPos; | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef LIST_T_ | |
#define LIST_T_ | |
template <class BaseData> | |
List <BaseData>::List() | |
{ | |
previous = 0; | |
currentNode = 0; | |
head = 0; | |
numNodes = 0; | |
currentPos = 0; | |
} | |
template <class BaseData> | |
List <BaseData>::List(List<BaseData> &init) | |
{ | |
if (this == &init) return; | |
ListNode *newList, *current, *newNode; | |
current = init.head; | |
newList = 0; | |
head = 0; | |
while (current) | |
{ | |
newNode = new ListNode; | |
newNode->listData = current->listData; | |
newNode->link = 0; | |
if (newList) | |
{ | |
newList->link = newNode; | |
newList = newList->link; | |
} | |
else newList = newNode; | |
if (current == init.head) | |
head = newNode; | |
current = current->link; | |
} | |
numNodes = init.numNodes; | |
currentPos = 0; | |
previous = 0; | |
currentNode = 0; | |
} | |
template <class BaseData> | |
void List <BaseData>::insertBefore(const BaseData &item) | |
{ | |
ListNode *p; | |
p = new ListNode; | |
p->listData = item; | |
if (numNodes) | |
{ | |
if (head == currentNode) head = p; | |
p->link = currentNode; | |
if (previous) previous ->link = p; | |
++numNodes; | |
currentNode = p; | |
} | |
else | |
{ | |
head = p; | |
p->link = 0; | |
previous = 0; | |
++numNodes; | |
currentNode = p; | |
} | |
} | |
template <class BaseData> | |
BaseData * List<BaseData>::examine() | |
{ | |
BaseData *temp; | |
if (currentNode) | |
{ | |
temp = new BaseData; | |
*temp = currentNode->listData; | |
return (temp); | |
} | |
else | |
return 0; | |
} | |
template <class BaseData> | |
List <BaseData>::~List() | |
{ | |
destroy(); | |
} | |
template <class BaseData> | |
void List<BaseData>::destroy() | |
{ | |
ListNode *temp; | |
currentNode = head; | |
while (currentNode) | |
{ | |
temp = currentNode; | |
currentNode = currentNode->link; | |
delete temp; | |
} | |
previous = 0; | |
currentNode = 0; | |
head = 0; | |
numNodes = 0; | |
currentPos = 0; | |
} | |
template <class BaseData> | |
void List <BaseData>::first() | |
{ | |
if (numNodes) | |
{ | |
previous = 0; | |
currentNode = head; | |
currentPos = 1; | |
} | |
else | |
currentPos = 0; | |
} | |
template <class BaseData> | |
void List <BaseData>::last() | |
{ | |
while (currentNode->link) | |
{ | |
previous = currentNode; | |
currentNode = currentNode->link; | |
} | |
currentPos = numNodes; | |
} | |
template <class BaseData> | |
void List<BaseData>::makeCurrent (int position) | |
{ | |
if (( position < 1) || (position > numNodes)) | |
cout << "invalid position: "<< endl; | |
else | |
{ | |
first(); | |
for (int i = 1; i < position; i++) | |
{ | |
previous = currentNode; | |
currentNode = currentNode->link; | |
} | |
currentPos = position; | |
} | |
} | |
template <class BaseData> | |
void List<BaseData>::prev() | |
{ | |
int tempCurrPos = currentPos; | |
if (currentPos > 1) | |
{ | |
ListNode *temp = previous; | |
first(); | |
if (currentNode == temp) | |
{ | |
previous = 0; | |
currentNode = temp; | |
} | |
else | |
{ | |
while (currentNode->link != temp) | |
currentNode = currentNode->link; | |
previous = currentNode; | |
currentNode = temp; | |
} | |
currentPos = tempCurrPos -1; | |
} | |
else | |
{ | |
cout << "walking over front of list"; | |
currentPos = 0; | |
} | |
} | |
template <class BaseData> | |
void List<BaseData>::next() | |
{ | |
if (currentNode->link) | |
{ | |
previous = currentNode; | |
currentNode = currentNode->link; | |
currentPos++; | |
} | |
else | |
{ | |
cout << "walking over end of list"; | |
currentPos = 0; | |
} | |
} | |
template <class BaseData> | |
int List<BaseData>::current() | |
{ | |
return (currentPos); | |
} | |
template <class BaseData> | |
int List<BaseData>::count() | |
{ | |
return (numNodes); | |
} | |
template <class BaseData> | |
void List<BaseData>::insertAfter(const BaseData &item) | |
{ | |
ListNode *p; | |
p = new ListNode; | |
p->listData = item; | |
if (numNodes) | |
{ | |
p->link = currentNode->link; | |
currentNode->link = p; | |
++numNodes; | |
previous = currentNode; | |
currentNode = p; | |
currentPos++; | |
} | |
else | |
{ | |
head = p; | |
p->link = 0; | |
previous = 0; | |
++numNodes; | |
currentNode = p; | |
currentPos++; | |
} | |
} | |
template <class BaseData> | |
void List<BaseData>::remove() | |
{ | |
ListNode *p, *temp; | |
p = currentNode; | |
if (numNodes) //there are nodes | |
{if (previous) //this is not the first node in the list | |
{ //any other node in list but first | |
previous->link = currentNode->link; | |
if (currentNode->link != 0) | |
currentNode = currentNode->link; | |
else //deleting last node in list | |
{ | |
currentPos--; | |
currentNode = previous; | |
temp = head; | |
if (temp == currentNode) | |
previous = 0; | |
else | |
{ | |
while (temp->link != currentNode && temp) | |
temp = temp->link; | |
previous = temp; | |
} | |
} | |
delete p; | |
--numNodes; | |
} | |
else | |
{ //delete first node in list | |
head = head->link; | |
delete p; | |
currentNode = head; | |
--numNodes; | |
//if first and last node in list | |
if (!numNodes) currentPos = 0; | |
} | |
} | |
else cout << "empty list" << endl; | |
} | |
template <class BaseData> | |
void List<BaseData>::replace(BaseData &item) | |
{ | |
if (currentNode) | |
currentNode->listData = item; | |
} | |
template <class BaseData> | |
List<BaseData>& List<BaseData>:: operator = (List<BaseData> &init) | |
{ | |
if (this == &init) return *this; | |
ListNode *temp, *newList, *current, *newNode; | |
currentNode = head; | |
while (currentNode) //delete existing left side list | |
{ | |
temp = currentNode; | |
currentNode = currentNode->link; | |
delete temp; | |
} | |
current = init.head; | |
newList = 0; | |
while (current) //copy list | |
{ newNode = new ListNode; | |
newNode->listData = current->listData; | |
newNode->link = 0; | |
if (newList) | |
{ | |
newList->link = newNode; | |
newList = newList->link; | |
} | |
else newList = newNode; | |
if (current == init.head) | |
head = newNode; | |
current = current->link; | |
} | |
numNodes = init.numNodes; | |
currentPos = 0; | |
previous = 0; | |
currentNode = 0; | |
return *this; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment