The file contains numbers:
Test.txt
127
128
129
The part of the program I am working on is to make a selection out of the linked list. After the program displays the info, the next function asks the user to make a selection (Basically to pick between the numbers: 127,128, or 129). When the number is picked out, the end result will show: "This number was selected. I need to figure out a way to seek out and display that selection. The function that I have been working on is called selectList. I initilized a variable to receive the user input, but I am unsure on how to figure out the "location" of the selection for the linked list.
My code is listed below:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <string>
#include <cstdlib>
#include <list>
using std::ifstream;
using namespace std;
class FloatList
{
private:
struct ListNode
{
float value;
struct ListNode *next;
};
ListNode *head;
public:
FloatList(void) //Constructor
{ head = NULL; }
void appendNode(float);
void insertNode(float);
void deleteNode(float);
void displayList(void);
void selectList(void);
};
void FloatList::appendNode(float num)
{
ListNode *newNode, *nodePtr;
newNode = new ListNode;
newNode->value = num;
newNode->next = NULL;
if (!head)
head = newNode;
else
{
nodePtr = head;
while (nodePtr->next)
nodePtr = nodePtr->next;
nodePtr->next = newNode;
}
}
void FloatList::displayList(void)
{
ListNode *nodePtr;
nodePtr = head;
while (nodePtr)
{
cout << nodePtr->value << endl;
nodePtr = nodePtr->next;
}
}
void FloatList::selectList(void)
{
ListNode *nodePtr;
nodePtr = head;
int x = 0;
cout<<"Please make a selection: \n";
cin>>x;
cin.get();
}
void FloatList::insertNode(float num)
{
ListNode *newNode, *nodePtr, *previousNode;
newNode = new ListNode;
newNode->value = num;
if (!head)
{
head = newNode;
newNode->next = NULL;
}
else
{
nodePtr = head;
while (nodePtr != NULL && nodePtr->value < num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
if (previousNode == NULL)
{
head = newNode;
newNode->next = nodePtr;
}
else
{
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
int main ()
{
FloatList list;
ifstream infile;
infile.open("test.txt");
//Builds list of Nodes from a File
while (!infile.eof())
{
int x = 0;
infile>>x;
list.appendNode(x);
}
//Display info
list.displayList();
cout<<"\n";
list.selectList();
cin.get();
}

New Topic/Question
Reply




MultiQuote






|