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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
#ifndef PERSON_H
#define PERSON_H
struct PersonRec
{
char name[20];
int bribe;
PersonRec* link;
}
;
class PersonList {
public:
PersonList();
~PersonList();
void ViewList();
void AddToList ();
private:
PersonRec* head;
void Advance(PersonRec*);
void Reset(PersonRec*);
void PrevPtr(PersonRec*);
PersonRec* InsertBefore(PersonRec*,PersonRec*,PersonRec**);
void InsertAfter(PersonRec*,PersonRec*);
PersonRec* CurrRec();
/*
insert here other private member functions as you
see the need for them. However, you cannot add
any additional member variables or public
member functions than the four listed above
*/
};
#endif
//Here are my functions:
#include "personlist.h"
#include <iostream>
using namespace std;
PersonList::PersonList()
{
head=NULL;
}
PersonList::~PersonList()
{
for(head;head!=NULL;head=head->link)
{
delete head;
}
}
PersonRec* PersonList::InsertBefore(PersonRec* aPtr,PersonRec* currPtr, PersonRec** currPtr2)
{
PersonRec ** headPointer=&head;
PersonRec *temp = new PersonRec;
temp->bribe = currPtr->bribe;
temp->link = currPtr->link;
currPtr= aPtr;
currPtr->link=temp;
**currPtr2=*aPtr;
currPtr2=&(head->link);
**currPtr2=*currPtr;
**currPtr2=**headPointer;
return currPtr;
}
void PersonList::InsertAfter(PersonRec* a,PersonRec* b)
{
}
void PersonList::Reset(PersonRec* a)
{
a=head;
}
void PersonList::ViewList()
{
if(head==NULL)
{
cout<<"The list is empty. "<<endl;
}
else if(head!=NULL)
{
for(head;head!=NULL;head=head->link)
{
for(int x=0;x<20;x++)
{
cout<<head->name[x];
}
cout<<" "<<head->bribe;
}
}
}
void PersonList::Advance(PersonRec* currPtr)
{
currPtr=currPtr->link;
}
void PersonList::AddToList()
{
PersonRec* aPtr=new PersonRec;
PersonRec** theHead=&head;
char aName[20];
int aBribe;
cout << "\nEnter the person's name: ";
cin.getline(aName, 20);
cout<< "\nEnter the person's contribution: ";
cin >> aBribe;
aPtr->bribe=aBribe;
for(int x=0;x<20;x++)
{
aPtr->name[x]=aName[x];
}
PersonRec* currPtr=new PersonRec;
PersonRec** currPtr2=&head;
currPtr=head;
if(head!=NULL)
{
while(currPtr!=NULL)
{
if(currPtr->bribe < aPtr->bribe)
{
PersonRec *Checker=InsertBefore(aPtr,currPtr,currPtr2);
cout<<Checker->bribe<<endl;
cout<<Checker->name[0];
cout<<Checker->name[1];
cout<<head->bribe<<endl;
cout<<head->name[0];
cout<<head->name[1];
currPtr=NULL;
}
else if(currPtr->bribe > aPtr->bribe)
{
Advance(currPtr);
}
}
}
else
{
head=aPtr;
//head->link=NULL;
}
currPtr2=theHead;
}
//Here is my driver.
#include "PersonList.h"
#include <iostream>
using namespace std;
int displayMenu (void);
void processChoice(int, PersonList&);
int main ( )
{
int num;
PersonList myList;
do
{
num = displayMenu();
if (num != 3)
processChoice(num, myList);
} while (num != 3);
return 0;
}
int displayMenu (void)
{
int choice;
cout << "\nMenu\n";
cout << "==============================\n\n";
cout << "1. Add student to waiting list\n";
cout << "2. View waiting list\n";
cout << "3. Exit program\n\n";
cout << "Please enter choice: ";
cin >> choice;
cin.ignore();
return choice;
}
void processChoice(int choice, PersonList& p)
{
switch (choice)
{
case 1: p.AddToList();
break;
case 2: p.ViewList();
break;
}
}
|