码迷,mamicode.com
首页 > 其他好文 > 详细

链表操作实现类

时间:2014-09-21 13:14:50      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:链表   算法   

有哪里不对的请指正

#include<iostream>
using namespace std;
struct listNode
{
	int value;
	listNode *next;
	listNode()
	{
		next = NULL;
	}
};
class myList
{
private:
	listNode* head;
	listNode* tail;
public:
	myList()
	{
		head = NULL;
		tail = NULL;
	}
	void insert(listNode *node)
	{
		if(head == NULL)
		{
			head = node;
			tail = node;
		}
		else
		{
			tail->next = node;
			tail = node;
		}
	}
	void insert(int i)
	{
		listNode *new_node = new listNode();
		new_node->value = i;
		if(head == NULL)
		{
			head = new_node;
			tail = new_node;
		}
		else
		{
			tail->next = new_node;
			tail = new_node;
		}
	}
	listNode* get_head()
	{
		return head;
	}
	void print()
	{
		listNode *temp = head;
		while(temp != NULL)
		{
			cout << temp->value << " ";
			temp = temp->next;
		}
		cout << endl;
	}
	void reversal()//reverse the list
	{
		listNode* before = NULL;
		listNode *current = head;
		listNode *temp = head;
		while(current != NULL)
		{
			listNode* next = current->next;
			current->next = before;
			before = current;
			current = next;
		}
		head = tail;
		tail = temp;
	}
	void reorder()//12345 重排序为15243
	{
		listNode* current = head;
		while(true)
		{
			listNode* next = current->next;
			listNode* findTheEnd = next;
			listNode* tempPre = findTheEnd;
			if( next == NULL||next->next == NULL )break;
			while(findTheEnd->next != NULL)
			{
				tempPre = findTheEnd;
				findTheEnd = findTheEnd->next;
			}
			tempPre->next = NULL;
			current->next = findTheEnd;
			findTheEnd->next = next;
			current = next;
			next = next->next;
		}
	}
	listNode* find_mid()
	{
		listNode* first = head;
		listNode* second = head;
		while(second != NULL)
		{
			second = second->next;
			if(second != NULL)
			{
				first = first->next;
				second = second->next;
			}
		}
		return first;
	}
	bool judge_circle()
	{
		listNode* first = head, *second = head;
		if(head == NULL) return false;
		do
		{
			second = second->next;
			if(second != NULL)
			{
				first = first->next;
				second = second->next;
			}
		}
		while(second != NULL && second != first && first != second);
		return first == second;
	}
	listNode* func(listNode* root, int k)
	{
		listNode* first = root;
		listNode* second = root;
		while(k--)
		{
			if(second != NULL)
				second = second->next;
			else break;
		}
		if(second == NULL)
			return NULL;
		else
		{
			while(second != NULL)
			{
				first = first->next;
				second = second->next;
			}
			return first;
		}
	}
	void merge_sort(listNode* h)//归并排序
	{
		if(h->next != NULL)
		{
		listNode *first = h, *second;
		listNode *onestep, *twostep ,*previous;
		onestep = twostep = h;
		while(true)
		{
			if(twostep->next != NULL)
			{
				twostep = twostep->next;
				previous = onestep;
				onestep = onestep->next;
			}
			else break;
			if(twostep->next != NULL)
				twostep = twostep->next;
			else break;
		}
		previous->next = NULL;
		second = onestep;
		merge_sort(first);
		merge_sort(second);
		listNode* last_sorted;
		if(first->value <= second->value)
		{
			last_sorted = first;
			first = first->next;
		}
		else
		{
			last_sorted = second;
			second = second->next;
		}
		while(first != NULL && second != NULL)
		{
			if(first->value <= second->value)
			{
				last_sorted->next = first;
				last_sorted = first;
				first = first->next;
			}
			else
			{
				last_sorted->next = second;
				last_sorted = second;
				second = second->next;
			}
		}
		if(first == NULL)
		{
			last_sorted->next = second;
		}
		else 
		    last_sorted->next = first;
		}
	}
};


链表操作实现类

标签:链表   算法   

原文地址:http://blog.csdn.net/u012999424/article/details/39449771

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!