一方向チェーン関連操作

5880 ワード

typedef char DataType;

typedef struct node
{
	DataType data;
	struct node *next;
}NODE;


/************************************************************************
   :  CreateSinglyLinkedList
    :	    (Charles Tan)
    :	 2013-4-17
    :       
   :  
   :                                              
************************************************************************/
NODE* CreateSinglyLinkedList()
{
	NODE *head = (NODE*)malloc(sizeof(NODE));
	head->next = NULL;

	return head;
}

/************************************************************************
   :  Insert
    :	    (Charles Tan)
    :	 2013-4-17
    :             
   :  
   :                                              
************************************************************************/
int Insert(NODE *head, DataType data, int index)
{
	NODE *curNode = (NODE*)malloc(sizeof(NODE));
	curNode->data = data;

	if (head == NULL)
	{
		return -1;
	}

	if (index < 0 || index > GetLength(head))
	{
		return -2;
	}

	NODE *p = head;
	for(int i = 0; i < index; i++)
	{
		p = p->next;
	}

	curNode->next = p->next;
	p->next = curNode;

	return 0;
}

/************************************************************************
   :  GetLength
    :	    (Charles Tan)
    :	 2013-4-17
    :          
   :  
   :                                              
************************************************************************/
int GetLength(NODE *head)
{
	int len = 0;
	NODE *p = head;

	if (head == NULL)
	{
		return -1;
	}
	
	while(p->next != NULL)
	{
		len++;
		p = p->next;
	}

	return len;
}

/************************************************************************
   :  Delete
    :	    (Charles Tan)
    :	 2013-4-17
    :               
   :  
   :                                              
************************************************************************/
int Delete(NODE *head, int index)
{
	NODE *pre = head;
	NODE *p;

	if (head == NULL)
	{
		return -1;
	}

	if (index < 0 || index >= GetLength(head))
	{
		return -2;
	}

	for(int i = 0; i < index; i++)
	{
		pre = pre->next;
	}

	p = pre->next;
	pre->next = pre->next->next;
	free(p);
	
	return 0;
}

/************************************************************************
   :  Delete
    :	    (Charles Tan)
    :	 2013-4-17
    :                    
   :  
   :                                              
************************************************************************/
int Delete(NODE *head, DataType data)
{
	NODE *pre = head;
	NODE *p;
	
	if (head == NULL)
	{
		return -1;
	}

	while(pre->next != NULL && pre->next->data != data)
	{
		pre = pre->next;
	}

	if (pre->next == NULL)
	{
		return -3;
	}

	p = pre->next;
	pre->next = pre->next->next;
	free(p);

	return 0;
}

/************************************************************************
   :  ClearList
    :	    (Charles Tan)
    :	 2013-4-17
    :       
   :  
   :                                              
************************************************************************/
int ClearList(NODE *head)
{
	NODE *p = head->next;
	NODE *q;

	if (head == NULL)
	{
		return -1;
	}

	while(p != NULL)
	{
		q = p->next;
		head->next = q;
		free(p);
		p = q;
	}

	return 0;
}

/************************************************************************
   :  IsEmpty
    :	    (Charles Tan)
    :	 2013-4-17
    :           
   :  
   :                                              
************************************************************************/
BOOL IsEmpty(NODE *head)
{
	if (head->next == NULL)
	{
		return true;
	}

	return false;
}

/************************************************************************
   :  GetAt
    :	    (Charles Tan)
    :	 2013-4-17
    :               
   :  
   :                                              
************************************************************************/
DataType GetAt(NODE *head, int index)
{
	NODE *p = head->next;

	if (head == NULL)
	{
		return -1;
	}

	if (index < 0 || index >= GetLength(head))
	{
		return -2;
	}

	for(int i = 0; i < index; i++)
	{
		p = p->next;
	}

	return p->data;
}

/************************************************************************
   :  FindAddr
    :	    (Charles Tan)
    :	 2013-4-17
    :               
   :  
   :                                              
************************************************************************/
NODE* FindAddr(NODE *head, DataType data)
{
	NODE *p = head->next;

	if (head == NULL)
	{
		return NULL;
	}

	while(p != NULL && p->data != data)
	{
		p = p->next;
	}

	if (p == NULL)
	{
		return NULL;
	}

	return p;
}

/************************************************************************
   :  FindIndex
    :	    (Charles Tan)
    :	 2013-4-17
    :               
   :  
   :                                              
************************************************************************/
int FindIndex(NODE *head, DataType data)
{
	int index = 0;
	NODE *p = head->next;

	if (head == NULL)
	{
		return -1;
	}

	while(p != NULL && p->data != data)
	{
		index++;
		p = p->next;
	}

	if (p == NULL)
	{
		return -3;
	}

	return index;
}

/************************************************************************
   :  SetAt
    :	    (Charles Tan)
    :	 2013-4-17
    :               
   :  
   :                                              
************************************************************************/
int SetAt(NODE *head, int index, DataType data)
{
	NODE *p = head->next;

	if (head == NULL)
	{
		return -1;
	}

	if (index < 0 || index >= GetLength(head))
	{
		return -2;
	}

	for(int i = 0; i < index; i++)
	{
		p = p->next;
	}

	p->data = data;

	return 0;
}