Linked List

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();
}
 
V

Victor Bazarov

Dream said:
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.

Depends on what you intend to do with it. What's the _required_
interface?
2. Also how to do the fn call in main for that LOCATE subroutine that
returns a node????

I am not sure I understand the question. Don't you already have a call
to the 'LOCATE' function in your 'main'?
Any help would be appreciated.
[...[


void main(void)

'main' returns 'int'. Do not write 'void' between two parentheses in
a function declaration. Just omit it.
{
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);
^^^^^^^^^^^^^^^
Isn't this a call to 'LOCATE'?
//APPEND_To_END(10);

cout << "Take a look at the list now, after appending 20 at the end of
list" << endl;
List.TRAVERSE();
}

V
 
F

Frank Chang

Victor Bazorov, I think the original poster wants to know how to write
a Locate(int) member function that returns a node. Is that correct?
Thank you.
 
V

Victor Bazarov

Frank said:
Victor Bazorov, I think the original poster wants to know how to write
a Locate(int) member function that returns a node. Is that correct?
Thank you.

Frank Chung, I think you should ask the original poster. For that,
reply to him, not to me.
Thank you.
 
O

osmium

Dream Catcher said:
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????

Your LOCATE() function returns nothing so it is unlikely you could do
anything the results, which are a null. You are in the position of
architect *and* programmer so it is up to the architect part of you to
decide what you think is a valid and useful interface. I think you started
backwards, by writing code.

One way is to return the ordinal number of the located item and then provide
a set of functions that operate on list elements given the ordinal number.
Some examples: change(), remove(), insert_after(), insert_before(). I have
used such a scheme in a simulation language and it was extremely useful.
Note that change() could be used to simply inspect the item - a useful list
will have more data than the simple list you're writing to get a grasp of
the fundamentals..
 
F

Frank Chang

I am guessing that you want to write a member function node*
LinkedList::locate(int)
that returns a node. Here is the MSVC7.1 code:

Linked::node* Linked::LOCATE(int value)
{
node *NodePtr;
node *PreviousNode;

//If the list is empty do nothing
if (!Head)
return NULL;


//Determine if first node is the one
if (Head->data == value)
{
cout << "value found in the Head node\n";
return Head; //return Head mde
}
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


In addition, you need to change the access level of your nested struct
node to public so that in main , the following code will compile and
run,

Linked List;

List.APPEND(10);
List.APPEND(20);

Linked::node *ptra = List.LOCATE(20);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top