user-0 提问: 带有头结点的双链表的实现和相关操作

回复:
user: user-0
content: 哥哥，能帮我注释一下么，有的看不懂…

user: user-0
content: 引用 1 楼 李文强liwq的回复:双向链表属于基本数据结构，网上资料太多了。下面是我写的，仅供参考。   #include <stdio.h> #include <stdlib.h>  typedef struct T_Node { 	int iValue; 	struct T_Node *pLeftNode; 	struct T_Node *pRightNode; }TNode;  static TNode *g_ptHeadNode = NULL;  TNode *func_create(int iValue) { 	TNode *ptNode = (TNode*)malloc(sizeof(TNode)); 	if(NULL == ptNode) 	{ 		return NULL; 	} 	 	ptNode->pLeftNode = NULL; 	ptNode->pRightNode = NULL;	 	ptNode->iValue = iValue; 	 	return ptNode; }  int func_isEmptyCheck() { 	if(NULL == g_ptHeadNode) 	{ 		return -1; 	} 	 	return 0; }  /*插入尾部*/ void func_tailInsert() { 	int iValue = 0; 	int iRet = func_isEmptyCheck(); 	if(-1 == iRet) 	{ 		printf("请输入头结点的数值:"); 		scanf("%d",&iValue);		 		g_ptHeadNode = func_create(iValue); 		 		return; 	} 	 	printf("请输入要添加的尾节点的数值:"); 	scanf("%d",&iValue); 	TNode *ptNode = func_create(iValue); 	 	TNode *ptTailNode = g_ptHeadNode; 	 	while(ptTailNode->pRightNode) 	{ 		ptTailNode = ptTailNode->pRightNode; 	} 	 	ptTailNode->pRightNode = ptNode; 	 	ptNode->pLeftNode = ptTailNode; 	ptNode->pRightNode = NULL; 	 	return; }  /*插入头部*/ void func_headInsert() { 	int iValue = 0; 	int iRet = func_isEmptyCheck(); 	if(-1 == iRet) 	{ 		printf("请输入头结点的数值:"); 		scanf("%d",&iValue);		 		g_ptHeadNode = func_create(iValue); 		 		return; 	} 	 	printf("请输入要添加的头节点的数值:"); 	scanf("%d",&iValue); 	TNode *ptNode = func_create(iValue); 	 	TNode *ptRightNode = g_ptHeadNode->pRightNode; 	 	g_ptHeadNode->pLeftNode = ptNode;	 	ptNode->pRightNode = g_ptHeadNode; 	 	g_ptHeadNode = ptNode; 	 	return; }  /*删除尾部*/ void func_tailDelete() { 	int iValue = 0; 	int iRet = func_isEmptyCheck(); 	if(-1 == iRet) 	{ 		printf("双向链表是空的\n"); 		return; 	}	 	 	TNode *ptTailNode = g_ptHeadNode; 	 	while(ptTailNode->pRightNode) 	{ 		ptTailNode = ptTailNode->pRightNode; 	} 		 	ptTailNode->pLeftNode->pRightNode = NULL;	 	 	printf("你要删除的尾节点为%d\n",ptTailNode->iValue); 	 	free(ptTailNode); 	ptTailNode = NULL; 	 	return; }  /*删除头部*/ void func_headDelete() { 	int iValue = 0; 	int iRet = func_isEmptyCheck(); 	if(-1 == iRet) 	{ 		printf("双向链表是空的\n"); 		return; 	} 	 	TNode *ptNextNode = g_ptHeadNode->pRightNode; 	 	ptNextNode->pLeftNode = NULL; 		 	printf("你要删除的头结点为%d\n",g_ptHeadNode->iValue); 	 	free(g_ptHeadNode); 	g_ptHeadNode = NULL; 	 	g_ptHeadNode = ptNextNode; 	 	return; }  /*显示打印*/ void func_showAll() { 	int iValue = 0; 	int iRet = func_isEmptyCheck(); 	if(-1 == iRet) 	{ 		printf("双向链表是空的\n"); 		return; 	}	 	 	TNode *ptNode = g_ptHeadNode; 	 	while(ptNode) 	{ 		printf("%d<=>",ptNode->iValue); 		ptNode = ptNode->pRightNode; 		if(ptNode == g_ptHeadNode) 		{ 			break; 		} 	} 	printf("\n"); 	 	return; }  /*删除*/ void func_allDelete() { 	int iValue = 0; 	int iRet = func_isEmptyCheck(); 	if(-1 == iRet) 	{ 		printf("双向链表是空的\n"); 		return; 	}	 	 	TNode *ptNode = g_ptHeadNode; 	TNode *ptRightNode = NULL; 	 	while(ptNode) 	{ 		ptRightNode = ptNode->pRightNode; 		free(ptNode); 		ptNode=NULL; 		 		ptNode = ptRightNode; 	} 	 	g_ptHeadNode = NULL; 	 	return; }  int main() { 	int iFlag = 0;  	while(1) 	{ 		printf("操作选项:\n\t1:尾部增加;\t2:头部增加;\n\t3:尾部删除;\t4:头部删除;\n\t5:输出全部;\t6:全部删除;\n\t0:退出\n"); 		printf("请输入你的选择:"); 		scanf("%d",&iFlag); 		switch(iFlag) 		{ 			case 0: 				break; 				 			case 1: 				func_tailInsert(); 				break; 				 			case 2: 				func_headInsert(); 				break; 				 			case 3: 				func_tailDelete(); 				break; 					 			case 4: 				func_headDelete(); 				break; 					 			case 5: 				func_showAll(); 				break; 					 			case 6: 				func_allDelete(); 				break; 					 			default:				 				break; 		} 		 		if(0 == iFlag) 		{ 			break; 		} 		if(5 != iFlag) 		{ 			func_showAll(); 		} 	} 	 	func_allDelete(); 	 	return 0; }  谢谢，我一会仔细看一看…

user: user-1
content: 双向链表属于基本数据结构，网上资料太多了。下面是我写的，仅供参考。   #include <stdio.h> #include <stdlib.h>  typedef struct T_Node { 	int iValue; 	struct T_Node *pLeftNode; 	struct T_Node *pRightNode; }TNode;  static TNode *g_ptHeadNode = NULL;  TNode *func_create(int iValue) { 	TNode *ptNode = (TNode*)malloc(sizeof(TNode)); 	if(NULL == ptNode) 	{ 		return NULL; 	} 	 	ptNode->pLeftNode = NULL; 	ptNode->pRightNode = NULL;	 	ptNode->iValue = iValue; 	 	return ptNode; }  int func_isEmptyCheck() { 	if(NULL == g_ptHeadNode) 	{ 		return -1; 	} 	 	return 0; }  /*插入尾部*/ void func_tailInsert() { 	int iValue = 0; 	int iRet = func_isEmptyCheck(); 	if(-1 == iRet) 	{ 		printf("请输入头结点的数值:"); 		scanf("%d",&iValue);		 		g_ptHeadNode = func_create(iValue); 		 		return; 	} 	 	printf("请输入要添加的尾节点的数值:"); 	scanf("%d",&iValue); 	TNode *ptNode = func_create(iValue); 	 	TNode *ptTailNode = g_ptHeadNode; 	 	while(ptTailNode->pRightNode) 	{ 		ptTailNode = ptTailNode->pRightNode; 	} 	 	ptTailNode->pRightNode = ptNode; 	 	ptNode->pLeftNode = ptTailNode; 	ptNode->pRightNode = NULL; 	 	return; }  /*插入头部*/ void func_headInsert() { 	int iValue = 0; 	int iRet = func_isEmptyCheck(); 	if(-1 == iRet) 	{ 		printf("请输入头结点的数值:"); 		scanf("%d",&iValue);		 		g_ptHeadNode = func_create(iValue); 		 		return; 	} 	 	printf("请输入要添加的头节点的数值:"); 	scanf("%d",&iValue); 	TNode *ptNode = func_create(iValue); 	 	TNode *ptRightNode = g_ptHeadNode->pRightNode; 	 	g_ptHeadNode->pLeftNode = ptNode;	 	ptNode->pRightNode = g_ptHeadNode; 	 	g_ptHeadNode = ptNode; 	 	return; }  /*删除尾部*/ void func_tailDelete() { 	int iValue = 0; 	int iRet = func_isEmptyCheck(); 	if(-1 == iRet) 	{ 		printf("双向链表是空的\n"); 		return; 	}	 	 	TNode *ptTailNode = g_ptHeadNode; 	 	while(ptTailNode->pRightNode) 	{ 		ptTailNode = ptTailNode->pRightNode; 	} 		 	ptTailNode->pLeftNode->pRightNode = NULL;	 	 	printf("你要删除的尾节点为%d\n",ptTailNode->iValue); 	 	free(ptTailNode); 	ptTailNode = NULL; 	 	return; }  /*删除头部*/ void func_headDelete() { 	int iValue = 0; 	int iRet = func_isEmptyCheck(); 	if(-1 == iRet) 	{ 		printf("双向链表是空的\n"); 		return; 	} 	 	TNode *ptNextNode = g_ptHeadNode->pRightNode; 	 	ptNextNode->pLeftNode = NULL; 		 	printf("你要删除的头结点为%d\n",g_ptHeadNode->iValue); 	 	free(g_ptHeadNode); 	g_ptHeadNode = NULL; 	 	g_ptHeadNode = ptNextNode; 	 	return; }  /*显示打印*/ void func_showAll() { 	int iValue = 0; 	int iRet = func_isEmptyCheck(); 	if(-1 == iRet) 	{ 		printf("双向链表是空的\n"); 		return; 	}	 	 	TNode *ptNode = g_ptHeadNode; 	 	while(ptNode) 	{ 		printf("%d<=>",ptNode->iValue); 		ptNode = ptNode->pRightNode; 		if(ptNode == g_ptHeadNode) 		{ 			break; 		} 	} 	printf("\n"); 	 	return; }  /*删除*/ void func_allDelete() { 	int iValue = 0; 	int iRet = func_isEmptyCheck(); 	if(-1 == iRet) 	{ 		printf("双向链表是空的\n"); 		return; 	}	 	 	TNode *ptNode = g_ptHeadNode; 	TNode *ptRightNode = NULL; 	 	while(ptNode) 	{ 		ptRightNode = ptNode->pRightNode; 		free(ptNode); 		ptNode=NULL; 		 		ptNode = ptRightNode; 	} 	 	g_ptHeadNode = NULL; 	 	return; }  int main() { 	int iFlag = 0;  	while(1) 	{ 		printf("操作选项:\n\t1:尾部增加;\t2:头部增加;\n\t3:尾部删除;\t4:头部删除;\n\t5:输出全部;\t6:全部删除;\n\t0:退出\n"); 		printf("请输入你的选择:"); 		scanf("%d",&iFlag); 		switch(iFlag) 		{ 			case 0: 				break; 				 			case 1: 				func_tailInsert(); 				break; 				 			case 2: 				func_headInsert(); 				break; 				 			case 3: 				func_tailDelete(); 				break; 					 			case 4: 				func_headDelete(); 				break; 					 			case 5: 				func_showAll(); 				break; 					 			case 6: 				func_allDelete(); 				break; 					 			default:				 				break; 		} 		 		if(0 == iFlag) 		{ 			break; 		} 		if(5 != iFlag) 		{ 			func_showAll(); 		} 	} 	 	func_allDelete(); 	 	return 0; }