노 헤드 노드 싱글 체인 시계
                                            
 2345 단어  데이터 구조
                    
typedef int ELEM_TYPE;
typedef struct Node
{
	ELEM_TYPE data;
	struct Node* next;
}Node,*PNode;
void Init(PNode* phead);
PNode BuyNode(ELEM_TYPE val);
bool InsertTail(PNode* phead,ELEM_TYPE val);
bool InsertHead(PNode* phead,ELEM_TYPE val);
void Show(PNode phead);
bool DeleteKey(PNode* phead,ELEM_TYPE key);
void clear(PNode* phead);Link.cpp
#include 
#include 
#include 
#include "Link.h"
void Init(PNode* phead)
{
	if(phead == NULL)
	{
		return;
	}
	*phead = NULL;
}
PNode BuyNode(ELEM_TYPE val)
{
	PNode pnewnode = (PNode)malloc(sizeof(Node));
	if(pnewnode == NULL) exit(0);
	pnewnode->data = val;
	pnewnode->next = NULL;
	return pnewnode;
}
bool InsertTail(PNode* phead,ELEM_TYPE val)
{
	if(phead == NULL)
	{
		return false;
	}
	if(*phead == NULL)
	{
		PNode pnewnode = BuyNode(val);
		*phead = pnewnode;
		return true;
	}
	PNode pcur = *phead;
	PNode pnewnode = BuyNode(val);
	while(pcur->next != NULL)
	{
		pcur = pcur->next;
	}
	pcur->next = pnewnode;
	return true;
}
bool InsertHead(PNode* phead,ELEM_TYPE val)
{
	if(phead == NULL)
	{
		return false;
	}
	PNode pnewnode = BuyNode(val);
	pnewnode->next =*phead;
	(*phead) = pnewnode;
	return true;
}
bool DeleteKey(PNode* phead,ELEM_TYPE key)
{
	if(phead == NULL)
	{
		return false;
	}
	PNode pfront = *phead;
	while(pfront->next != NULL)
	{
		if(pfront->next->data == key)
		{
			break;
		}
		pfront = pfront->next;
	}
	if(pfront->next == NULL)
	{
		return false;
	}
	PNode pcur = pfront->next;
	pfront->next = pcur->next;
	free(pcur);
	return true;
}
void Show(PNode phead)
{
	PNode pcur = phead->next;
	while(pcur->next != NULL)
	{
		printf("%d ",pcur->data);
		pcur = pcur->next;
	}
	printf("
");
}
void clear(PNode* phead)
{	
	struct Node* pCur = *phead;	
	struct Node* pNext ;	
	while(pCur->next != NULL)	
	{		
		pNext = pCur->next;		
		free(pCur);		
		pCur = pNext;	
	}	
	*phead = NULL;
}
   main.cpp
#include 
#include "Link.h"
int main()
{
	PNode head;
	Init(&head);
	for(int i=0; i<10; i++)
	{
		InsertTail(&head,i);
	}
	DeleteKey(&head,4);
	Show(head);
	clear(&head);
} 이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
정수 반전Udemy 에서 공부 한 것을 중얼거린다 Chapter3【Integer Reversal】 (예) 문자열로 숫자를 반전 (toString, split, reverse, join) 인수의 수치 (n)가 0보다 위 또는 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.