Any help is greatly appreciated..
How do I read this information int a Doubly Linked List? I don't
even know where to start.
Code:
#include<iomanip.h>
#include<string>
#include<fstream>
#include"DubDV_listP.C"
ifstream IF ("csc23565_lab4.txt");
ofstream OF ("OUTPUT_lab4.txt");
typedef struct{
string lastName;
string phoneNumber;
string course[15];
} Person;
main()
{
int RecCount=1;
Person Rec;
if( !IF )
{cout<<"cannot open file"<<endl;
exit;}
// loop for Records of Person
while ( IF >> Rec.lastName)
{ OF<<Rec.lastName << " ";
cout <<" ";
cout <<Rec.lastName<<" ";
IF >>Rec.phoneNumber;
OF<<Rec.phoneNumber << " ";
cout <<Rec.phoneNumber<<" ";
// loop through the courses for this Record
int courseIndex=0; // reseting the array index to zero
IF >>Rec.course[courseIndex];
while (Rec.course[courseIndex] != "XXX111")
{cout<<Rec.course[courseIndex]<<" ";
OF<<Rec.course[courseIndex];
courseIndex++;
IF>>Rec.course[courseIndex];
}// end of course loop
OF << endl;
cout << endl;
RecCount++;
c++;
}// end of while loop for records of Person
}
The sample info in the txt file is basically a directory of students:
Arthur 1234567890 MTH235 PHY235 SOC235 XXX111
Grant 1234567890 PSY235 ART235 XX111
Johns 1234567890 CSC235 XXX111
and the file "DubDV_listP.C" I'm including is :
Code:
typedef int DlistItemType;
class DlistNode; // linked list node
typedef DlistNode* ptrType; // pointer to node
class DlistNode // a node on the list
{
friend class DlistClass;
DlistItemType DItem; // a data item on the list
ptrType DNext; // pointer to next node
ptrType DPrev; // pointer to prev node
}; // end class listNode
class DlistClass
{
public:
// constructors and destructor:
DlistClass(); // default constructor
DlistClass(const DlistClass& L); // copy constructor
~DlistClass(); // destructor
// list operations:
bool DListIsEmpty() const;
int DListLength() const;
void DListInsert(int NewPosition, DlistItemType NewItem,
bool& Success);
void DListDelete(int Position, bool& Success);
void DListRetrieve(int Position, DlistItemType& DataItem,
bool& Success) const;
private:
int Size; // number of items in list
ptrType Head; // pointer to linked list of items
ptrType Tail;
ptrType DPtrTo(int Position) const;
// Returns a pointer to the Position-th node
// in the linked list.
}; // end class
DlistClass::DlistClass(const DlistClass& L): Size(L.Size)
{
if (L.Head == NULL)
Head = NULL; // original list is empty
else
{ // copy first node
Head = new DlistNode;
assert(Head != NULL); // check allocation
Head->DItem = L.Head->DItem;
// copy rest of list
ptrType NewPtr = Head; // new list pointer
// NewPtr points to last node in new list
// OrigPtr points to nodes in original list
for (ptrType OrigPtr = L.Head->DNext;
OrigPtr != NULL;
OrigPtr = OrigPtr->DNext)
{ NewPtr->DNext = new DlistNode;
assert(NewPtr->DNext != NULL);
NewPtr = NewPtr->DNext;
NewPtr->DItem = OrigPtr->DItem;
} // end for
NewPtr->DNext = NULL;
} // end if
} // end copy constructor
DlistClass::~DlistClass()
{
bool Success;
while (!DListIsEmpty())
DListDelete(1, Success);
} // end destructor
bool DlistClass::DListIsEmpty() const
{
return bool(Size == 0);
} // end ListIsEmpty
int DlistClass::DListLength() const
{
return Size;
} // end ListLength
ptrType DlistClass::DPtrTo(int Position) const
// --------------------------------------------------
// Locates a specified node in a linked list.
// Precondition: Position is the number of the
// desired node.
// Postcondition: Returns a pointer to the desired
// node. If Position < 1 or Position > the number of
// nodes in the list, returns NULL.
// --------------------------------------------------
{
if ( (Position < 1) || (Position > DListLength()) )
return NULL;
else // count from the beginning of the list
{ ptrType Cur = Head;
for (int Skip = 1; Skip < Position; ++Skip)
Cur = Cur->DNext;
return Cur;
} // end if
} // end PtrTo
void DlistClass::DListRetrieve(int Position,
DlistItemType& DataItem,
bool& Success) const
{
Success = bool( (Position >= 1) &&
(Position <= DListLength()) );
if (Success)
{ // get pointer to node, then data in node
ptrType Cur = DPtrTo(Position);
DataItem = Cur->DItem;
} // end if
} // end ListRetrieve
void DlistClass::DListInsert(int NewPosition,
DlistItemType NewItem,
bool& Success)
{
int NewLength = DListLength() + 1;
Success = bool( (NewPosition >= 1) &&
(NewPosition <= NewLength) );
if (Success)
{ // create new node and place NewItem in it
ptrType NewPtr = new DlistNode;
Success = bool(NewPtr != NULL);
if (Success)
{ Size = NewLength;
NewPtr->DItem = NewItem;
// attach new node to list
if (NewPosition == 1)
{ // insert new node at beginning of list
NewPtr->DNext = Head;
Head = NewPtr;
}
else
{ ptrType Prev = DPtrTo(NewPosition-1);
// insert new node after node
// to which Prev points
NewPtr->DNext = Prev->DNext;
Prev->DNext = NewPtr;
} // end if
} // end if
} // end if
} // end ListInsert
void DlistClass::DListDelete(int Position,
bool& Success)
{
ptrType Cur;
Success = bool( (Position >= 1) &&
(Position <= DListLength()) );
if (Success)
{ --Size;
if (Position == 1)
{ // delete the first node from the list
Cur = Head; // save pointer to node
Head = Head->DNext;
}
else
{ ptrType Prev = DPtrTo(Position-1);
// delete the node after the
// node to which Prev points
Cur = Prev->DNext; // save pointer to node
Prev->DNext = Cur->DNext;
} // end if
// return node to system
Cur->DNext = NULL;
delete Cur;
Cur = NULL;
} // end if
} // end ListDelete
// END of listClass Implementation Code
If you've read this far, thank you. I don't normally post this much code, but I just don't know where to start with my problem.. i've been drawing blanks since yesterday.