您的位置:首页 > 其它

一个简单的链表结构

2015-06-14 16:39 381 查看
仅仅实现了基本的链表操作,如创建、查找、删除、排序等。

//头文件
/*there is no head node exist
 *
 */
#include <iostream>

using namespace std;

typedef struct Node{
	int value;
	struct Node* next;
}Node,*ListNode;

bool isEmpty(ListNode );//judge the list status
bool createList(ListNode* ,const int=10);//create new list
bool printList(ListNode ,ostream& output=cout);//print the list
int size(ListNode);
bool insert(ListNode* ,int ,int);
//insert in the last
bool insertTail(ListNode* ,int );

bool find(ListNode ,int ,int& );//find a node in list
bool deleteNode(ListNode* ,int ,int& );
bool deleteNode(ListNode* ,int ,bool flag=true);
bool getData(ListNode ,int ,int& );
bool deleteList(ListNode* );//delete the list entire

ListNode sort(ListNode );//sort,and return a new list
int fmin(ListNode );
int fmax(ListNode );


//源文件
#include <time.h>
#include "List.h"
#include <stdlib.h>

bool isEmpty(ListNode pHead)
{
	if(!pHead)
		return true;
	return false;
}

bool createList(ListNode* pHead,const int N)
{
	if(N<0)
		return false;
	if(N==0)
		return true;		
	static size_t randSeed=100;
	ListNode p1=new Node();
	if(!p1)//apply for memory failed
		return false;
	srand(time(0)+randSeed);
	p1->value=rand()%100+1;
	*pHead=p1;
	for(int i=0;i<N-1;++i)
	{
		srand(time(0)+randSeed);
		ListNode pNew=new Node();
		if(!pNew)
		{
			p1->next=NULL;
			return false;
		}
		pNew->value=rand()%100+1;
		p1->next=pNew;
		p1=pNew;
		randSeed+=rand();
	}
	p1->next=NULL;//the last node pointer to NULL
	return true;
}

bool printList(ListNode pHead,ostream& output)
{
	if(!pHead)
		return false;
	ListNode p=pHead;
	while(p)
	{
		output<<p->value<<"   ";
		p=p->next;
	}
	return true;	
}

int size(ListNode pHead)
{
	ListNode p=pHead;
	int length(0);
	while(p)
	{
		length++;
		p=p->next;		
	}
	return length;
}

bool insert(ListNode* pHead,int pos,int data)
{//insert before the pos node
	if(pHead==NULL)
		return false;
	ListNode p,k;
	k=p=*pHead;
	int j=1;
	//pos should more than 0
	while(p && j<pos)
	{
		k=p;
		p=p->next;
		++j;
	}
	//if the list is empty,insert will be refused
	if(!p || j>pos)//if pos<1,return;
		return false;
	ListNode q;
	q=new Node();
	q->value=data;
	q->next=p;
	if(k==p)//在第一个节点前插入
	{
		*pHead=q;
	}
	else
		k->next=q;
	return true;
}

bool insertTail(ListNode* pHead,int data)
{
	if(pHead==NULL)
		return false;
	ListNode p=*pHead;
	if(!p)
	{
		ListNode q=new Node();
		q->value=data;
		q->next=NULL;
		*pHead=q;
		return true;
	}
	while(p->next)
	{
		p=p->next;
	}
	ListNode q=new Node();
	q->value=data;
	q->next=NULL;
	p->next=q;
	
	return true;
}

bool deleteNode(ListNode* pHead,int pos,int& value)
{
	if(pHead==NULL || *pHead==NULL)
		return false;
	ListNode p,q;
	p=q=*pHead;
	int j=1;
	while(p && j<pos)
	{
		q=p;
		p=p->next;
		j++;
	}
	if(!p || j>pos)
		return false;
	if(p==q)
	{
		value=p->value;
		*pHead=p->next;
		delete p;
		p=NULL;
		return true;
	}
	else
	{
		value=p->value;
		q->next=p->next;
		delete p;
		p=NULL;
		return true;
	}
}

//delete the node where value=data
//if flag=true,delete all node where value=data,or delete the first one
bool deleteNode(ListNode* pHead,int data,bool flag)
{
	if(pHead==NULL || *pHead==NULL)
		return false;
	ListNode p,q;
	p=q=*pHead;
	if(flag)
	{
		while(p)
		{
			if(p->value==data)
			{
				if(p==q)
				{
					*pHead=p->next;
					delete p;
					p=q=*pHead;
				}
				else
				{
					q->next=p->next;
					delete p;
					p=q->next;
				}
			}
			q=p;
			p=p->next;		
		}
	}
	else
	{
		while(p && p->value!=data)
		{
			q=p;
			p=p->next;
		}
		if(!p)
			return false;
		if(p==q)
		{
			*pHead=p->next;
			delete p;
			p=NULL;
		}
		else
		{
			q->next=p->next;
			delete p;
			p=NULL;
		}
	}
	return true;
}

//find data in list,pos is the position in list
bool find(ListNode pHead,int data,int& pos)
{
	ListNode p=pHead;
	pos=0;
	while(p)
	{
		pos++;
		if(p->value==data)
		{
			return true;
		}
		p=p->next;
	}
	return false;
}

//read the value in k node
bool getData(ListNode pHead,int k,int& data)
{
	if(pHead==NULL)
		return false;
	if(k<=0 || k>size(pHead))
		return false;
	int i=1;
	ListNode p=pHead;
	while(p && i<k)
	{
		i++;
		p=p->next;
	}
	if(!p)
		return false;
	data=p->value;
	return true;
}

bool deleteList(ListNode* pHead)
{
	if(pHead==NULL)
		return false;
	ListNode p=*pHead;
	while(p)
	{
		ListNode q=p->next;
		delete p;
		p=q;		
	}
	*pHead=NULL;
	return true;
}

//select sort
ListNode sort(ListNode pHead)
{
	ListNode pHeadNew;
	pHeadNew=NULL;
	if(!pHead)
		return pHeadNew;
	while(pHead)
	{
		int min=fmin(pHead);
		deleteNode(&pHead,min,false);
		insertTail(&pHeadNew,min);
	}
	return pHeadNew;
}

int fmax(ListNode pHead)
{
	int max=0x80000000;
	ListNode p=pHead;
	while(p)
	{
		if(p->value > max)
		{
			max=p->value;
		}
		p=p->next;
	}
	return max;	
}

int fmin(ListNode pHead)
{
	int min=0x7FFFFFFF;
	ListNode p=pHead;
	while(p)
	{
		if(p->value < min)
		{
			min=p->value;
		}
		p=p->next;
	}
	return min;	
}


//测试程序
#include <iostream>
#include "List.h"

using namespace std;

const int N=10;

int main()
{
	ListNode pHead;
	pHead=NULL;
	if(!createList(&pHead,N))
		cout<<"create list failed!"<<endl;
	cout<<"The initial length:"<<size(pHead)<<endl;
	
	cout<<endl<<"print list:"<<endl;
	if(!isEmpty(pHead))
		printList(pHead);

	insert(&pHead,1,45);
	insert(&pHead,3,30);
	cout<<endl<<"print list:"<<endl;
	if(!isEmpty(pHead))
		printList(pHead);
	
	insertTail(&pHead,18);
	cout<<endl<<"print list:"<<endl;
	if(!isEmpty(pHead))
		printList(pHead);
	
	int valueToBeDeleted;
	deleteNode(&pHead,1,valueToBeDeleted);
	cout<<endl<<"print list:"<<endl;
	if(!isEmpty(pHead))
		printList(pHead);
	
	valueToBeDeleted=2;
	deleteNode(&pHead,valueToBeDeleted);
	cout<<endl<<"print list:"<<endl;
	if(!isEmpty(pHead))
		printList(pHead);
	
	int value;
	cout<<endl<<"find result:"<<find(pHead,18,value)<<endl;
	cout<<"position:"<<value<<endl;
	
	cout<<"read data:"<<getData(pHead,11,value)<<endl;
	cout<<"data:"<<value<<endl;
	
	cout<<"min:"<<fmin(pHead)<<endl;
	
	ListNode newList=sort(pHead);
	if(!isEmpty(newList))
		printList(newList);
	else
		cout<<"list is empty..."<<endl;
	
	deleteList(&pHead);
	if(!isEmpty(pHead))
		printList(pHead);
	else
		cout<<endl<<"list is empty..."<<endl;	
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: