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.

  1. Create a program that has linked list with integers stored into a file
  2. Sort the linked list (quicksort)
  3. Find the min element of the linked list that is bigger than the average value of all elements.
  4. Save all results in a file
  5. 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.

Quicksort (or qsort in C nomenclature) is an array-based sorting algorithm and a standard tool for every C/C++ compiler known. I used to use it for sorting linked lists, but the first thing you have to do is put the list elements in an array, create a compare() function, and then call qsort. After that, you walk through the array and rebuild the list. BTW, you you REALLY need to use a doubly-linked list? A singly-linked list with just pointers to next should be just fine. The end of the list is when you see that pNext is pointing to NULL (0 in C++ terms). For large lists, this method is MUCH faster than what you are doing, which is more of a bubblesort that a quicksort. The only downside is the memory needed for the array of pointers to list elements. IE, 1000 elements == 1000 x sizeof(void*), or on a 64bit system, approximately 8000 bytes - not much in truth! Your use of the pPrev pointer will take the same amount of space for 1000 elements... Only use a doubly-linked list if you REALLY have to traverse the list in a reverse direction.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.