Hi,

I am confused in making circular linked list.
This is what I have.

CircularLinkedList.h

#ifndef CircularLinkedList_H
#define CircularLinkedList_H

class CircularLinkedList
{
public:
	void remove();
	void set_number_people(int);
	void print();
};
#endif

CircularLinkedList.cpp

#include "CircularLinkedList.h"
#include "Node.h"
#include <stdio.h>


void CircularLinkedList::set_number_people(int num_people)  //This is where I am confused
{	
	Node *head = new Node;
	int n = num_people;
	Node *temp = new Node;	
	*head->set_next(*temp);
	for(int x = 2; x < n; x++)
	{

	}
}

Node.h

#ifndef Node_H
#define Node_H
#include<stdio.h>

class Node
{
public:
	void set_data(int data)
	{
		this->data = data;
	}

	void set_next(Node temp)
	{
		temp.next = new Node;
	}

	int get_data(Node temp)
	{
		return temp->data;  //'->Node::data' : left operand has 'class' type, 
                                      //use '.'
	}

	Node get_next(Node temp)
	{
		return temp->next;
	}

private:
	int data;
	Node *next;
};
#endif

driver.cpp

#include<iostream>
#include "CircularLinkedList.h"

using namespace std;

int main()
{
	int people, count;
	cout<<"Enter number of people: ";
	cin>>people;
	cout<<"Enter number of counts: ";
	cin>>count;
	CircularLinkedList cclist;
	cclist.set_number_people(people);
	//cclist.print();
	return 0;
}

Initially I had made one method of making circular linked list in node class which is

Node link(int num_people)
	{
		int n = num_people;
		Node *head = new Node;
		head->data = 1;
		head->next = new Node;	
		head->next->data = 2;
		Node *temp = head->next;
		for(int x = 2; x <= n; x++)
		{
			temp->next = new Node;
			temp->next->data = x;
			temp = temp->next;		
			if(x == n)
			{
				temp = NULL;
			}
		}
		if(temp == NULL)
		{
			temp = head;
		}
		return *head;
	}

But now I am confused in making the cirular linked list in Circularlinkedlist class not in Node.

Thanks in advance.

Can you be a little more specific on what you are confused about? I don't think your link() function works correctly, so that may be your problem.

Right now, I am NOT using link() function (that was what I had made initially). I am confused in CircularLinkedList.cpp; the set_number_people() function. In that function I want to use set_next() function (which is actually in Node.h) but dont really know how.

Thanks for replying.

Ah okay, well for that specific part I think your problem is this line in CircularLinkedList.cpp:

*head->set_next(*temp);

It should be:

head->set_next(*temp)

when you use head->function() with the "->" operator it's the same as using (*head).function(). The "->" operator is just shorthand. So when you had *head->set_next(*temp) it was actually *(*head).set_next(*temp) which is probably where your error was coming from.

Can I ask why you have a function for setting the next field of the current node? Why are you just saying this:
head->next = temp;

Oh yes I got that error fixed (head->set_next(*temp))

I cant use head->next because next is a private member of Node class and I want to use it in CircularLinkedList class.

Sorry about that, my brain is running a little slow from being at work all day. Okay, was that the problem or is there something else?

My problem is not solved yet. I mean, I am still confused in creating a circular linked list. I am not comfortable in using set_next() function as I am not sure how will that work.

For a circular linked list you want the last node in the list to point to the first, right? In this function:

Node link(int num_people)
	{
		int n = num_people;
		Node *head = new Node;
		head->data = 1;
		head->next = new Node;	
		head->next->data = 2;
		Node *temp = head->next;
		for(int x = 2; x <= n; x++)
		{
			temp->next = new Node;
			temp->next->data = x;
			temp = temp->next;	
           /*Everything is fine up to here I think. For the next part, you are checking if you are at the last node in the list. If so, you set the current node to NULL. Why aren't you having it point back to the beginning of the list? (aka circular linked list). It is just set to NULL so the list ends, it is not a circularly linked list.*/
			if(x == n) 
			{
				temp = NULL;
                                //should be this so list is circular:
                               // temp->next=head;
			}
		}
                //This just had another pointer to the head node, it didn't make the list circular at all so you don't need this part.
		if(temp == NULL)
		{
			temp = head;
		}
		return *head;
	}

So to re-write this function correctly inside of CircularLinkedList.cpp you need to re-write the set_next function in node.h so it actually sets the next field of the pointer that calls the function to the to the pointer you pass in.

Right now it is this:

void set_next(Node temp)
	{
		temp.next = new Node;
	}

But this just just sets temp's next field to a new node, it doesn't have head's next field point to temp. So calling head->set_next(temp) doesn't set head->next = temp. You can still create a new node outside of the function, then pass in the new node and have the pointer you want pointing to that new node call the function. So correctly written it is this:

void set_next(Node* temp)
{
this->next= temp; //now if you say head->set_next(temp) then head->next=temp
}                 //note: You aren't passing in a node anymore, but a pointer to a node

So finally we re-write the function inside CircularLinkedList.cpp with the corrected set_next() function.

#include "CircularLinkedList.h"
#include "Node.h"
#include <stdio.h>


void CircularLinkedList::set_number_people(int num_people)  
{

Node *head = new Node;
int n = num_people;
Node *temp = new Node;
head->set_next(temp); //here we don't pass in *temp, only temp
for(int x = 2; x <= n; x++) 
{
   temp->next = new Node;
   temp->next->data = x;
   temp = temp->next;
   if(x == n) 
   {
      temp->set_next(head) = NULL;
                             
   }
}
}

Hopefully this helps, sorry if its confusing and long. I had to edit because I accidentally submitted my answer half finished and probably should have just deleted it. Anyway there are probably syntax errors so if this doesn't work let me know, I typed very quickly. Any questions at all feel free to ask.

Can you please tell me how does the following statement works:

head->set_next(*temp)

If the set_next() function is something like this:

void set_next(Node temp)
	{
		temp.next = new Node;
	}

then does the statement: head->set_next(*temp) means that head.next points to a new node?

Sorry I had some typos in the corrected function, it should be this:

#include "CircularLinkedList.h"
#include "Node.h"
#include <stdio.h>


void CircularLinkedList::set_number_people(int num_people)  
{

Node *head = new Node;
int n = num_people;
Node *temp = new Node;
head->set_next(temp); //here we don't pass in *temp, only temp
  for(int x = 2; x <= n; x++) 
  {
   Node *temp2 = new Node;
   temp2->set_data(x);
   temp->set_next(temp2); //temp->next = temp2;
   temp = temp2;  //move to the next pointer in the list.
   if(x == n) 
    {
      temp->set_next(head); //if you are at the last node, set the next field to point back to the head.
                             
    }
  }
}

Thank you so much for all your help.

No problem, glad I could help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.