链表的基本操作

#include <iostream>
#include <vector>
#include <algorithm>
#include <time.h>

using namespace std;


struct ListNode
{
	int data;
	ListNode* next;

	//ListNode(int a = 0,ListNode* p = 0) : data(a) , next(p) {};
};


void initNode(ListNode& node)
{
	node.data = 0;
	node.next = 0;
}


bool isEmpty(ListNode& head)
{
    return(head.next == NULL);
}


void pushFront(ListNode& head,int a)
{
	ListNode* node = new ListNode;
	node->data = a;
	node->next = head.next;
	head.next = node;
}


void popFront(ListNode& head)
{
	if(head.next)
	{
		ListNode* p = head.next;
		head.next =  p->next;
		delete p;
	}
}


ListNode* front(ListNode& head)
{
	return(head.next);
}


ListNode* insert(ListNode* p, int a)
{
	ListNode* node = new ListNode;
	node->data = a;
	node->next = p->next;
	p->next = node;
	return(node);
}


//p插入node之后
ListNode* insert(ListNode* node,ListNode* p)
{
	p->next = node->next;
	node->next = p;
	return(p);
}

//将s到e之间的链表插入到node之后
ListNode* insert(ListNode* node,ListNode* s,ListNode* e)
{
	e->next = node->next;
	node->next = s; 
	return(e);
}


ListNode* erease(ListNode& head,ListNode* p)
{
	if(p->next)   //非最后一个节点
	{
		ListNode* node = p->next;
        p->data = node->data;

		//删除node
		p->next = node->next;
		delete node;
		return(p);
	}

	//删除最后一个节点,则从头遍历链表来删除
    ListNode* node = &head;
	for( ; node && node->next != p ; node = node->next);
	
	if(node->next == p)
	{
         node->next = NULL;
		 delete p;
	}
	return(NULL);
}


//反转链表
void reverse(ListNode& head)
{
    ListNode* p = head.next;
	head.next = NULL;

	ListNode* node;
	while(p)
	{
		node = p;
        p = p->next;
	   
		//将node插入链表头部
		/*node->next = head.next;
		head.next = node;*/
		insert(&head,node);
	}
}


//将head2合并到head1,保证两个链表都是有序的,假设链表是升序排列
void merge(ListNode& head1,ListNode& head2)
{
	ListNode *p,*p1,*p2,*t;
	p = &head1;
	p1 = head1.next;
	p2 = head2.next;

	head1.next = NULL;
	head2.next = NULL;
	while(p1 && p2)
	{
		if(p1->data <= p2->data)
		{
			t = p1;
			p1 = p1->next;       
			p = insert(p,t);  
		}
		else
		{
			t = p2;
			p2 = p2->next;
			p = insert(p,t);
		}
	}

	if(p1)
	{
		p->next = p1;
	}

	if(p2)
	{
		p->next = p2;
	}
}


//插入排序
void sort(ListNode& head)
{
    ListNode *p = head.next , *p1 , *t;
	head.next = NULL;
	while(p)
	{
         for(p1 = &head; p1->next; p1 = p1->next)
		 {
			 if(p->data <= p1->next->data)
			 {
			     break;
			 }
		 }
       
		 t = p;
		 p = p->next;
		 //将t插入到p1后
		 insert(p1,t);
	}
}


void clear(ListNode& head)
{
	ListNode *p1,*p2;
	p1 = head.next;
	while(p1)
	{
        p2 = p1;
		p1 = p1->next;
		delete p2;
	}
	head.next = NULL;
}

//辅助程序
void print(ListNode& head)
{
	ListNode* p = head.next;
	for( ; p ; p = p->next)
	{
		cout << p->data << " ";
	}
	cout << endl;
}

int isEqual(vector<int>& ar,ListNode& head)
{
    ListNode* p = head.next;
	for(int i = 0; i < ar.size(); ++i)
	{
		if(ar[i] != p->data)
		{
			return(0);
		}
		p = p->next;
	}
	return(1);
}

void test_merge()
{
	srand((unsigned)time(NULL));

	vector<int> array;
	int a,n;
	ListNode head;
	initNode(head);

	ListNode head1;
	initNode(head1);

	while(1)
	{
		n = rand() % 500;
		for(int i = 0; i < n; ++i)
		{
			a = rand();
			insert(&head,a);
			array.push_back(a);
		}
		sort(head);

		n = rand() % 500;
		for(int i = 0; i < n; ++i)
		{
			a = rand();
			insert(&head1,a);
			array.push_back(a);
		}
		sort(head1);

		merge(head,head1);
		sort(array.begin(),array.end());

		if(!isEqual(array,head))
		{
			cout << "no" << endl;
		}
		else
		{
			cout << "yes" << endl;
		}

		clear(head);
		clear(head1);
		array.clear();
	}
}

void test_sort()
{
	srand((unsigned)time(NULL));

	vector<int> array;
	int a,n;
	ListNode head;
	initNode(head);

	while(1)
	{
		n = rand() % 500;
		for(int i = 0; i < n; ++i)
		{
			a = rand();
			insert(&head,a);
			array.push_back(a);
		}
		sort(head);
		sort(array.begin(),array.end());
		if(!isEqual(array,head))
		{
			cout << "no" << endl;
		}
		else
		{
			cout << "yes" << endl;
		}

		clear(head);
		array.clear();
	}
}


int main()
{
	test_sort();
	return(0);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值