데이터 구조 (C 언어) - 단 방향 링크

5632 단어 데이터 구조
C 언어의 단 방향 링크 는 일부 데 이 터 를 하나의 구조 체 에 넣 은 다음 구조 체 에 struct xxx * next 의 구성원 을 추가 하여 다음 노드 를 가리 키 는 데 사용 하 는 것 이다.
인용 할 때 임시 구조 체 변 수 를 만들어 서 인용 합 니 다.
원 구조 체 변수 가 struct xxx * p 이면 struct xxx * temp 를 만 들 수 있 습 니 다.  그리고 for (temp = p; temp - > next! = NULL;  temp = temp - > next) 옮 겨 다 니 며 참조 합 니 다.
다음은 개인 이 쓴 단 방향 링크 의 일부 기능 을 실현 합 니 다.
# include 

struct Student
{
	int id;
	struct Student * next;
};

void menu()
{
	struct Student * InitList();
	int DestroyList( struct Student *p);
	int ClearList( struct Student *p);
	int ListEmpty( struct Student *p);
	int ListLength( struct Student *p);
	struct Student * GetElem(int i, struct Studeent *p);
	struct Student * PriorElem(struct Student *p, int i);
	struct Student * NextElem(struct Student *p, int i);
	int ListInsert(struct Student *p, int i, int j);   //  id   i    , id   j
	int ListCat(struct Student *p, int i);       //     
	int ListDelete(struct Student *p, int i);
	struct Student * ListCopy(struct Student *p);
	struct Student * ListCombOrder(struct Student *p, struct Student *q);   //       
	struct Student * ListCombDisorder( struct Student *p, struct Student *q);  //        
	struct Student ** ListDivOrder(struct Student * p, int i);      //  id   i   ,     id   i   
	struct Student ** ListDivDisorder(struct Student *p, int n);    //         n-1
}

struct Student * InitList()
{
	struct Student * p;
	p = (struct Student *) malloc(sizeof(struct Student));
	p->next = NULL;
	return p;
}

int DestoryList( struct Student *p)
{
	free(p);
	return 1;   //  1      
}

int ClearList( struct Student *p)
{
	p->next = NULL;
	return 1;
}

int ListEmpty( struct Student *p)
{
	if (p->next == NULL)
		return 0;
	else return 1;
}

int ListLength( struct Student *p)
{
	int length=0;
	struct Student * tempstu;
	for (tempstu = p; tempstu->next != NULL; tempstu = tempstu->next )
		length++;
	return length;
}

struct Student * GeteElem(int i, struct Student *p)
{
	struct Student * tempstu;
	for (tempstu = p; tempstu->id != i; tempstu = tempstu->next)
		if (tempstu->next == NULL)
			break;
	if ( tempstu->id == i )
		return tempstu;
	else return NULL;
}

struct Student * PriorElem(struct Student *p, int i)
{
	struct Student * tempstu, * ptempstu;
	ptempstu = p;
	tempstu = p->next;
	for (; tempstu->id != i; tempstu = tempstu->next )
	{
		if (tempstu->next == NULL)
			break;
		ptempstu = ptempstu->next;
	}
	
	if (tempstu->id == i)
		return ptempstu;
	else return NULL;
}

struct Student * NextElem(struct Student *p, int i)
{
	struct Student * tempstu;
	for (tempstu = p; tempstu->id != i; tempstu = tempstu->next )
		if (tempstu->next == NULL)
			break;

	if (tempstu->id == i)
		return tempstu->next;
	else return NULL;
}

int ListInsert(struct Student *p, int i, int j)
{
	struct Student * beginstu;
	for (beginstu = p; p->id != i; p = p->next );
	struct Student *q;
	q = p;
	q = q->next;
	q->id = j;
	q->next = p->next;
	p = beginstu;
	return 1;
}

int ListCat(struct Student *p, int i)
{
	int ListLength(struct Student *p);
	int n = ListLength(p);
	struct Student * tempstu = p;

	for (int j = 0; j < n; j++)
		tempstu = tempstu->next;
	tempstu->id = i;
	tempstu->next = NULL;
	return 1;
}

int ListDelete(struct Student *p, int i)
{
	struct Student * tempstu;
	for ( tempstu = p; tempstu->id != i; tempstu = tempstu->next );
	tempstu = tempstu->next->next;
	return 1;
}

struct Student * ListCopy(struct Student *p)
{
	struct Student *tempstu, *q;
	tempstu = p;
	for (q = p; p->next != NULL; p = p->next )
	{
		q->id = p->id;
		q = q->next;
	}
	p = tempstu;
	return q;
}

struct Student * ListCombOrder(struct Student *p, struct Student *q)   //       
{
	struct Student * TComb, *Comb;
	TComb = Comb;
	struct Student *pComb, *qComb;
	pComb = p;  qComb = q;

	while( p->next == NULL || q->next == NULL)
		if (p->id > q->id)
		{
			TComb->id = p->id;
			TComb = TComb->next;
			p = p->next;
		}
		else
		{
			TComb->id = q->id;
			TComb = TComb->next;
			q = q->next;
		}

	if (p->next != NULL)
		for (; p->next != NULL; p = p->next )
		{
			TComb->id = p->id;
			TComb = TComb->next;
		}
	else 
		for (; q->next != NULL; q = q->next )
		{
			TComb->id = p->id;
			TComb = TComb->next;
		}
	TComb->next = NULL;

	p = pComb;
	q = qComb;
	return Comb;
}

struct Student * ListCombDisorder( struct Student *p, struct Student *q)    //        
{
	struct Student * Comb, * TComb;
	TComb = Comb;
	struct Student * pComb, * qComb;
	pComb = p;   qComb = q;

	for (; p->next != NULL; p = p->next)
	{
		TComb->id = p->id;
		TComb = TComb->next;
	}

	TComb->id = p->id;
	TComb->next = q;

	for (; q->next != NULL; q = q->next)
	{
		TComb->id = q->id;
		TComb = TComb->next;
	}
	TComb->next = NULL;

	return Comb;
}


struct Student ** ListDivOrder(struct Student * p, int i)            //       
{ 
	struct Student * stu[2];
	struct Student * tempstu;
	stu[0] = p;
	for ( tempstu = stu[0]; tempstu->id != i; tempstu = tempstu->next );
	stu[1] = tempstu;
	tempstu->next = NULL;
	return stu;
}

struct Student ** ListDivDisorder(struct Student *p, int n)          //        
{
	struct Student *stu[2];
	struct Student * tempstu;
	int count = 0;
	stu[0] = p;
	for (tempstu = stu[0]; count < n; count++ )
		tempstu = tempstu->next;
	stu[1] = tempstu;
	tempstu->next = NULL;
	return stu;
}

(상기 기능 은 제 가 생각 할 수 있 는 모든 필요 한 기능 입 니 다. 누락 이 있 으 면 제출 하 십시오)

좋은 웹페이지 즐겨찾기