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
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
struct listType
{
string last = "last";
string first = "first";
int id = 0;
listType *link;
};
void createList(listType *&head, listType *&tail)
{
head = new listType;
tail = new listType;
head -> last = "AAA";
head -> first = "AAA";
head -> id = 0;
tail -> last = "zzz";
tail -> first = "zzz";
tail -> id = 0;
head -> link = tail;
tail -> link = NULL;
cout << "create list" << endl;
}
void insertList(listType *head, listType *tail, string last, string first, int id)
{
listType *prior, *knew, *next;
knew = new listType;
knew -> last = last;
knew -> first = first;
knew -> id = id;
prior = head;
next = head -> link;
while (next != tail && knew -> id > next -> id)
{
prior = next;
next = next -> link;
}
prior -> link = knew;
knew -> link = next;
}
void readEm(listType *head, listType *tail)
{
listType *c, *prior, *next;
string last, first;
int id;
string pos;
ifstream inf("program2.dat");
while(!inf.eof())
{
inf >> last >> first >> id;
last.erase(last.find(","), 1);
insertList(head, tail, last, first, id);
cout << id << endl;
}
}
bool emptyList(listType *head, listType *tail)
{
return head->link == tail;
}
void traversEm(listType *head, listType *tail, ofstream &outf)
{
listType *c;
if (!emptyList(head, tail));
{
c = head->link;
while (c != tail)
{
cout << c-> id << " " << c-> first << " " << c-> last << endl;
outf << c -> id << " " << c-> first << " " << c -> last << endl;
c = c->link;
}
//else
{
cout << "Empty List" << endl;
outf << "Empty List" << endl;
}
system("pause");
}
}
void deleteList(listType *head, listType *tail, string last, string first, int id)
{
listType *c, *prior, *next;
c = prior -> link;
c -> last = last;
c -> first = first;
c -> id = id;
prior = head;
next = head->link;
while (next != tail && c -> id != next->id)
{
prior = next;
next = next->link;
}
prior->link = c;
delete c;
}
int main()
{
cout << "1st action" << endl;
//string last, first;
//int id;
//ofstream outf("program2.ot");
//listType *c, *head, *tail;
cout << "before list" << endl;
//createList(head, tail);
//emptyList(head, tail);
//readEm(head, tail);
//traversEm(head, tail, outf);
//insertList(555, Luke, Skywalker);
//traversEm(head, tail, outf);
//deleteList(800, Leia, Organa);
//traversEm(head, tail, outf);
return 0;
system("pause");
}
|