作业--day19

头插

作业--day19_第1张图片

/*
 * function:    头插
 * @param [ in] 
 * @param [out] 
 * @return      返回head
 */
pnode_t InsertHead(pnode_t head, datatype element){
	pnode_t p = CreateNode();
	if(NULL == p){
		return head;
	}
	strcpy(p->data, element);
	if(NULL == head){
		head = p;
		return head;
	}else{
		p->next = head;
		p->prev = head->prev;
		head->prev->next = p;
		head->prev = p;
		head = p;
		return head;
	}
}

尾插

作业--day19_第2张图片

/*
 * function:    尾插
 * @param [ in] 
 * @param [out] 
 * @return      返回head
 */
pnode_t InsertTail(pnode_t head, datatype element){
	pnode_t p = CreateNode();
	if(NULL == p){
		return head;
	}
	strcpy(p->data, element);
	if(NULL == head){
		head = p;
		return head;
	}else{
		p->next = head;
		p->prev = head->prev;
		head->prev->next = p;
		head->prev = p;
		return head;
	}
}

头删

作业--day19_第3张图片

/*
 * function:    头删
 * @param [ in] 
 * @param [out] 
 * @return      返回head
 */
pnode_t DeleteHead(pnode_t head){
	if(NULL == head){
		puts("linklist is empty!");
	}else if(NULL == head->next){
		FreeNode(head);
		head = NULL;
	}else{
		pnode_t p = head;
		head = head->next;
		head->prev = p->prev;
		p->prev->next = head;
		FreeNode(p);
		p = NULL;
	}
	return head;
}

尾删

作业--day19_第4张图片

/*
 * function:    尾删
 * @param [ in] 
 * @param [out] 
 * @return      返回head
 */
pnode_t DeleteTail(pnode_t head){
	if(NULL == head){
		puts("linklist is empty!");
	}else if(head == head->next){
		FreeNode(head);
		head = NULL;
	}else{
		pnode_t p = head->prev;
		p->prev->next = head;
		head->prev = p->prev;
		FreeNode(p);
		p = NULL;
	}
	return head;
}

遍历输出

/*
 * function:    遍历输出
 * @param [ in] 
 * @param [out] 
 * @return      
 */
void Output(pnode_t head){
	if(NULL == head){
		puts("linklist is empty!");
		return;
	}
	puts("正向遍历:");
	pnode_t p = head;
	while(head != p->next){
		printf("%s\t", p->data);
		p = p->next;
	}
	printf("%s\n", p->data);
	
	puts("反向遍历:");
	do{
		printf("%s\t", p->data);
		p = p->prev;
	}while(head->prev != p);
	puts("");

}

你可能感兴趣的:(c语言)