Click here to Skip to main content
16,021,169 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have created a program of circular linked list but when display option is chosen it only display the 1st and last node data and address. I am trying to solve but i am not getting it..but still i am trying to sought out.I am posting code below.Please help...
_________________________________________________________________________
code of circular singly linked list..
_________________________________________________________________________

C++
#include<iostream>
using namespace std;

class node
{
	public:
		int data;
		node *next;
		node()
		{
			data=0;
			next=0;
		}
		node(int value,node *n=0)
		{
			data=value;
			next=n;
		}
};

class link
{
	public:
		node *head;
		link()
		{
			head=0;
		}
		void insert_last()
		{
			int value;
			cout<<"Enter Value=";
			cin>>value;
			//node *n;
			if(head==0)
			{
				head=new node(value);
				head->next=head;
			}
			else
			{
				node *tmp;
				tmp=head;
				while(tmp->next!=head->next)
				{
					tmp=tmp->next;
				}
				tmp->next=new node(value);
				tmp->next->next=head;
			}
		}

		void insert_first()
		{
			int value;
			node *n;
			cout<<"Enter a value=";
			cin>>value;
			if(head==0)
			{
				n=new node(value);
				n->next=head;
				head=n;
			}
			else
			{
				head=new node(value,head);
			}
		}
		void insert_pos_before()
		{
			int value,pos;
			cout<<"Enter Position to Insert=";
			cin>>pos;
			cout<<"Enter Element=";
			cin>>value;
			node *tmp=head;
			node *n=new node(value);
			if(pos==1)
			{
				head=new node(value,head);
			}
			else
			{
				for(int i=1;i<(pos-1);i++)
				{
					tmp=tmp->next;
					n->next=tmp->next;
					tmp->next=n;
				}
			}
		}
		void insert_pos_after()
		{
			int value,pos;
			cout<<"Enter Position to insert=";
			cin>>pos;
			cout<<"Enter Value=";
			cin>>value;
			node *n=new node(value);
			node *tmp=head;
			for(int i=1;i<pos;i++)>
			{
				tmp=tmp->next;
				n->next=tmp->next;
				tmp->next=n;
			}
		}
		void del_first()
		{
			if(head==0)
			{
				cout<<"List is Empty"<<endl;
			}
			else
			{
				head=head->next;			
			}
			cout<<"First Node Deleted...!"<<endl;
		}
		void del_last()
		{
			if(head==0)
			{
				cout<<"List is Empty"<<endl;
			}
			else
			{
				node *tmp=head;
				while(tmp->next->next!=0)
				{
					tmp=tmp->next;
				}
				tmp->next=0;
				cout<<"Last Node Deleted...!"<<endl;
			}
		}
		void del_pos()
		{
			int p;
			if(head==0)
			{
				cout<<"List is Empty"<<endl;
			}
			else
			{
				node *tmp=head;
				cout<<"Insert Position=";
				cin>>p;
				for(int i=1;i<(p-1);i++)
				{
					tmp=tmp->next;
				}
				tmp->next=tmp->next->next;
			}
			cout<<"Node Deleted...!"<<endl;
		} 
		void display()
		{
			node *tmp=head;
			cout<<"Address of Head="<<head<<endl;
			cout<<endl;
			if(head==NULL)
			{
                    cout<<"List is Empty...!"<<endl;
            }
            else
            {
			    while(tmp->next!=head)
			    {
				 cout<<"Value:"<<tmp->data<<endl;
				 tmp=tmp->next;
			    }
		     cout<<"Value:"<<tmp->data<<"   "<<"Next Address="<<tmp->next<<endl;		
             }
        }
		void update()
		{
			int p;
			int value;
			if(head==0)
			{
				cout<<"List is Empty"<<endl;
			}
			else
			{
				node *tmp=head;
				cout<<"Insert Position of node=";
				cin>>p;
				cout<<"Insert new Value=";
				cin>>value; 
				for(int i=1;i<p;i++)>
				{
					tmp=tmp->next;
					if(i==p)
					{
						break;
					}
				}
				tmp->data=value;
				cout<<"Value Updated in the node...!"<<endl;
				
			}
		}
};
int main()
{
	//node n;
	link l;
	int ch;
	char i;
	do
	{
		cout<<"**************** LINKED LIST ****************"<<endl;
		cout<<"1: INSERT NODE"<<endl;
		cout<<"2: REMOVE NODE"<<endl;
		cout<<"3: UPDATE VALUE IN NODE"<<endl;
		cout<<"4: DISPLAY"<<endl;
		cout<<"5: EXIT"<<endl;
		cout<<endl;
		cout<<"Enter your choice=";
		cin>>ch;	
		switch(ch)
		{
			case 1:	cout<<"A: INSERT NODE FIRST"<<endl;
				cout<<"B: INSERT NODE LAST"<<endl;
				/*cout<<"C: INSERT NODE BEFORE"<<endl;
				cout<<"D: INSERT NODE AFTER"<<endl;*/
				cout<<"*****************************"<<endl;
				cout<<"Enter Choice=";
				cin>>i;
				switch(i)
				{
					case 'A': l.insert_first();
					          break;
						
					case 'B': l.insert_last();
						  break;
	
					/*case 'C': l.insert_pos_before();
						   break;

					case 'D': l.insert_pos_after();
						  break;*/
					
					default: cout<<"Invalid Option...!"<<endl;
						 break;
				}
				break;

			case 2: cout<<"A: DELETE NODE FIRST"<<endl;
				cout<<"B: DELETE NODE LAST"<<endl;
				cout<<"C: DELETE NODE FROM THE DESIRED POSITION"<<endl;
				cout<<"*****************************"<<endl;
				cout<<"Enter Choice=";
				cin>>i;
				switch(i)
				{
					case 'A': l.del_first();
					          break;
						
					case 'B': l.del_last();
						  break;
	
					case 'C': l.del_pos();
						   break;
					
					default: cout<<"Invalid Option...!"<<endl;
						 break;
				}
				break;

			case 3: l.update();
				break;

			case 4: l.display();
				break;
			
			default: cout<<"Invalid Choice.."<<endl;
				 break;			
		}
	}while(ch>0 && ch<5);

	return 0;
}


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 19-Apr-14 4:10am
v2

I am not wading through that lot and trying to work out what you have done, without even a clue as to what your data is, or the order in which you enter it.

So, use the debugger and follow it through: putt a breakpoint immediately after you have built your list, and check it - follow the nodes through and make sure it is exactly what you expect. If it is, then the problem is in your display function. If it isn't, then he problem is with how you built it - so start again, and follow the insert methods and see what exactly it does.

This is your code, and your homework - so I think you can start looking at it yourself! If you get to a point where it doesn't work and you know what it is doing wrong but can't work out exactly why, then ask again - but don't throw your whole program at us and expect us to work it out for you.
 
Share this answer
 
Comments
A94 19-Apr-14 12:07pm    
I have been trying since two days...and also this is my home work.however i am learning it myself.And as far as clue is concerned i'll provide to you.. so if data is 1,2,3,4,5 it will display 1 and 5 not 1,2,3,4,5...
OriginalGriff 19-Apr-14 12:16pm    
"I have been trying since two days"
And? What results do you have? Is the error in the print, or the build?
And which part? What is it creating? what have you looked at?

Yes, I could run your code, and fix it for you - but that doesn't teach you anything!
This is a simple problem, with simple code so you should be able to debug it yourself. If you can't, how are you going to cope next week when you homework is harder? :laugh:

You have been doing what for two days? What have you eliminated as "Not being the problem"?
A94 19-Apr-14 12:23pm    
ok i agree it takes time and also agree that its a simple problem but since for two days i was trying to solve other functions in the code and i have fixed it too but i think there is a problem in display function...!
OriginalGriff 20-Apr-14 4:25am    
What makes you "think there is a problem in display function"?
What have you done that proves that?
A94 22-Apr-14 11:23am    
Ok..! I have solved it myself..thanks a lot...!!
Quote:
tmp=head;
while(tmp->next!=head->next)

That way the code inside the while loop is never executed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900