GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Apr-2005, 09:46
itsmecathys itsmecathys is offline
New Member
 
Join Date: Apr 2005
Posts: 11
itsmecathys is on a distinguished road

search linked list


HI,
I am trying to build a program that searches a linked list for a number that the user puts in. If the number is there I want to show its position in the list. If it is not there I want to show zero. I have a lot of the program written, the only problem is, is that it is sort of backwards. I am new at this. I have a delete function, and I thought a search function would do about the same thing except instead of deleting the number the user puts in, I want to show its position. But I am deleteing it still. In my code where I have "delete" I don't know what to put here. Can someone set me straight?
Thanks so much.....here is my code .cpp & .h

CPP / C++ / C Code:
/////////////////////////////////////////////.cpp file

#include <iostream>
#include "ModLinkedList.h"
using namespace std;

//Prototype
void search(int);
int number;

int main()
{
	LinkedList<int> list;

	// Build the list
	cout << "Enter a number to be searched: ";
	cin >> number;
	cout << "Now searching for the number\n";
	list.displayNode(number);
	list.appendNode(1);
	list.appendNode(2);
	list.appendNode(3);
	list.appendNode(4);
	list.appendNode(5);
	list.appendNode(6);
	

	cout << "Now displaying the number.\n";
	list.displayNode(number);
	list.displayList();
	return 0;
}

CPP / C++ / C Code:
////////////////////////////////////////////////////////.h file ////////////////

// A class template for holding a linked list.
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>		// For cout and NULL
using namespace std;

template <class T>
class LinkedList
{
private:
	// Declare a structure for the list
	struct ListNode
	{
		T value;
		struct ListNode *next;
	}; 

	ListNode *head;	// List head pointer

public:
	LinkedList()	// Constructor
		{ head = NULL; }
	~LinkedList(); // Destructor
	void appendNode(T);
	void insertNode(T);
	void deleteNode(T);
	void displayList();
	void displayNode(T);
};


//**************************************************
// appendNode appends a node containing the value  *
// pased into newValue, to the end of the list.    *
//**************************************************

template <class T>
void LinkedList<T>::appendNode(T newValue)
{
	ListNode *newNode, *nodePtr;

	// Allocate a new node & store newValue
	newNode = new ListNode;
	newNode->value = newValue;
	newNode->next = NULL;

	// If there are no nodes in the list
	// make newNode the first node
	if (!head)
		head = newNode;
	else	// Otherwise, insert newNode at end
	{
		// Initialize nodePtr to head of list
		nodePtr = head;

		// Find the last node in the list
		while (nodePtr->next)
			nodePtr = nodePtr->next;

		// Insert newNode as the last node
		nodePtr->next = newNode;
	}
}

//**************************************************
// displayList shows the value                     *
// stored in each node of the linked list          *
// pointed to by head.                             *
//**************************************************

template <class T>
void LinkedList<T>::displayList()
{
	ListNode *nodePtr;

	nodePtr = head;
	while (nodePtr)
	{
		cout << nodePtr->value << endl;
		nodePtr = nodePtr->next;
	}
}

//**************************************************
// The insertNode function inserts a node with     *
// newValue copied to its value member.            *
//**************************************************

template <class T>
void LinkedList<T>::insertNode(T newValue)
{
	ListNode *newNode, *nodePtr, *previousNode = NULL;

	// Allocate a new node & store newValue
	newNode = new ListNode;
	newNode->value = newValue;
	
	// If there are no nodes in the list
	// make newNode the first node
	if (!head)
	{
		head = newNode;
		newNode->next = NULL;
	}
	else	// Otherwise, insert newNode
	{
		// Initialize nodePtr to head of list and previousNode to NULL.
		nodePtr = head;
		previousNode = NULL;

		// Skip all nodes whose value member is less
		// than newValue.
		while (nodePtr != NULL && nodePtr->value < newValue)
		{	
			previousNode = nodePtr;
			nodePtr = nodePtr->next;
		}

		// If the new node is to be the 1st in the list,
		// insert it before all other nodes.
		if (previousNode == NULL)
		{
			head = newNode;
			newNode->next = nodePtr;
		}
		else	// Otherwise, insert it after the prev. node.
		{
			previousNode->next = newNode;
			newNode->next = nodePtr;
		}
	}
}

//*****************************************************
// The deleteNode function searches for a node        *
// with searchValue as its value. The node, if found, *
// is deleted from the list and from memory.          *
//*****************************************************

template <class T>
void LinkedList<T>::deleteNode(T searchValue)
{
	ListNode *nodePtr, *previousNode;

	// If the list is empty, do nothing.
	if (!head)
		return;
	
	// Determine if the first node is the one.
	if (head->value == searchValue)
	{
		nodePtr = head->next;
		delete head;
		head = nodePtr;
	}
	else
	{
		// Initialize nodePtr to head of list
		nodePtr = head;

		// Skip all nodes whose value member is 
		// not equal to searchValue.
		while (nodePtr != NULL && nodePtr->value != searchValue)
		{	
			previousNode = nodePtr;
			nodePtr = nodePtr->next;
		}

		// If nodePtr is not at the end of the list, 
		// link the previous node to the node after
		// nodePtr, then delete nodePtr.
		if (nodePtr)
		{
			previousNode->next = nodePtr->next;
			delete nodePtr;
		}
	}
}

//**************************************************
// Destructor                                      *
// This function deletes every node in the list.   *
//**************************************************

template <class T>
LinkedList<T>::~LinkedList()
{
	ListNode *nodePtr, *nextNode;

	nodePtr = head;
	while (nodePtr != NULL)
	{
		nextNode = nodePtr->next;
		delete nodePtr;
		nodePtr = nextNode;
	}
}

/////////////////////////////////////////////
template <class T>
void LinkedList<T>::displayNode(T searchValue)
{
	ListNode *nodePtr, *previousNode;

	// If the list is empty, do nothing.
	if (!head)
		return;
	
	// Determine if the first node is the one.
	if (head->value == searchValue)
	{
		nodePtr = head->next;
		//delete head;
		head = nodePtr;
	}
	else
	{
		// Initialize nodePtr to head of list
		nodePtr = head;

		// Skip all nodes whose value member is 
		// not equal to searchValue.
		while (nodePtr != NULL && nodePtr->value != searchValue)
		{	
			previousNode = nodePtr;
			nodePtr = nodePtr->next;
		}

		// If nodePtr is not at the end of the list, 
		// link the previous node to the node after
		// nodePtr, then delete nodePtr.
		if (nodePtr)
		{
			previousNode->next = nodePtr->next;
			delete nodePtr;
		}
	}
}

/////////////////////////////////////////////

#endif
Last edited by LuciWiz : 16-Apr-2005 at 09:52. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 16-Apr-2005, 09:59
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Well, I see you cycle through the list until you find the desired value. I suggest you use a variable for counting which position in the list you are at. You would increment this variable until you find the required value. Then, just display it, or use a return to give it to the calling function.

Kind regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 16-Apr-2005, 10:02
itsmecathys itsmecathys is offline
New Member
 
Join Date: Apr 2005
Posts: 11
itsmecathys is on a distinguished road

How to post code correctly on this forum?


Sorry about the last post "code", for the life of me I can't find HOW to post correctly. Can someone help me out?

Thanks
  #4  
Old 16-Apr-2005, 10:08
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline
Junior Member
 
Join Date: Apr 2005
Location: Arizona
Posts: 35
Stack Overflow will become famous soon enough
Hello,

You can post code by enclosing your code in [C] / [C++] tags. For a following example, copy the bolded line and paste it in a post. You will see that it encloses any text as a code block:

[C]int main() { return 0; }[/C]

Hope this helps.


- Stack Overflow
__________________
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [C] / [C++] tags. Your question may have been asked before, try the search facility.
  #5  
Old 16-Apr-2005, 10:08
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Just follow the link in my signature. Or you could hit the "reply" button bellow (Not the big "post reply" button, the little one in the right) and see how I included this piece of code:

CPP / C++ / C Code:
//C++ Code

[Edit]
Wow, Stack, it seems you beat me to the "Submit Reply" button by a few seconds! Congratulations
[/Edit]
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #6  
Old 16-Apr-2005, 10:12
itsmecathys itsmecathys is offline
New Member
 
Join Date: Apr 2005
Posts: 11
itsmecathys is on a distinguished road
I think someone slipped me a stupid pill. Thank you for your replies. I read about the [c++] but was looking for a button or something... duh
Thanks again.
Last edited by itsmecathys : 16-Apr-2005 at 10:16. Reason: messed up
  #7  
Old 16-Apr-2005, 10:14
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline
Junior Member
 
Join Date: Apr 2005
Location: Arizona
Posts: 35
Stack Overflow will become famous soon enough
Quote:
Originally Posted by LuciWiz
[Edit]
Wow, Stack, it seems you beat me to the "Submit Reply" button by a few seconds! Congratulations
[/Edit]
» Heh. Fast reflexes.


Quote:
Originally Posted by itsmecathys
Thank you....I did read about the [c++] but I was looking for a button or something...duh
» Ah, yes. No problem.


- Stack Overflow
__________________
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [C] / [C++] tags. Your question may have been asked before, try the search facility.
  #8  
Old 16-Apr-2005, 12:00
itsmecathys itsmecathys is offline
New Member
 
Join Date: Apr 2005
Posts: 11
itsmecathys is on a distinguished road

Still having prolems


When I run my program, it is suupose to show me the number the user put in. there are a total of 6 numbers, if the user guesses a number that is in the list, it should show that number. BUT, it is showing every other number, except the number the user put in. I hope I explained this good enough.
  #9  
Old 16-Apr-2005, 12:13
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by itsmecathys
When I run my program, it is suupose to show me the number the user put in. there are a total of 6 numbers, if the user guesses a number that is in the list, it should show that number. BUT, it is showing every other number, except the number the user put in. I hope I explained this good enough.

You probably printed everything in the while, right? I didn't say that. When you exit the loop, if you found the value, then print the counter.

Please show us the code you've come up with now. Only the relevant function, please.

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #10  
Old 16-Apr-2005, 15:54
itsmecathys itsmecathys is offline
New Member
 
Join Date: Apr 2005
Posts: 11
itsmecathys is on a distinguished road
OIC.....thanks again
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Merge sort on a linked list Temujin_12 C++ Forum 1 06-Mar-2008 21:33
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12
adding to linked list from external file cghv C Programming Language 3 09-Mar-2005 14:36
Search Engine Positioning 101 and 201 "How To" Tips... 000 Search Engine Optimization Forum 0 29-May-2003 11:34

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:53.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.