I have to do this program in C++ but I don't know how exactly to write it so that it displays everything it has to display. I'll be really thankful if you try to help me because I'm not so good at programming.
- Create a program that has linked list with integers stored into a file
- Sort the linked list (quicksort)
- Find the min element of the linked list that is bigger than the average value of all elements.
- Save all results in a file
- Create function main with menu.
Here it is the code I did so far:
#include <iostream>
#include <fstream>
using namespace std;
struct IntegerList {
int data[6];
int nInteger;
IntegerList* pNext;
IntegerList* pPrev;
};
// Pointers to First and Last items
IntegerList *g_pFirstItem = NULL;
IntegerList *g_pLastItem = NULL;
void ReadFile(int nInteger){
IntegerList* pItem ;
pItem->nInteger = nInteger;
fstream filename;
g_pFirstItem = new IntegerList;
filename.open("list.txt");
cout << "The numbers are:"<< filename << endl;
filename.close();
}
// Quick Sort List
void QuickSortList(IntegerList *pLeft, IntegerList *pRight)
{
IntegerList *pStart;
IntegerList *pCurrent;
int nCopyInteger;
// If the left and right pointers are the same, then return
if (pLeft == pRight) return;
// Set the Start and the Current item pointers
pStart = pLeft;
pCurrent = pStart->pNext;
// Loop forever (well until we get to the right)
while (1)
{
// If the start item is less then the right
if (pStart->nInteger < pCurrent->nInteger)
{
// Swap the items
nCopyInteger = pCurrent->nInteger;
pCurrent->nInteger = pStart->nInteger;
pStart->nInteger = nCopyInteger;
}
// Check if we have reached the right end
if (pCurrent == pRight) break;
// Move to the next item in the list
pCurrent = pCurrent->pNext;
}
// Swap the First and Current items
nCopyInteger = pLeft->nInteger;
pLeft->nInteger = pCurrent->nInteger;
pCurrent->nInteger = nCopyInteger;
// Save this Current item
IntegerList *pOldCurrent = pCurrent;
// Check if we need to sort the left hand size of the Current point
pCurrent = pCurrent->pPrev;
if (pCurrent != NULL)
{
if ((pLeft->pPrev != pCurrent) && (pCurrent->pNext != pLeft))
QuickSortList(pLeft, pCurrent);
}
// Check if we need to sort the right hand size of the Current point
pCurrent = pOldCurrent;
pCurrent = pCurrent->pNext;
if (pCurrent != NULL)
{
if ((pCurrent->pPrev != pRight) && (pRight->pNext != pCurrent))
QuickSortList(pCurrent, pRight);
}
}
// finds the maximum and minimum in the list
// assumes that head pointer is defined elsewhere
int MaxMinInList(int *max, int *min)
{
// start at the root
IntegerList *head = new IntegerList;
IntegerList *pCurrent = head;
if (pCurrent == NULL)
return 0; // list is empty
// initialize the max and min values to the first node
*max = *min = pCurrent->nInteger;
// loop through the list
for (pCurrent = pCurrent->pNext; pCurrent != NULL; pCurrent = pCurrent->pNext)
{
if (pCurrent->nInteger > *max)
*max = pCurrent->nInteger;
else if (pCurrent->nInteger < *min)
*min = pCurrent->nInteger;
};
}
int main(){
cout << QuickSortList << endl;
cout << ReadFile << endl;
cout << MaxMinInList << endl;
}
I can't work with files and I hope you are about to help me. I copied the quicksort algorithm from the internet but i need help to display the result.