Does anyone know how to create circular linked list whose links are numbered from 1 to n specified by the user?

Recommended Answers

All 10 Replies

Does anyone know how to create circular linked list whose links are numbered from 1 to n specified by the user?

Yes.... yes, I do.

I love answering questions sometimes!


Create a circular linked list class and add an index member variable that will always be +1 to the input.

Does anyone know how to create circular linked list whose links are numbered from 1 to n specified by the user?

struct Node
{
    int info; 
    Node * next;
    Node(const int& x, Node * link)
      : info(x),
        next(link)
    { }
};

Node *CreateList(int n)
{
	Node * first = NULL;
	Node * temp = NULL;
	Node * last = NULL;
	for(int i = 1;i <= n;i++)
	{
		first = new Node(i,first);
		first -> next = first;
		temp = new Node(i,temp);
		first = temp;
		last -> next = first;
		
		

	
	}
	return last;
}

<< moderator edit: added [code][/code] tags >>

here is the code thus far for a circular linked list, if a user enters 4 i want 1 2 3 4 inserted into the cingular list, how do i do it, not quite clear on it, any help would greatly be appreciated.

how do i do it, not quite clear on it, any help would greatly be appreciated.

Here is a code snippet of a function to initialize something using a value:

void FillMyArray(int ArrayToBeFilled[], int TopValueToBeInserted)
{
      for(int i = 1; i<=TopValueToBeInserted;i++)
      {
           ArrayToBeFilled[i] = i;
      }
}

That should be enough to fill it

void FillMyArray(int ArrayToBeFilled[], int TopValueToBeInserted)
{
      for(int i = 1; i<=TopValueToBeInserted;i++)
      {
           ArrayToBeFilled[i] = i;
      }
}

The usual (and correct) idiom is like this.

for(int i = 0; i < TopValueToBeInserted; i++)

An array of size N is indexed from 0 to N-1.

Actually, for what he's asking for... I do need to make a modification, but not what you are saying.

ArrayToBeFilled[i-1] = i;

He wants it to be 1 to N for values.

But thanks for pointing that out!

Or using the usual idiom,

for ( int i = 0; i < TopValueToBeInserted; i++ )
      {
           ArrayToBeFilled[i] = i + 1;
      }

That works too :)

That works too :)

Maybe i wasn't to clear what i need the program to do is this: Assume that people are arranged in a circle and are numbered from 1 to n. if we count every 4th person, removing a person as we count them off then the first person removed is 4 the second person removed is 8 the third person removed is 5 since the fourth person is no longer in the circle and so on. I need to print the order in which the people are removed from a given circle n= number of people and m is the number used to count off. I need a circular list to do this, not sure how to incorporate it until there is no one to delete.

Apparently you don't trust the people you ask questions to. Otherwise you wouldn't post the same question on multiple sites. That's a pet peeve of mine, so if you're going to post identical threads and identical replies, at least include what you've been shown in your identical thread on the other sites as well so that we know where you stand.

Yes, I lurk in a lot of forums besides Daniweb.

#include<stdio.h>
#include<conio.h>
int addnode(int t);
void display(void);
int main()
{  int i;
for(i=0;i<10;i++)
{addnode(i);
display();
}getch();
}struct node
{ int info[10];
  struct node*link;
}
struct node*first=NULL,*temp,*ptr;
void addnode(int*item)
{temp=(struct node*)malloc(size of (struct));
temp->info[10]=item;
temp->link=null;if(first==NULL)
{first=temp;}
else{ptr=first;
while(ptr->link=!null)
{ptr=ptr->link;}
ptr->link=temp;
}
 void display()
{ptr=first;
while(ptr!=null)
{printf("%d",ptr->info);
ptr=ptr->link;
}
}}
commented: You should have saved yourself the hassle of joining, if that's the best you can do -4
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.