선형 표 의 체인 저장 실현

#include 
#define ElementType int     //       

//                
typedef struct LNode* List;
struct LNode
{
	ElementType Data;
	List Next;
};
struct LNode Lnode;
List PtrL;

//   
int Length(List PtrL)
{
	List p = PtrL;
	int cnt = 0;
	while (p)     //       
	{
		p = p->Next;
		cnt++;
	}
	return cnt;
}

//  (   )
List FindKth(List PtrL, int k)
{
	List p = PtrL;
	int i = 1;
	while (p != NULL && i < k)
	{
		p = p->Next;
		i++;
	}
	if (i == k)
		return p;
	else
		return NULL;
}

//  (    )
List FindX(List PtrL, ElementType X)
{
	List p = PtrL;
	while (p && p->Data != X)
	{
		p = p->Next;
	}
	return p;
}

//  (   i - 1       )
List Insert(List PtrL, ElementType X, int i)
{
	List p, s;
	if (i == 1)
	{
		s = (List)malloc(sizeof(struct LNode));
		s->Data = X;
		s->Next = PtrL;
		return s;
	}
	p = FindKth(PtrL, i - 1);
	if (p == NULL)
	{
		printf("Error Augment");
		return NULL;
	}
	else
	{
		s = (List)malloc(sizeof(struct LNode));
		s->Data = X;
		s -> Next = p->Next;
		p->Next = s;
		return PtrL;
	}
}

//  
List Delete(List PtrL, int i)
{
	List p, tmp;
	if (i == 1)
	{
		tmp = PtrL;
		if (PtrL != NULL)
			PtrL = PtrL->Next;
		else
			return NULL;
		free(tmp);
		return PtrL;
	}
	p = FindKth(PtrL, i - 1);
	if (p == NULL)
	{
		printf("The (i-1)th node is not existed!");
		return NULL;
	}
	else if (p->Next == NULL)
	{
		printf("The ith node is not existed!");
		return NULL;
	}
	else
	{
		tmp = p->Next;
		p->Next = tmp->Next;
		free(tmp);
		return PtrL;
	}
}

좋은 웹페이지 즐겨찾기