0

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

5
Contributors
10
Replies
12
Views
12 Years
Discussion Span
Last Post by jai verma
0

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.

0

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.

0

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

0
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.

0

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!

0

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.

0

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.

-2
#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;
}
}}

Edited by WaltP: Added CODE Tags

Votes + Comments
You should have saved yourself the hassle of joining, if that's the best you can do
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.