create Circular linked list

Hi . I have question about circular linked list . how can i create first node in circular linked list
struct Node
{
int item;
Node* next;
};
int main()
{
Node* newPtr = new Node;
Node* list;
cin>> newPtr->item;
list = newPtr;
newPtr->next = list;
return 0;
}
You already did.
What's the problem?
how to add another node to end
The old node points to the new node and the new node points to the first (=the old) node.
Hi . my problem is when enter a last node when cout list the last data come to first
for example
Enter Data : 1
Enter Data : 2
Your Data : 2
Your Data : 1
#include<iostream>
using namespace std;

struct Node
{
int data;
Node* next;
};
void insertBeginNode(Node*&);
void displayNode(Node*);
void insertEndNode(Node*);
int main()
{
Node* list;
insertBeginNode(list);
insertEndNode(list);
displayNode(list);
return 0;
}
void insertBeginNode(Node*& list)
{
Node* newPtr = new Node;
cout << "\n Please Enter Data : ";
cin >> newPtr->data;
list = newPtr;
newPtr->next = list;
}
void displayNode(Node* list)
{
if ( list != NULL)
{

Node* first = list->next;
Node* cur = first;
do
{
cout << "\n Your Data is : ";
cout << cur->data;
cur = cur->next;
}while(cur != first);
}
}
void insertEndNode(Node* list)
{
Node* first = list->next;
Node* cur = first;
Node* prev = cur;
while( cur != first)
{
prev = cur;
cur = cur->next;
}
Node* newPtr = new Node;
cout << "\n Please Enter Data : ";
cin >> newPtr->data;
newPtr->next = cur->next;
prev -> next = newPtr;
}



Topic archived. No new replies allowed.