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