一个简单的链表结构

仅仅实现了基本的链表操作,如创建、查找、删除、排序等。

//头文件
/*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;
}
时间: 2024-08-02 16:04:28

一个简单的链表结构的相关文章

利用java实现一个简单的链表结构

定义: 所谓链表就是指在某节点存储数据的过程中还要有一个属性用来指向下一个链表节点,这样的数据存储方式叫做链表 链表优缺点: 优点:易于存储和删除 缺点:查询起来较麻烦 下面我们用java来实现如下链表结构: 首先定义节点类: 复制代码package LinkTest;/** 链表节点类 @author admin */public class Node {private int value;//存储数据private Node next;//下一个节点/** 定义构造器 @param vlau

建立和输出一个简单的链表

#include <stdio.h> #define NULL 0 struct student { long num; float score; struct student * next }; void main() { struct student a,b,c, *head, *p; a.num = 1001; a.score = 89.2; b.num = 1002; b.score = 90.1; c.num = 1003; c.score = 92.1; head = &a

(原创)用Java实现链表结构对象:单向无环链表

转载请注明本文出处:http://www.cnblogs.com/Starshot/p/6918569.html 链表的结构是由一个一个节点组成的,所谓链,就是每个节点的头尾连在一起.而单向链表就是:每个节点包含了当前节点的值和下一个节点引用.双向链表就是每个节点包含了当前节点的值和上下两个节点的引用.相对于数组结构,链表的增删效率会更加高. 这边文章主要讲怎么用Java实现一个简单的链表结构:单向无环链表.以及实现一些数据处理的方法. 首先,新建一个节点类(本次例子中的节点值都是字符串类型):

来写一个简单的PHP MVC结构

MVC结构,其实就是三个Model,Contraller,View单词的简称,Model,主要任务就是把数据库或者其他文件系统的数据按照我们需要的方式读取出来.View,主要负责页面的,把数据以html的形式显示给用户.Controller,主要负责业务逻辑,根据用户的 Request进行请求的分配,比如说显示登陆界面,就需要调用一个控制器userController的方法loginAction来显示.云鼎娱乐城 下面我们用PHP来创建一个简单的MVC结构系统. 首先创建单点入口,即bootst

利用C++实现一个链表结构

利用C++实现链表结构 1.定义链表的数据结构 CList作为一个链表类,它的成员是由CNode组成 CNode有两个属性,tElement用于指向当前的节点,next用于指向下一个节点 /******************************************************** ** CList链表结构实现 ** ** ** ***********************************************************/ #ifndef _LIST

链表结构(贪吃蛇小游戏)

实现原理 为蛇的身体每一个节点添加脚本BodyFollow,使其能跟随父节点移动和设置它的子节点 1 public class BodyFollow : MonoBehaviour { 2 3 //下一个节点. 4 public BodyFollow next; 5 6 //本节点当前位置. 7 Vector3 originalPisition; 8 9 //节点移动的方法. 10 public void Follow(Vector3 gotoPosition) 11 { 12 //移动前记录下

[转]PHP实现MVC开发: 一个简单的MVC

今天研究了下PHP MVC结构,所以决定自己写个简单的MVC,以待以后有空再丰富.至于什么MVC结构,其实就是三个Model,Contraller,View单词的简称,,Model,主要任务就是把数据库或者其他文件系统的数据按 照我们需要的方式读取出来.View,主要负责页面的,把数据以html的形式显示给用户.Controller,主要负责业务逻辑,根据用户的 Request进行请求的分配,比如说显示登陆界面,就需要调用一个控制器userController的方法loginAction来显示.

PHP (20140523)PHP实现MVC开发: 一个简单的MVC

今天研究了下PHP MVC结构,所以决定自己写个简单的MVC,以待以后有空再丰富.至于什么MVC结构,其实就是三个Model,Contraller,View单词的简称,,Model,主要任务就是把数据库或者其他文件系统的数据按 照我们需要的方式读取出来.View,主要负责页面的,把数据以html的形式显示给用户.Controller,主要负责业务逻辑,根据用户的 Request进行请求的分配,比如说显示登陆界面,就需要调用一个控制器userController的方法loginAction来显示.

简单的链表实现

Java LinkedList底层是基于双向链表来实现的,为了更好的理解其实现原理,自己对简单的链表结构做了Java实现,代码如下 class MyLinkedList public MyLinkedList() { this.size = 0; this.last = null; this.first = null; head = first; } public boolean add(E e){ linkLast(e); return true; } public void reset(){