1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
class StatehoodInfo
{
public:
char Order[3];
char StateName[15];
char StateAbbrev[3];
char DateOfEntry[11];
public:
StatehoodInfo()
{
strncpy(Order,"00",3);
strncpy(StateName,"Unknown",15);
strncpy(StateAbbrev,"00",3);
strncpy(DateOfEntry,"Unknown",11);
}
StatehoodInfo(char * o,char * SName, char * SAbbrev, char * Date)
{
strncpy(Order,o,3);
strncpy(StateName,SName,15);
strncpy(StateAbbrev,SAbbrev,3);
strncpy(DateOfEntry,Date,11);
}
StatehoodInfo(StatehoodInfo& rhs)
{
strncpy(this->Order,rhs.Order,3);
strncpy(this->StateName,rhs.StateName,15);
strncpy(this->StateAbbrev,rhs.StateAbbrev,3);
strncpy(this->DateOfEntry,rhs.DateOfEntry,11);
}
~StatehoodInfo()
{
}
char * getorder(void) { return Order; }
void setorder(char * o)
{
strncpy(Order,o,3);
return;
}
char * getstateName(void) { return StateName; }
void setstateName(char * SName)
{
strncpy(StateName,SName,15);
return;
}
char * getstateAbbrev(void) { return StateAbbrev; }
void setstateAbbrev(char * SAbbrev)
{
strncpy(StateAbbrev,SAbbrev,3);
return;
}
char * getdate(void) { return DateOfEntry; }
void setdate(char * Date)
{
strncpy(DateOfEntry,Date,11);
return;
}
void ShowStateInfo(ostream& target)
{
target << "State: " << Order << setw(5) << StateName << setw(5)
<< StateAbbrev << setw(5) <<" was admitted on " << DateOfEntry<<endl;
return;
}
};
template <typename T>
struct ListNode
{
T thePayload;
ListNode * theLink;
};
void AppendNode(ListNode<StatehoodInfo> * ,ListNode<StatehoodInfo> * );
int main()
{
const int MAXSIZE = 51;
StatehoodInfo States[MAXSIZE];
int sub = 0;
fstream BinIn;
ListNode<StatehoodInfo> * headPtr = NULL;
ListNode<StatehoodInfo> * currentNodePtr;
ListNode<StatehoodInfo> * newNodePtr;
ListNode<StatehoodInfo> * previousNodePtr;
BinIn.open(".\\Datafiles\\USA-Statehood-Info-LAB-10-Spring-2014.bin",ios::in|ios::binary);
if(BinIn.fail())
{
cout<<"Unable to Process\n";
return 999;
}
while(BinIn.eof() == false)
{
BinIn.read(reinterpret_cast<char *>(&States[sub]),sizeof(StatehoodInfo));
sub++;
}
int maxElements = sub;
int index;
while(index < maxElements) //reading file
{
//Declare a new ListNode object
newNodePtr = new ListNode<StatehoodInfo>; //Create a instance of the Node object on heap
if(newNodePtr->theLink == NULL)
{
cout << "\nUnable to create the Node object on heap.\n";
return 999;
}
/*newNodePtr->thePayload.Order = States[index];
newNodePtr->thePayload.StateName = States[index];
newNodePtr->thePayload.StateAbbrev = States[index];
newNodePtr->thePayload.DateOfEntry = States[index];*/
newNodePtr->theLink = NULL; //ListNode object's link member set to NULL
if(headPtr == NULL)
{
//List is empty
headPtr = newNodePtr; //assign the ADDRESS of new Node object to headPtr ptr
currentNodePtr = newNodePtr; //ditto for currentNodePtr pointer
}
else
{
//List has at least one ListNode. APPEND the new ListNode to the END OF THE LIST
cout << "\nAPPEND a NODE.\n\n";
AppendNode(headPtr,newNodePtr);
}
States[index].ShowStateInfo(cout);
index++; //Get next payload value for another ListNode
} //End List create loop
system("CLS");
//Traversal
currentNodePtr = headPtr; //Store the entry address into currentNodePtr ptr (to protect contents of headPtr ptr)
while(currentNodePtr != NULL)
{
States[index].ShowStateInfo(cout);
currentNodePtr = currentNodePtr->theLink; //'point' to NEXT Node
}
}
void AppendNode(ListNode<StatehoodInfo> * theHeadPtr,ListNode<StatehoodInfo> * theNewNode)
{
//APPEND a new Node to the END of the List
ListNode<StatehoodInfo> * currentNodePtr = theHeadPtr;
ListNode<StatehoodInfo> * previousNodePtr;
//Find the 'end' of the current List
while(currentNodePtr != NULL)
{
previousNodePtr = currentNodePtr; //Save address of the NODE PRECEDING the current Node
currentNodePtr = currentNodePtr->theLink;
}
//AT END of present List..'previousNodePtr' is address of former 'last' ListNode object
theNewNode->theLink = currentNodePtr; //New Node is now the FINAL (LAST) Node with link == NULL
previousNodePtr->theLink = theNewNode; //Store address of the new ListNode object in the link member
//of the previous ListNode objct
return;
}
|