线性表的链式表示与实现(单链表)

       本示例实现了单链表的存储与基本操作,仅作为一个加深理解的示例,没有经过严格的测试,没有进行算法优化。。。

#include <stdio.h>
#include <stdlib.h>

/*---------Data Object Structure-----------*/
#define ElemType char
typedef struct Node/*Node structure*/
{
	ElemType elem;
	struct Node *next;
}Node,*PNode;

typedef struct SLList/*Single Link List*/
{
	PNode head;  /*Head pointer*/
	int length;  /*List length*/
}SLList;

#define TRUE 1
#define FALSE 0
#define null '0'

/*---------Operation Set-------------------*/
//Initialize the list
int InitList(SLList* list);
//Destroy the list
void DestroyList(SLList* list);
//Removes all of the elements from this list
void ClearList(SLList* list);
//Appends the specified element to the end of this list
int Append(SLList* list,ElemType value);
//Appends the specified list src to the end of the list dist
int AppendList(SLList* dist,const SLList* src);
//Inserts the specified element at the specified position in this list
int Insert(SLList* list,int position,ElemType value);
//Inserts the specified element at the beginning of this list
int InsertFirst(SLList* list,ElemType value);
//Inserts the src list at position of the dist list
int InsertList(SLList* dist,const SLList* src,int position);
//Inserts the specified list src to the beginning of the list dist
int InsertListFirst(SLList* dist,const SLList* src);
//Removes the element at the specified position in this list
ElemType RemoveAt(SLList* list,int position);
//Remove all matched element from the list and return the counts be removed
int Remove(SLList* list,ElemType value);
//Removes the first occurrence of the specified element from this list, if it is present
int RemoveFirstOccurrence(SLList* list,ElemType value);
//Removes the last occurrence of the specified element from this list, if it is present
int RemoveLastOccurrence(SLList* list,ElemType value);
//Removes and returns the first element from this list
ElemType RemoveFirst(SLList* list);
//Removes and returns the last element from this list
ElemType RemoveLast(SLList* list);
//Returns the first element in this list
ElemType Get(const SLList* list,int position);
//Returns the first element in this list
ElemType GetFirst(const SLList* list);
//Returns the last element in this list
ElemType GetLast(const SLList* list);
//Returns PNode at specified position in this list
PNode GetPNode(const SLList* list,int position);
//Returns the first node's pointer in this list
PNode GetFirstPNode(const SLList* list);
//Returns the last node's pointer in this list
PNode GetLastPNode(const SLList* list);
//Returns the index of the first occurrence of the specified element in this list
int IndexOf(const SLList* list,ElemType value);
//Returns the index of the last occurrence of the specified element in this list
int LastIndexOf(const SLList* list,ElemType value);
//Replaces the element at the specified position in this list with the specified value
ElemType Set(SLList* list,int position,ElemType value);
//Replaces the frist element equals to old_value in this list with new_value
int ReplaceFirst(SLList* list,ElemType old_value,ElemType new_value);
//Replaces the last element equals to old_value in this list with new_value
int ReplaceLast(SLList* list,ElemType old_value,ElemType new_value);
//Replaces all elements with old_value in this list with new_value
int Replace(SLList* list,ElemType old_value,ElemType new_value);
//Return the size of this list
int Size(const SLList* list);
//Return wether the list is empty
int IsEmpty(const SLList* list);
//Retrun wether there is a element means to value
int Contains(const SLList* list,ElemType value);
//Return wether sub_list is contained in list
int ConstainsAll(const SLList* list,const SLList* sub_list);
//Get sublist in [from,to]
int SubList(const SLList* list,int from,int to,SLList* sublist);
//Reverse the list
SLList Reverse(SLList* list);
//Return intersection of two list
SLList Intersect(const SLList* list1,const SLList* list2);
//Print list inline 
void PrintList(const SLList * list);
//Print list and return 
void PrintlnList(const SLList * list);  

/*
* Function: Initialize list
* Parameters: list: Address of list
* Return: TRUE/FALSE
*/
int InitList(SLList* list)
{
    //Create head node
    PNode head=(PNode)malloc(sizeof(Node));
    if(!head)
		return FALSE;
    head->elem=null;
    head->next=NULL;	

    list->head=head;
    list->length=0;

    return TRUE;
}

/*
* Function: Destroy list
* Parameters: list: Address of list
* Return: void
*/
void DestroyList(SLList* list)
{
	free(list->head);
	list->head=NULL;
	list->length=0;
}

/*
* Function: Set each element in this list with value null
* Parameters: list: Address of list
* Return: void
*/
void ClearList(SLList* list)
{
	PNode pCurrent=list->head;
	
    while(pCurrent->next!=NULL)
    {
        pCurrent=pCurrent->next;
		pCurrent->elem=null;
    }
	pCurrent->elem=null;
}

/*
* Function: Appends the specified element to the end of this list
* Parameters: list: Address of list
*			  value: Value used to set
* Return: Insertion position
*/
int Append(SLList* list,ElemType value)
{
    PNode pCurrent=list->head;
    PNode pNode=(PNode)malloc(sizeof(Node));

	if(!pNode)
		return -1;
	
    while(pCurrent->next!=NULL)
    {
        pCurrent=pCurrent->next;                      
    }
    
    pNode->elem=value;
    pNode->next=NULL;
    pCurrent->next=pNode;
	pNode=NULL;
//	printf("Append:%c\n",pCurrent->next->elem);
    return ++list->length;
}

/*
* Function: Appends the specified list src to the end of the list dist
* Parameters: dist: Target list
*			  src: Source list
* Return: Insertion position
*/
int AppendList(SLList* dist,const SLList* src)
{
	PNode pSrc=src->head;
	while(pSrc!=GetLastPNode(src))
	{
		pSrc=pSrc->next;
		Append(dist,pSrc->elem);
	}
	
	return dist->length-src->length+1;
}

/*
* Function: Inserts the specified element at the specified position in this list
* Parameters: list: Address of list
*			  position: Insertion position index
*		      value: Value of inserted node
* Return: TRUE/FALSE
*/
int Insert(SLList* list,int position,ElemType value)
{
	PNode pCurrent=list->head;
	PNode pInsert;
	int i=0;

	if(position<1 || position>list->length+1)//Out of bounds
		return FALSE;

	while(i<position-1)//Get last element
	{
		pCurrent=pCurrent->next;
		i++;
	}

	pInsert=(PNode)malloc(sizeof(Node));
	if(!pInsert)
		return FALSE;
	pInsert->elem=value;
	pInsert->next=pCurrent->next;
	pCurrent->next=pInsert;
	list->length++;

	return TRUE;
}

/*
* Function: Inserts the specified element at the beginning of this list
* Parameters: list: Address of list
*			  value: Insertion value
* Return: TRUE/FALSE
*/
int InsertFirst(SLList* list,ElemType value)
{
	PNode pCurrent=list->head;
	PNode pInsert;

	pInsert=(PNode)malloc(sizeof(Node));
	if(!pInsert)
		return FALSE;
	pInsert->elem=value;
	pInsert->next=pCurrent->next;
	pCurrent->next=pInsert;
	list->length++;

	return TRUE;
}

/*
* Function: Inserts the src list at position of the dist list
* Parameters: dist: Target list
*			  src: Source list
*			  position: Insertion position
* Return: TRUE/FALSE
*/
int InsertList(SLList* dist,const SLList* src,int position)
{
	int i=position;
	PNode pDistInsPos;//Insert position
	PNode pSrcCurPos=src->head;//Current position in src list

	if(dist->length<1 || src->length<1)
		return FALSE;

	if(position<1 || position>dist->length+1)
		return FALSE;

	if(position==dist->length+1)//Append
	{
		AppendList(dist,src);
		return TRUE;
	}

	pDistInsPos=GetPNode(dist,position);
	
	while(pSrcCurPos!=GetLastPNode(src))
	{
		pSrcCurPos=pSrcCurPos->next;
		Insert(dist,i++,pSrcCurPos->elem);
	}
	GetPNode(dist,i-1)->next=pDistInsPos;

	return TRUE;
}

/*
* Function: Inserts the specified list src to the beginning of the list dist
* Parameters: dist: Target list
*			  src: Source list
* Return: TRUE/FALSE
*/
int InsertListFirst(SLList* dist,const SLList* src)
{
	PNode pDistInsPos;//Insert position(Original first item)
	PNode pSrcCurPos;//Current position in src list
	int i=0;

	if(dist->length<1 || src->length<1)
		return FALSE;


	pDistInsPos=dist->head->next;
	pSrcCurPos=GetLastPNode(src);

	//Each time the element in src list insert before the first element in dist list
	while(i<src->length)
	{
		PNode pInsert=(PNode)malloc(sizeof(PNode));
		if(!pInsert)
			return FALSE;
		pInsert->elem=pSrcCurPos->elem;
		pInsert->next=pDistInsPos;
		pDistInsPos=pInsert;
		pInsert=NULL;
		i++;
		pSrcCurPos=GetPNode(src,src->length-i);		
	}
	dist->head->next=pDistInsPos;
	dist->length+=src->length;

	return TRUE;
}

/*
* Function: Removes the element at the specified position in this list
* Parameters: position: index of removed element
* Return: Value of removed element
*/
ElemType RemoveAt(SLList* list,int position)
{
	int i=0;
	ElemType value;
	PNode pDel;
	PNode pCurrent=list->head;//Point to head

	if(position<1 || position>list->length)
		return null;

	for(;i<position-1;)//Get prior node of node will deleted
	{
		pCurrent=pCurrent->next;
		i++;
	}
	
	pDel=pCurrent->next;
	value=pDel->elem;
	pCurrent->next=pCurrent->next->next;
	free(pDel);
	list->length--;

	return value;
}

/*
* Function: Remove all elements with specified value
* Parameters: value: Specified value
* Return: Counts of removed elements
*/
int Remove(SLList* list,ElemType value)
{
	int i=0;
	int count=0;
	PNode pDel;
	PNode pCurrent=list->head;//Point to head

	for(;i<list->length;)//Get prior node of node will deleted
	{
		if(pCurrent->next->elem==value)
		{
			pDel=pCurrent->next;
			pCurrent->next=pCurrent->next->next;
			free(pDel);
			list->length--;
			count++;
			continue;//Jump step i++
		}
		pCurrent=pCurrent->next;
		i++;
	}	

	return count;
}

/*
* Function: Removes the first occurrence of the specified element from this list, if it is present
* Parameters: value: Specified value
* Return: Index of removed position
*/
int RemoveFirstOccurrence(SLList* list,ElemType value)
{
	int i=0;
	PNode pDel;
	PNode pCurrent=list->head;//Point to head

	for(;i<list->length;)//Get prior node of node will deleted
	{
		if(pCurrent->next->elem==value)
		{
			pDel=pCurrent->next;
			pCurrent->next=pCurrent->next->next;//Delete
			free(pDel);
			list->length--;
			return ++i;
		}
		pCurrent=pCurrent->next;
		i++;
	}

	return -1;
}

/*
* Function: Removes the last occurrence of the specified element from this list, if it is present
* Parameters: value: Specified value
* Return: Index of removed position
*/
int RemoveLastOccurrence(SLList* list,ElemType value)
{
	int i=0;
	int index=-1;
	PNode pBefDel=NULL;
	PNode pDel=NULL;
	PNode pCurrent=list->head;//Point to head

	for(;i<list->length;)//Get prior node of node will deleted
	{
		if(pCurrent->next->elem==value)//Next element matches
		{
			index=i+1;
			pBefDel=pCurrent;
		}
		pCurrent=pCurrent->next;//Point to next
		i++;
	}

	if(pBefDel)//If find then remove the last one
	{
		pDel=pBefDel->next;
		pBefDel->next=pBefDel->next->next;//Delete
		free(pDel);
		list->length--;
	}

	return index;
}

/*
* Function: Removes and returns the first element from this list
* Parameters: list: Address of list
* Return: Value of removed element
*/
ElemType RemoveFirst(SLList* list)
{
	PNode pDel;
	ElemType value;

	if(0==list->length)//Empty
		value=null;
	else if(1==list->length)//Just one element
	{
		value=list->head->next->elem;
		free(list->head->next);
		list->head=NULL;
		list->length--;
	}
	else
	{
		pDel=list->head->next;
		value=pDel->elem;
		list->head->next=list->head->next->next;
		free(pDel);
		list->length--;
	}

	return value;
}

/*
* Function: Removes and returns the last element from this list
* Parameters: list: Address of list
* Return: Value of removed element
*/
ElemType RemoveLast(SLList* list)
{
	PNode pCurrent=list->head;
	PNode pDel;
	ElemType value;

	while(pCurrent->next->next!=NULL)
		pCurrent=pCurrent->next;

	pDel=pCurrent->next;
	value=pDel->elem;
	pCurrent->next=NULL;//Delete last one
	free(pDel);
	list->length--;

	return value;
}

/*
* Function: Returns the first element in this list
* Parameters: 
* Return: Value of specified element
*/
ElemType Get(const SLList* list,int position)
{
	int i=0;
	PNode pCurrent=list->head;//Point to head

	if(position<1 || position>list->length)
		return null;

	for(;i<position;)
	{
		pCurrent=pCurrent->next;
		i++;
	}

	return pCurrent->elem;
}

/*
* Function: Returns the first element in this list
* Parameters: list: Address of list
* Return: Element value
*/
ElemType GetFirst(const SLList* list)
{
	return list->head->next->elem;
}

/*
* Function: Returns the last element in this list
* Parameters: list: Address of list
* Return: Element value
*/
ElemType GetLast(const SLList* list)
{
	PNode pCurrent=list->head;

	while(pCurrent->next!=NULL)
		pCurrent=pCurrent->next;

	return pCurrent->elem;
}

/*
* Function: Returns PNode at specified position in this list
* Parameters: 
* Return: Pointer of found node
*/
PNode GetPNode(const SLList* list,int position)
{
	int i=0;
	PNode pCurrent=list->head;//Point to head

	if(position<1 || position>list->length)
		return null;

	while(i<position)
	{
		pCurrent=pCurrent->next;
		i++;
	}

	return pCurrent;
}

/*
* Function: Returns the first node's pointer in this list
* Parameters: 
* Return: Pointer of found node
*/
PNode GetFirstPNode(const SLList* list)
{
	return list->head->next;
}

/*
* Function: Returns the last node's pointer in this list
* Parameters: 
* Return: Pointer of found node
*/
PNode GetLastPNode(const SLList* list)
{
	PNode pCurrent=list->head;

	while(pCurrent->next!=NULL)
		pCurrent=pCurrent->next;

	return pCurrent;
}

/*
* Function: Returns the index of the first occurrence of the specified element in this list
* Parameters: value: Value used to match
* Return: Index of found element
*/
int IndexOf(const SLList* list,ElemType value)
{
	int i=0;
	PNode pCurrent=list->head;//Point to head

	for(;i<list->length;)
	{
		if(pCurrent->next->elem==value)//Matches
			return ++i;

		pCurrent=pCurrent->next;
		i++;
	}

	return -1;
}

/*
* Function: Returns the index of the last occurrence of the specified element in this list
* Parameters: value: Value used to match
* Return: Index of found element
*/
int LastIndexOf(const SLList* list,ElemType value)
{
	int i=0;
	int index=-1;

	PNode pCurrent=list->head;//Point to head

	for(;i<list->length;)
	{
		if(pCurrent->next->elem==value)//Next element matches
		{
			index=i+1;
		}
		pCurrent=pCurrent->next;//Point to next
		i++;
	}

	return index;
}

/*
* Function: Replaces the element at the specified position in this list with the specified value
* Parameters: 
* Return: Original value
*/
ElemType Set(SLList* list,int position,ElemType value)
{
	ElemType old_value;

	if(position<1 || position>list->length)
		return null;

	old_value=GetPNode(list,position)->elem;
	GetPNode(list,position)->elem=value;//Set new value

	return old_value;
}

/*
* Function: Replaces the frist element equals to old_value in this list with new_value
* Parameters: 
* Return: Position of repaced element
*/
int ReplaceFirst(SLList* list,ElemType old_value,ElemType new_value)
{
	int i=1;

	PNode pCurrent;

	if(list->length<=0)//Empty list
		return -1;

	pCurrent=list->head->next;//Point to head

	for(;i<=list->length;)//Get the node will be replaced
	{
		if(pCurrent->elem==old_value)
		{
			pCurrent->elem=new_value;//Replace
			return i;
		}
		pCurrent=pCurrent->next;
		i++;
	}

	return -1;
}

/*
* Function: Replaces the last element equals to old_value in this list with new_value
* Parameters: 
* Return: Position of repaced element
*/
int ReplaceLast(SLList* list,ElemType old_value,ElemType new_value)
{
	int i=1;
	int index=-1;

	PNode pCurrent;

	if(list->length<=0)//Empty list
		return -1;

	pCurrent=list->head->next;//Point to head

	for(;i<=list->length;)//Get the node will be replaced
	{
		if(pCurrent->elem==old_value)//Matches
		{
			index=i;
		}
		pCurrent=pCurrent->next;//Point to next
		i++;
	}

	if(index!=-1)//Exist
		Set(list,index,new_value);

	return index;
}

/*
* Function: Replaces all elements with old_value in this list with new_value
* Parameters: 
* Return: Counts of replaced element
*/
int Replace(SLList* list,ElemType old_value,ElemType new_value)
{
	int i=1;
	int count=0;//Counts of replaced elements

	PNode pCurrent;

	if(list->length<=0)//Empty list
		return -1;

	pCurrent=list->head->next;//Point to head
	for(;i<=list->length;)//Get the node will be replaced
	{
		if(pCurrent->elem==old_value)//Matches
		{
			pCurrent->elem=new_value;//Replace
			count++;
		}
		pCurrent=pCurrent->next;//Point to next
		i++;
	}

	return count;
}

/*
* Function: Get the size of this list
* Parameters: 
* Return: Size of list
*/
int Size(const SLList* list)
{
	return list->length;
}

/*
* Function: Wether the list is empty
* Parameters: list: Address of list
* Return: TRUE/FALSE
*/
int IsEmpty(const SLList* list)
{
	if(list->length<=0)
		return TRUE;

	return TRUE;
}

/*
* Function: Wether there is a element means to value
* Parameters: 
* Return: TRUE/FALSE
*/
int Contains(const SLList* list,ElemType value)
{
	PNode pCurrent;
	PNode pEnd;

	if(list->length<=0)
		return FALSE;

	pCurrent=list->head->next;//Points to first element
	pEnd=GetLastPNode(list);

	while(pCurrent!=pEnd)
	{
		if(value==pCurrent->elem)
			return TRUE;
		pCurrent=pCurrent->next;
	}

	if(value==pCurrent->elem)//Test last one
		return TRUE;

	return FALSE;
}

/*
* Function: Return wether sub_list is contained in list
* Parameters: 
* Return: TRUE/FALSE
*/
int ConstainsAll(const SLList* list,const SLList* sub_list)
{
	PNode pCurrent;
	PNode pEnd;
	PNode pSubCurrent;
	PNode pSubEnd;
	int index=1;

	if(list->length<=0 || list->length<sub_list->length)//list is empty or less than sub_list
		return FALSE;
	if(sub_list<=0)//sub_list is empty 
		return TRUE;

	pCurrent=list->head->next;//Points to the first element of list
	pEnd=GetLastPNode(list);
	pSubCurrent=sub_list->head->next;//Points to the first element of sub_list
	pSubEnd=GetLastPNode(sub_list);

	while(pSubCurrent<=pSubEnd)
	{
		if(pCurrent->elem==pSubCurrent->elem)//Matches
		{
			pCurrent=pCurrent->next;
			index++;
			if(pSubCurrent==pSubEnd)//Succeed
				return TRUE;
			pSubCurrent=pSubCurrent->next;
		}
		else
		{
			pCurrent=pCurrent->next;//Move on
			index++;
			if( (list->length-index+1) < sub_list->length)//Not enough elements to compare
				return FALSE;
			pSubCurrent=sub_list->head->next;//Reset
		}
		
	}

	return FALSE;
}

/*
* Function: Get sublist in [from,to]
* Parameters: 
* Return: TRUE/FALSE
*/
int SubList(const SLList* list,int from,int to,SLList* sublist)
{
	int count;
	PNode pCurrent;
	
	InitList(sublist);

	if(list->length<=0 || from<1 || to <1 || to>list->length || from>to)
		return FALSE;

	pCurrent=GetPNode(list,from);
	count=to-from+1;

	while(count>0)
	{
		Append(sublist,pCurrent->elem);
		pCurrent=pCurrent->next;
		count--;
	}

	return TRUE;
}

/*
* Function: Reverse the list
* Parameters: list: Address of list
* Return: Reversed list
*/
SLList Reverse(SLList* list)
{
	int count;
	PNode pCurrent;
	SLList newList;
	InitList(&newList);

	if(list->length<=0)
		return newList;

	pCurrent=list->head->next;
	count=list->length;

	while(count>0)
	{
		InsertFirst(&newList,pCurrent->elem);
		pCurrent=pCurrent->next;
		count--;
	}

	return newList;
}

/*
* Function: Get intersection of two list
* Parameters: 
* Return: Intersection list
*/
SLList Intersect(const SLList* list1,const SLList* list2)
{
	SLList commList;
	InitList(&commList);

	if(list1->length<1 || list2->length<1)
		return commList;

	if(list1->length > list2->length)
	{
		PNode pCurrent=list2->head->next;
		int i=list2->length;
		while(i>0)
		{
			if(Contains(list1,pCurrent->elem))
				Append(&commList,pCurrent->elem);
			pCurrent=pCurrent->next;
			i--;
		}
	}
	else
	{
		PNode pCurrent=list1->head->next;
		int i=list1->length;
		while(i>0)
		{
			if(Contains(list2,pCurrent->elem))
				Append(&commList,pCurrent->elem);
			pCurrent=pCurrent->next;
			i--;
		}
	}

	return commList;
}

/*
* Function: Print list inline 
* Parameters: list: Address of list
* Return: void
*/
void PrintList(const SLList * list)
{
	if(0==list->length)  /*Empty*/
		printf("null");  
	else  
	{  
		PNode pNode=list->head->next;
		
		while(pNode->next!=NULL)
		{
			printf("%c ",pNode->elem);                                   
			pNode=pNode->next;
		}
		
		printf("%c",pNode->elem);
	}
}

/*
* Function: Print list and return a new line 
* Parameters: list: Address of list
* Return: void
*/
void PrintlnList(const SLList * list)
{
//	printf("len:%d\n",list->length);
	if(0==list->length)  /*Empty*/
		printf("null\n");  
	else  
	{  
		PNode pNode=list->head->next;
		
		while(pNode->next!=NULL)
		{
			printf("%c ",pNode->elem);                                   
			pNode=pNode->next;
		}
		
		printf("%c\n",pNode->elem);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值