循环单链表...... [英] Circular Singly Linked List...

查看:82
本文介绍了循环单链表......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个循环链表的程序,但是当选择显示选项时,它只显示第一个和最后一个节点数据和地址。我正在努力解决,但我没有得到它..但我仍然想要找到。我正在下面发布代码。请帮助......

_________________________________________________________________________

圆形单链表代码..

_________________________________________________________________________



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..
_________________________________________________________________________

#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]已添加代码块 - OriginalGriff [/ edit]



[edit]Code block added - OriginalGriff[/edit]

推荐答案

我没有涉足那个地方,试图弄清楚你做了什么,甚至没有了解你的数据是什么,或输入它的顺序。



所以,使用调试器并遵循它:在构建列表后立即插入一个断点,并检查它 - 按照节点执行并确保它正是您所期望的。如果是,则问题出在您的显示功能中。如果不是,那么问题在于你是如何构建的 - 所以重新开始,然后按照插入方法查看它究竟是做什么的。



这是你的代码和你的作业 - 所以我认为你可以自己开始看它!如果你达到一个不起作用的地步,你知道它做错了什么但却无法解决原因,那么再问一遍 - 但是不要把你的整个程序扔给我们并期望我们解决它对你而言。
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.


Quote:

tmp = head;

while( tmp-> next!= head-> next)

tmp=head;
while(tmp->next!=head->next)



那样里面的代码,而循环。


这篇关于循环单链表......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆