Linked list progam, HELP!
i am somewhat new to C++ and i have to create a program using linked lists that adds and deletes elements and then prints them to an outfile, basically the infile contains a letter followed by a number and the letter is used in a switch statement that tells what function to use, any help is GREATLY appreciated! Heres what i have so far:
#include <iomanip>
#include <iostream>
#include <cctype>
#include <string>
#include <fstream>
using namespace std;
struct numStruct
{
int info;
int next;
};
void initialize(int &list, int &avail, numStruct numArray[]);
void print (int list, numStruct numArray[]);
void addElement (numStruct numArray[], int num, int &list, int &avail);
void deleteElement (numStruct numArray[], int num, int &list, int &avail);
int main()
{
ifstream inFile;
inFile.open ("vollmerp6.dat");
ofstream outFile("vollmerp6.out");
if(!inFile)
{
cout << "File does not exist!" << endl;
return 1;
}
numStruct numArray[12];
int list, avail, num;
char choice;
while (inFile)
{
switch (choice)
{
case'A':
inFile >> choice;
inFile >> num;
addElement(numArray, num, list, avail);
break;
case'D':
inFile >> choice;
inFile >> num;
deleteElement(numArray, num, list, avail);
break;
case'P':
inFile >> choice;
inFile >> num;
print(list, numArray);
break;
}
}
outFile.close();
inFile.close();
return 0;
}
void initialize (int &list, int &avail, numStruct numArray[])
{
int i;
list = -1;
avail = 0;
for(i = 1; i < 11; i++)
numArray[i].next = i + 1;
numArray[i].next = -1;
}
void print (int list, numStruct numArray[])
{
while(list != -1)
{
cout << numArray[list].info << " ";
list = numArray[list].next;
}
cout << endl << endl <<endl;
}
void addElement (numStruct numArray[], int num, int &list, int &avail)
{
int index, temp = list, tAvail = avail;
bool found = false;
numArray[avail].info = num;
avail = numArray[tAvail].next;
//if list is empty
if (list == -1)
{
list = tAvail;
numArray[list].next = -1;
}
else
{
//if it's the smallest in array
if (numArray[temp].info > num)
{
list = tAvail;
numArray[tAvail].next = temp;
}
//if at the middle or end
else
{
while(temp != -1 && !found)
{
if (numArray[temp].info > num)
found = true;
else
{
index = temp;
temp = numArray[temp].next;
}
}
numArray[index].next = tAvail;
numArray[tAvail].next = temp;
}
}
}
void deleteElement (numStruct numArray[], int num, int &list, int &avail)
{
int index, temp = list;
bool found = false;
if (numArray[temp].info >= num)
{
avail = list;
list = numArray[temp].next;
}
else
{
while(temp != -1 && !found)
{
if (numArray[temp].info >= num)
found = true;
else
{
index = temp;
temp = numArray[temp].next;
}
}
if (found && numArray[temp].info != num)
cout << "Not found in list.\n";
else
{
numArray[index].next = numArray[temp].next;
numArray[temp].next = avail;
avail = temp;
}
}
}
Re: Linked list progam, HELP!
You should probably read the "choice" before switching on it, rather than reading after you have switched.
Re: Linked list progam, HELP!
Quote:
Originally Posted by
bobgilmore89
i am somewhat new to C++ and i have to create a program using linked lists that adds and deletes elements and then prints them to an outfile, basically the infile contains a letter followed by a number and the letter is used in a switch statement that tells what function to use, any help is GREATLY appreciated! Heres what i have so far:
Please re-edit your post to use proper code tags. Your post is unformatted and very hard to read.
Regards,
Paul McKenzie