zoukankan      html  css  js  c++  java
  • 循环链表

    循环链表:只要知道表中任何一个结点的地址,就能遍历表中其他任一结点。这是我写的简单的循环链表:

    结点定义和抽象数据类型定义:

    struct CircleNode
    {
    	int data;
    	CircleNode *link;
    };
    //类定义
    class CircleList
    {
    public:
    	//构造函数析构函数
    	CircleList()
    	{
    		first=last=NULL;	//初始化头指针和尾指针
    	}
    	~CircleList(){}
    	//插入、删除、输出、搜索
    	CircleNode *Search(int i);
    	bool Insert(int i,int x);
    	bool Delete(int i);	
    	void Output();
    	
    private:
    	CircleNode *first,*last;
    };
    

    函数功能实现:

    /*
    插入结点算法:
    1、画图,分析两种简单情况,立得算法。
    */
    bool CircleList::Insert(int i,int x)
    {
    	CircleNode *newNode;
    	//如果循环链表为空,淡出处理
    	if(first==NULL&&i==0)
    	{
    		first=new CircleNode;
    		if(first==NULL)
    		{	
    			cout<<"新结点建立失败"<<endl;
    			return false;
    		}
    		else
    		{
    			last=first;
    			last->link=first;
    			first->data=x;
    		}
    		return true;
    	}
    	else			//不是第一个结点
    	{
    		newNode=new CircleNode;
    		if(newNode==NULL)
    		{
    			cout<<"新结点建立失败"<<endl;
    			return false;
    		}
    		newNode->link=last->link;
    		last->link=newNode;
    		newNode->data=x;
    		last=last->link;
    	}
    	return true;
    }
    
    /*
    输出循环链表算法:
    1、
    */
    void CircleList::Output()
    {
    	CircleNode *current=first;	//循环链表检测指针
    	while(current->link!=first)
    	{
    		cout<<current->data<<" ";
    		current=current->link;
    	}
    	cout<<current->data<<" ";
    	cout<<endl;
    }
    
    /*
    搜索算法:
    1、
    */
    CircleNode *CircleList::Search(int i)
    {
    	CircleNode *current=first;
    	int count=0;
    	while(count!=i)
    	{
    		current=current->link;
    		count++;
    	}
    	return current;
    }
    
    /*
    删除结点算法:
    1、
    */
    bool CircleList::Delete(int i)
    {
    	CircleNode *p;
    	CircleNode *current=first;
    	int count=0;
    	while(count<i-1)
    	{
    		current=current->link;
    		count++;
    	}
    	//current指针已经指向了需要删除的结点的前一个结点
    	p=current->link;	//指向要删除的结点
    	current->link=p->link;
    	delete p;
    	return true;
    
    }
    

    测试代码:

    cout<<"-----------循环链表----------"<<endl;
    	
    	CircleList cl;
    	cl.Insert(0,1);
    	cl.Insert(1,2);
    	cl.Insert(2,3);
    	cl.Insert(3,4);
    	cl.Insert(4,5);
    	cl.Output();
    	cout<<"搜索算法:"<<cl.Search(2)->data<<endl;
    	if(cl.Delete(2))
    		cout<<"删除成功"<<endl;
    	cl.Output();
    

    测试结果:

    -----------循环链表----------
    1 2 3 4 5
    搜索算法:3
    删除成功
    1 2 4 5
    请按任意键继续. . .

    这些代码是基础代码实现,优化的代码和更健壮的代码,将在开源中国网的博客中重新更新。

  • 相关阅读:
    【转】用.NET 2.0压缩/解压封装的类
    复制到剪贴板的js代码(兼容ie、firefox、chorme、safari...什么都兼容!)
    感觉文章和回复都不错,转载了用正则表达式找出不包含连续字符串abc的单词
    订阅到抓虾、google reader、鲜果等的代码
    [转]妙说23种设计模式
    实现Server.UrlEncode和Server.UrlDecode功能的js代码
    【转】用JS操作XML
    添加到某某书签、某某收藏的代码
    [总结]关于在线用户列表的统计![转载]
    多张图片交替变换的实现方法JS实现和flash实现
  • 原文地址:https://www.cnblogs.com/fistao/p/3096680.html
Copyright © 2011-2022 走看看