Note : Completed as per your requirement... Please run and check with your inputs..
Code:
#include <iostream>
#include <unistd.h>
using namespace std;
struct node
{
int
data;
//struct to store the data...
node
*next;
//Pointer to point next cell in a list..
};
class linked_list
{
private:
node *head, *tail;
public:
linked_list()
{
head =
NULL;
tail =
NULL;
}
int add_node(int
n)
//Func to add initial list elements...
{
node *tmp = new node; //Creating new node...
tmp->data
= n; //Assigning
user entered value to it..
tmp->next =
head; //Since it is circular list
assigning head
if(head ==
NULL)
{
head =
tmp; //New List...First
element..
tail = head;
}
else
{
node *temp = new node; //Create
temporary node to traverse list
temp = head;
while( temp != tail)
//Traverse until end of the list
{
temp =
temp->next; //Point to next cell
}
temp->next =
tmp; //Add at the end of the
list..
tail = tmp;
//Assigning temp to
tail..
}
tail->next =
head; //Since circular list always
make tail's next to point head..
return 0;
}
int add_nodeAtPosition(int
position, int value, int no, int *ref) //Tag 1 func call...
{
node *newnode =
new node; //Creating new
node...
newnode->data
=
value;
//Assigning user entered value to it..
newnode->next
=
NULL;
if(head ==
NULL)
//Validating list...
{
cout<<"\n\t<--Head null-->\n";
head =
newnode;
//If list is empty assign new node to head..
}
else if
(position > no) {
cout<<"\n\t<--Position out of
range-->\n"; //Invalid position entered by user..
return 0;
}
else{
node *temp1 = head;
node *temp2 = head;
while(--position) {
//Make it to point the exact
poisition entered by user..
temp1 =
temp1->next; //Point to next cell
temp2 = temp2->next;
(*ref)++;
//Refernce
value to update the reference position...
}
newnode -> next = temp2 -> next;
//Inserting new node...
temp1->next = newnode;
//Please
understand this by analysing this..
(*ref)++;
}
return 0;
}
int deleteAtPosition(int
position, int no, int *ref)
{
if(head ==
NULL)
//Validating list...
{
cout<<"\n\t<--List
empty-->\n";
return 0;
}
else if
(position > no) { //Invalid
position entered by user..
cout<<"\n\t<--Position out of
range-->\n";
return 0;
}
else{
(*ref) = 1;
node *temp1 = new node;
//Temporary nodes to traverse list..
node *temp = new node;
temp = head;
if (position == 1){
//Delete at begining...
temp = head;
tail->next = head ->
next;
head = head->next;
delete temp;
return 0;
}
else
{
--position;
//Make position
value to point exactly where the user entered..
while(--position) {
temp =
temp->next;
(*ref)++;
}
temp1 = temp-> next;
temp -> next = temp ->
next -> next;
delete
temp1; //Free the
memory...
(*ref)++;
}
}
return 0;
}
int updateReference(int *ref)
{ //Func to update the
refernce pointer...
int pos =
1;
node *temp = new
node;
temp =
head;
while(pos !=
*(ref)) {
//Traverse untill the refernce index and stop
traversal..
temp = temp->next;
pos++;
}
head =
temp;
//make head to point the
refernce index...
while(tail->next != head) {
tail = tail->next;
//Make tail to point the
index before head..
}
(*ref) =
1;
return 0;
}
int display_node(int position, int
*ref) { //Tag 3
node *temp = new
node;
temp =
head;
(*ref) =
1;
while(--position) {
//Point to the position
entered b user...
temp = temp->next;
(*ref)++;
//Increment the index of the refernce position also..
}
cout<<"\n\t"<<temp->data<<"\n"; //Display the
value in the index...
return 0;
}
/* int disp_node() {
node *temp = new
node;
temp =
head;
cout<<"\t";
do{
cout<<temp->data<<"-->";
temp = temp->next;
}while(temp !=
head);
cout<<endl<<endl;
return 0;
}
*/
};
int main()
{
linked_list a;
static int ref = 1;
int no, value;
int i = 0;
int tags;
int nop;
int pos = 1;
cin>>no;
//Total no of elements..
for(i = 0; i < no; i++) { //Adding initial
elements...
cin>>value;
a.add_node(value);
}
a.disp_node();
cin>>nop;
while(nop--)
{
cin>>tags>>pos; //Tags and ith
postition..
if (tags == 1) {
cin>>value; //Value to be
inserted...
//cout<<"\n\tAfter inserting value :
"<<endl;
a.add_nodeAtPosition(pos, value, no, &ref);
//a.disp_node();
a.updateReference(&ref);
//cout<<"\n\tAfter refernce position update :
"<<endl;
//a.disp_node();
}
else if(tags == 2) {
a.deleteAtPosition(pos,no, &ref);
a.updateReference(&ref);
//cout<<"\n\tAfter refernce position update :
"<<endl;
//a.disp_node();
}
else if(tags == 3) {
a.display_node(pos, &ref);
a.updateReference(&ref);
//cout<<"\n\tAfter refernce position update :
"<<endl;
//a.disp_node();
}
else
cout<<"\nInvalid tags.."<<endl;
}
return 0;
}
Output:
Output 2: (as per you required..)

________________________________________________________________________________________
Please comment if required any help...
Circle Game Sheila is a naughty girl. Yesterday she came up with a boring game and...
Algorithm implementation in Java please.
Josefine is currently setting up a new computer network at The University of Algorithms. The network consists of N computers numbered from 0 to N -1 that are initially not connected at all She builds the network by adding network cables between pairs of computers one at a time. Two computers A and B are connected if there exists at least one series of cables that leads from computer A to computer B. As other...
Topic:Data structures and algorithms
programming
Compiler:C++ 11, flag: -static -std=c++0x
Boring job One day, Rick got a sequence of N magic numbers. In order to find the latent code in the sequence, he decides to do the following operations with a magic parameter K: Step (1) Take out the first k numbers from current sequences into a temporary sequence ni namn, and pick the largest element Vmax out of these k numbers, then subtract 1 from the remaining K-1 elements....