C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 16th, 2005, 10:15 AM
cofffy cofffy is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 4 cofffy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 35 m 41 sec
Reputation Power: 0
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 it. But I am delteing 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

Code:
 
#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;
}
 
// 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

Reply With Quote
  #2  
Old April 16th, 2005, 07:06 PM
astralvoid astralvoid is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 9 astralvoid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 2 m 42 sec
Reputation Power: 0
There are a couple small problems with your display/search method.

The first problem is that if the first item on the list is the value you are looking for, then you are deleting it before anything is done with it. Since you are not using the 'delete' keyword, you are creating a small memory leak by setting the head node equal to the next node....the original head node is being left floating around in memory since you did not delete it.

See here: You point to the head->next node with nodePtr, then immediately change head to equal the new nodePtr. (head->next) which effectively removes the original head from your list. (at the same time you lose reference to it and thus create a memory leak).

Code:
 // Determine if the first node is the one.
 if (head->value == searchValue)
 {
 nodePtr = head->next;
 //delete head;
 head = nodePtr;
 }
 


In fact after looking over your code again, it appears that you've copied the deleteNode method and simply renamed it to the displayNode method.

The rest of the code just searches the list to the end, then if it's found, deletes it. Is this what you want to do?

Code:
 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;
 }
 


Take a look over what I've pointed out and perhaps you'll see what you're missing.

Hope this helps!

Reply With Quote
  #3  
Old April 17th, 2005, 09:36 AM
cofffy cofffy is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 4 cofffy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 35 m 41 sec
Reputation Power: 0
Yes, I did copy the delete, My thinking was I want to do the same thing as delete, BUT instead of deleteing the number the user puts in, I want to show it. Well, actually I want to show the index number where the element is stored. But the more I work on this, the more confused I'm getting. I'm going to check out what you said.....Thank you...I'll post back soon.

Reply With Quote
  #4  
Old April 17th, 2005, 10:20 AM
cofffy cofffy is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 4 cofffy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 35 m 41 sec
Reputation Power: 0
I checked out what you told me.....I fixed it!! works fine now....THANK YOU!!!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Linked List


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT