• 单向链表


     单向链表C++

    class IntSLLNode
    {
    public:
    	IntSLLNode()
    	{
    		next = 0;
    	}
    	IntSLLNode(int i, IntSLLNode *in = 0)
    	{
    		info = i;
    		next = in;
    	}
    	int info;
    	IntSLLNode *next;
    private:
    };
    
    class IntSLList
    {
    public:
    	IntSLList()
    	{
    		head = tail = 0;
    	}
    
    	~IntSLList();
    	int isEmpty()
    	{
    		return head == 0;
    	}
    
    	void addToHead(int);
    	void addToTail(int);
    	int deleteFromHead();
    	int deleteFromTail();
    	void deleteNode(int);
    	bool isInList(int)const;
    
    private:
    	IntSLLNode *head, *tail;
    };
    
    IntSLList::~IntSLList()
    {
    	for (IntSLLNode *p; isEmpty();)
    	{
    		p = head->next;
    		delete head;
    		head = p;
    	}
    }
    
    void IntSLList::addToHead(int el)
    {
    	head = new IntSLLNode(el, head);
    	if (tail == 0)
    		tail = head;
    }
    
    void IntSLList::addToTail(int el)
    {
    	if (tail != 0)
    	{
    		tail->next = new IntSLLNode(el);
    		tail = tail->next;
    	}
    	else
    		head = tail = new IntSLLNode(el);
    }
    
    int IntSLList::deleteFromHead()
    {
    	int el = head->info;
    	IntSLLNode *temp = head;
    	if (head == tail)
    	{
    		head = tail = 0;
    	}
    	else
    	{
    		head = head->next;
    	}
    	delete temp;
    	return el;
    }
    
    int IntSLList::deleteFromTail()
    {
    	int el = tail->info;
    	if (head == tail) 
    	{
    		delete head;
    		head = tail = 0;
    	}
    	else
    	{
    		IntSLLNode *temp;
    		for (temp = head; temp->next != tail; temp = temp->next)
    			delete temp;
    		tail = temp;
    		tail->next = 0;
    	}
    	return el;
    }
    void  IntSLList::deleteNode(int el)
    {
    	if (head != 0)
    		if (head == tail && el == head->info)
    		{
    			delete head;
    			head = tail = 0;
    		}
    		else if (el==head->info)
    		{
    			IntSLLNode *temp = head;
    			head = head->next;
    			delete temp;
    		}
    		else
    		{
    			IntSLLNode *temp, *pred;
    			for (pred = head, temp = head->next; temp != 0 && !(temp->info == el); pred = pred->next, temp = temp->next);
    			if (temp != 0)
    			{
    				pred->next = temp->next;
    				if (temp->next)
    					tail = pred;
    				delete temp;
    			}
    		}
    }
    
    bool IntSLList::isInList(int el)const
    {
    	IntSLLNode *temp;
    	for (temp = head; temp != 0 && !(temp->info = el); temp = temp->next)
    		return temp != 0;
    }
    天上我才必有用,千金散尽还复来!
  • 相关阅读:
    jquery datatable后台分页、移动端隐藏列、自定义列显示格式
    今日热门文章
    深入理解STL源码 系列文章
    LED显示屏通讯协议 2
    LED显示屏通讯协议 1
    定制的小键盘输入数字显示的LED计分显示屏
    Sql语句中的LIKE关键字中通配符以及Escape关键字
    Virtual Box扩展虚拟硬盘的问题
    选择腾讯云部署应用的要慎重了,私有网络阉割,可靠性变得难以确定!
    rabbitmq 重置
  • 原文地址:https://www.cnblogs.com/lutaishi/p/13436318.html
Copyright © 2020-2023  润新知