Self-Referential Classes (Linked List)

Does anyone know how does a Self-Referential Class Linked List code looks like?

I tried reading Deital but the codes were confusing and there was no example of the main function. Furthermore, I can't seem to find help on Google.

class Node
{
public:
Node( int ); // constructor
void setData( int ); // set data member
int getData() const; // get data member
void setNextPtr( Node * ); // set pointer to next Node
Node *getNextPtr() const; // get pointer to next Node
private:
int data; // data stored in this Node
Node *nextPtr; // pointer to another object of same type
}; // end class Node

I'm not sure how to continue the main function. Can anyone please give me some links to Self-Referential Classes Linked List codes or provide them here?

Thank you very much.
Last edited on
Init nextPtr with 0 for the last node and with the next node for all other nodes and you are done. What is your question?
I'm not sure how to continue the main function.

What do you have so far?
Well... I've done something like this.

#include <iostream>
using namespace std;

#include <string>

class Node
{
public:
Node(int AGE, string NAME)
{
setAge(AGE);
setName(NAME);
}
void setAge(int a)
{
age = a;
}
int getAge()
{
return age;
}
void setName(string n)
{
name = n;
}
string getName()
{
return name;
}
void setNextPtr(Node *node)
{
Node *last = this;
while (last->next)
last = last->next;
last->next = node;
}

Node *getNextPtr();

void search(name)
{

private:
int age;
string name;
Node *next;
};

void main()
{
Node *temp = NULL;
Node *temp2;
int age, choice;
string name;

while(true)
{
cout << "1. Insert Record\n2. Search Record\n3. Delete Record\n4. Print Record\n5. Quit\nEnter choice: ";
cin >> choice;
cout << endl << endl;
switch (choice)
{
case 1:
cout << "\n\nEnter first name: ";
cin >> name;
cout << "Enter age: ";
cin >> age;
temp2 = new Node(age, name);
cout << "Successfully created record.\n"
if (temp == NULL)
temp = temp2;
else
temp->setNextPtr(temp2);
break;
case 2:

}

}

}

But I'm getting some problem at the bolded part. I can insert the first one. I tried inserting the second data but the problem is I get some error.

Then debugged the thing and found out the problem at the bolded part. Thanks for your help.
This codes appends "node" to the end of the list provided that the value "next" was set to 0 before. Otherwise, the behavior is undefined (don't use NULL, use 0 instead). You should perhaps initialize "next" to 0 in the node constructor.
#include <iostream>
using namespace std;

#include <string>

class Node
{
public:
Node(int AGE, string NAME)
{
next = 0;
setAge(AGE);
setName(NAME);
}
void setAge(int a)
{
age = a;
}
int getAge()
{
return age;
}
void setName(string n)
{
name = n;
}
string getName()
{
return name;
}
void setNextPtr(Node *node)
{
Node *last = this;
while (last->next)
last = last->next;
last->next = node;
}
Node *getNextPtr();

private:
int age;
string name;
Node *next;
};

void main()
{
Node *temp = 0;
Node *temp2;
int age, choice;
string name;

while(true)
{
cout << "1. Insert Record\n2. Search Record\n3. Delete Record\n4. Print Record\n5. Quit\nEnter choice: ";
cin >> choice;
cout << endl << endl;
switch (choice)
{
case 1:
cout << "\n\nEnter first name: ";
cin >> name;
cout << "Enter age: ";
cin >> age;
temp2 = new Node(age, name);
cout << "Successfully created record.\n";
if (temp == 0)
temp = temp2;
else
temp->setNextPtr(temp2);
break;

}

}

}

I've changed the code to this but after I insert the second data, everything loops forever. Sorry but I'm an amateur in C++.
In which loop does the program hang? What do you mean by "second data"?
BTW, please use the code-tags and proper indention.
Sorry. This time the code is working right. I copied and paste from Visual Studio 2008 but the indentations disappear. How do I search for data that I've inserted?
Loop over each element, like you did in setNextPtr, and compare the value of the node with the value you are looking for. (Searching in linked lists requires time Ω(n))
Ok thanks. I've done the traversing the linked list.

Now I have to delete a single node. How do I delete a node and link the other nodes back?
When you are at the nodeToBeDeleted
make
previousNodes->next = nodeToBeDeleted->next
and then
delete nodeToBeDeleted
So if I delete in the middle, how do I reconnect the nodes?

How to delete the last node?
a->b->c->d->e
if you want to delete c
then make
a->b->d->e
and delete c;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
bool deleteANode(Node* List, Node* nToDelete){
Node* prev=NULL;
Node* cur=NULL;

for(cur=List; cur != NULL; prev=cur,cur=cur->getNextPtr() ){
	
	if(cur->getData() == nToDelete->getData()){// Found it
	
		//check
		if(prev==NULL){//  this is the first node
			List= cur->getNextPtr() ;
			
		
		}else{
			prev->setNextPtr(cur->next);
		}
		// delete the cur 
		delete cur;
		return true;
	
	}
}
return false;// not in the list

}
Topic archived. No new replies allowed.