D
Dream Catcher
1. I don't know once the node is located, how to return that node.
Should I return pointer to that node or should I return the struct of that
node.
2. Also how to do the fn call in main for that LOCATE subroutine that
returns a node????
Any help would be appreciated.
Thanks
#include <iostream.h>
class Linked
{
private:
struct node
{
int data;
struct node *link;
};
node *Head; //struct variable
public:
Linked(void) //constructor
{ Head = NULL;} //this establishes an empty linked list
//~Linked(void); //destructor
void APPEND (int); //to build linked lisd
void LOCATE(int); //Fn prototype
void REMOVE (void);
void APPEND_TO_END (int);
void TRAVERSE(void);
};
//building the linked list
void Linked::APPEND (int num)
{
node *NodePtr;
node *NewNode;
NewNode = new node; //DMA
NewNode->data = num;
NewNode->link = NULL;
if (!Head)
Head = NewNode; //if no node in the list, make this "NewNode", "Head"
node
else //append this newnode at the end of list
{
NodePtr = Head; //make NodePtr the Head of the list (only if there r
existing nodes in linked list)
while (NodePtr ->link)
NodePtr = NodePtr->link; //to move forward in linked list
NodePtr->link = NewNode; //append new node at end
}
}
//to locate a particular node
void Linked::LOCATE(int value)
{
struct node *NodePtr;
struct node *PreviousNode;
cout << "From Locate..i'll locate " << value << " for u in the linked list
and return the node" << endl;
cout << "where the value is located " << endl;
//If the list is empty do nothing
if (!Head)
return;
//Determine if first node is the one
if (Head->data == value)
{
cout << "value found in the Head node\n";
// return Head; //return Head node
}
/*make NodePtr the Head of linked list
Head = NodePtr;
/*finding the end of the list
while (NodePtr->link)
NodePtr = NodePtr->link;
move this Head node to end of linked list
NodePtr->link = Head;*/
else //serach for the node inside linked list
{
//initilaize NodePtr to Head of list
NodePtr = Head;
//Skip all Nodes whose value member is NOT equal to "value"
while ( NodePtr != NULL && NodePtr->data != value)
{
PreviousNode = NodePtr;
NodePtr = NodePtr->link;
}
// return NodePtr;
}
}// end LOCATE subroutine
void main(void)
{
Linked List; //class object
cout <<" from MAIN...Hi There!!" << endl;
//Build Linked List
List.APPEND(10);
List.APPEND(20);
List.APPEND(30);
List.LOCATE(20);
//APPEND_To_END(10);
cout << "Take a look at the list now, after appending 20 at the end of
list" << endl;
List.TRAVERSE();
}
Should I return pointer to that node or should I return the struct of that
node.
2. Also how to do the fn call in main for that LOCATE subroutine that
returns a node????
Any help would be appreciated.
Thanks
#include <iostream.h>
class Linked
{
private:
struct node
{
int data;
struct node *link;
};
node *Head; //struct variable
public:
Linked(void) //constructor
{ Head = NULL;} //this establishes an empty linked list
//~Linked(void); //destructor
void APPEND (int); //to build linked lisd
void LOCATE(int); //Fn prototype
void REMOVE (void);
void APPEND_TO_END (int);
void TRAVERSE(void);
};
//building the linked list
void Linked::APPEND (int num)
{
node *NodePtr;
node *NewNode;
NewNode = new node; //DMA
NewNode->data = num;
NewNode->link = NULL;
if (!Head)
Head = NewNode; //if no node in the list, make this "NewNode", "Head"
node
else //append this newnode at the end of list
{
NodePtr = Head; //make NodePtr the Head of the list (only if there r
existing nodes in linked list)
while (NodePtr ->link)
NodePtr = NodePtr->link; //to move forward in linked list
NodePtr->link = NewNode; //append new node at end
}
}
//to locate a particular node
void Linked::LOCATE(int value)
{
struct node *NodePtr;
struct node *PreviousNode;
cout << "From Locate..i'll locate " << value << " for u in the linked list
and return the node" << endl;
cout << "where the value is located " << endl;
//If the list is empty do nothing
if (!Head)
return;
//Determine if first node is the one
if (Head->data == value)
{
cout << "value found in the Head node\n";
// return Head; //return Head node
}
/*make NodePtr the Head of linked list
Head = NodePtr;
/*finding the end of the list
while (NodePtr->link)
NodePtr = NodePtr->link;
move this Head node to end of linked list
NodePtr->link = Head;*/
else //serach for the node inside linked list
{
//initilaize NodePtr to Head of list
NodePtr = Head;
//Skip all Nodes whose value member is NOT equal to "value"
while ( NodePtr != NULL && NodePtr->data != value)
{
PreviousNode = NodePtr;
NodePtr = NodePtr->link;
}
// return NodePtr;
}
}// end LOCATE subroutine
void main(void)
{
Linked List; //class object
cout <<" from MAIN...Hi There!!" << endl;
//Build Linked List
List.APPEND(10);
List.APPEND(20);
List.APPEND(30);
List.LOCATE(20);
//APPEND_To_END(10);
cout << "Take a look at the list now, after appending 20 at the end of
list" << endl;
List.TRAVERSE();
}