954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

circular linked lists

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

cblue
Newbie Poster
24 posts since Oct 2004
Reputation Points: 10
Solved Threads: 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.

marinme
Junior Poster in Training
63 posts since Apr 2005
Reputation Points: 10
Solved Threads: 1
 
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.

cblue
Newbie Poster
24 posts since Oct 2004
Reputation Points: 10
Solved Threads: 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

marinme
Junior Poster in Training
63 posts since Apr 2005
Reputation Points: 10
Solved Threads: 1
 
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.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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!

marinme
Junior Poster in Training
63 posts since Apr 2005
Reputation Points: 10
Solved Threads: 1
 

Or using the usual idiom,

for ( int i = 0; i < TopValueToBeInserted; i++ )
      {
           ArrayToBeFilled[i] = i + 1;
      }
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

That works too :)

marinme
Junior Poster in Training
63 posts since Apr 2005
Reputation Points: 10
Solved Threads: 1
 
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.

cblue
Newbie Poster
24 posts since Oct 2004
Reputation Points: 10
Solved Threads: 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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
#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;
}
}}
jai verma
Newbie Poster
1 post since Apr 2010
Reputation Points: 6
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You