C+ Seeking through a Linked List

Seeking through a Linked LIst

Page 1 of 1

3 Replies - 1615 Views - Last Post: 19 February 2009 - 09:37 AM Rate Topic: -----

#1 digitalhitman00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-February 09

C+ Seeking through a Linked List

Post icon  Posted 17 February 2009 - 12:54 PM

I have a small program that reads a file and puts everything into a linked list in the program.
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();


}



Is This A Good Question/Topic? 0
  • +

Replies To: C+ Seeking through a Linked List

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,657
  • Joined: 23-August 08

Re: C+ Seeking through a Linked List

Posted 17 February 2009 - 02:10 PM

Just like you display the list, but instead of displaying each entry, you only display it if the data matches what was entered.
Was This Post Helpful? 0
  • +
  • -

#3 digitalhitman00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-February 09

Re: C+ Seeking through a Linked List

Posted 19 February 2009 - 09:10 AM

Thanks, that was helpful. Here is the code I added for all the newbies like me:


void FloatList::selectList(void)
{
ListNode *nodePtr;
nodePtr = head;
int x = 0;
cout<<"Please make a selection: \n";
cin>>x;

while(nodePtr)
{
	if (nodePtr->value == x)
	{
		cout <<"You have Selected: "<<nodePtr->value << endl;
	}
	nodePtr = nodePtr->next;

}
cin.get();
}


Was This Post Helpful? 0
  • +
  • -

#4 anoraxis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 17-February 09

Re: C+ Seeking through a Linked List

Posted 19 February 2009 - 09:37 AM

why do you insert lines for input and output in your linked list class?. you can do it, but if you want to use this list for others programs, one with visual interface for example, this list cant work correctly.

it is a good practice to keep our classes without dependencies of this type , otherwise the use of the code is very restrictive.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1