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
|
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
using namespace std;
struct listtype
{
string first;
string last;
int id;
listtype *link;
};
void createlist(listtype *&head, listtype *&tail)
{
head = new listtype;
tail = new listtype;
head->first = " AAA";
tail->first = "zzz";
head->last = " AAA";
tail->last= "zzz";
head->link = tail;
tail->link = nullptr;
head->id = -1;
tail->id = 9999;
}
bool emptylist(listtype *head, listtype *tail)
{
return (head->link == tail);
}
// declare thje function
void insertlist(listtype *head, listtype *tail, int id, string firstname, string lastname) ;
void readem(listtype *head, listtype *tail)
{
ifstream inf("data.txt");
string last, first;
int id = 0;
while (!inf.eof())
{
inf >> last >> first >> id;
insertlist(head, tail, id, first, last);
}
}
void traverselist(listtype *head, listtype *tail, ofstream &outf)
{
listtype *c;
if (!emptylist (head, tail))
{
///////////////////////////////////////////////////////////////////////////////
head->link = c; // *** warning *** variable 'c' is uninitialized when used here
/////////////////////////////////////////////////////////////////////////////////
outf << setw(15) << "First" << setw(20) << "Last" << endl << setw(15) << "ID"
<< setw(15) << "Name" << setw(15) << "Name" << endl;
for (int i = 0; i < 29; i++) outf << "-";
outf << endl;
while (c != tail)
{
outf << c->id << setw(10) << c->first << setw(15) << c->last << endl;
c->link = c;
}
for (int i = 0; i < 29; i++) outf << "-";
outf << endl;
}
}
void deletelist(ofstream &outf, listtype *head, listtype *tail, string lastname)
{
listtype *prior, *next, *c;
c->last = lastname;
prior = head;
head->link = c;
////////////////////////////////////////////////////////////////////////////
c->link = next; // *** warning *** variable 'next' is uninitialized when used here
///////////////////////////////////////////////////////////////////////////
while (next != tail && c->last < lastname)
{
prior = c;
c->link = c;
next->link = next;
}
if (c != tail && c->last == lastname)
{
prior->link = next;
delete c;
}
else
{
outf << lastname << " is not in the list and could not be removed" << endl;
}
}
void insertlist(listtype *head, listtype *tail, int id, string firstname, string lastname)
{
listtype *knew, *prior, *next;
knew = new listtype;
knew->id = id;
knew->first = firstname;
knew->last = lastname;
prior = head;
head->link = next;
while (next != tail && knew->last > next->last)
{
prior = next;
next->link = next;
}
prior->link = knew;
knew->link = next;
}
int main()
{
ofstream outf;
outf.open("output.txt");
listtype *head, *tail;
createlist(head, tail);
emptylist(head, tail);
readem(head, tail);
traverselist(head, tail, outf);
insertlist(head, tail, 555, "Luke", "Skywalker");
traverselist(head, tail, outf);
deletelist(outf, head, tail, "Organa");
traverselist(head, tail, outf);
system("pause");
return 0;
}
|