- C/C++ code
-
/*sgx 2008-10-30 c语言 双向链表*/#include <stdio.h>#include <assert.h>#include <malloc.h>#define TRUE 1;#define FALSE 0;typedef int ELEMTYPE;typedef struct DoubleLinkNode{ ELEMTYPE data; struct DoubleLinkNode *last; struct DoubleLinkNode *next; }dLinkNode,*dLinkList;dLinkList CreateDLlist(void);/*创建空链*/int InitDLlist(dLinkList * dl);/*初始化链*/int InsertLNode(dLinkNode *pNode,ELEMTYPE e);/*节点前插入节点,返回TRUE,FALSE*/int InsertNNode(dLinkNode *pNode,ELEMTYPE e);/*在某节点后面插入节点,*/int DeleteNode(dLinkNode *pNode);/*删除节点,返回TRUE换或FALSE*/void DestroyDLlist(dLinkList dl);/*销毁链*/ void LNTravel(dLinkList dl);/*从前向后遍历*/void NLTravel(dLinkList dl);/*从后向前遍历*/int main(){ dLinkList dl=CreateDLlist(); if(InitDLlist(&dl))printf("Initial sucess!\n"); LNTravel(dl); NLTravel(dl); DestroyDLlist(dl); return 0;}dLinkList CreateDLlist(void)/*创建空链*/{ dLinkList dl=NULL; return dl;}int InitDLlist(dLinkList *dl)/*初始化链*/{ ELEMTYPE e; char symbol; dLinkNode *pNode; if(*dl!=NULL) {printf("this LinkList has been Initialed.\n");return FALSE;} printf("input data:"); scanf("%d",&e); getchar();/*获取空格*/ *dl=(dLinkNode*)malloc(sizeof(dLinkNode)); if(dl==NULL){printf("assigned memory failed,end Initailization!\n"); return FALSE;} (*dl)->data=e; (*dl)->next=NULL; (*dl)->last=NULL; pNode=*dl; printf("continue?y/n "); scanf("%c",&symbol); getchar(); while(symbol=='y' || symbol=='Y') { printf("input data:"); scanf("%d",&e); getchar(); InsertNNode(pNode,e); pNode=pNode->next; printf("continue?y/n "); scanf("%c",&symbol); getchar(); } return TRUE; }int InsertLNode(dLinkNode *pNode,ELEMTYPE e)/*节点前面插入节点,返回TRUE,FALSE*/{ dLinkNode *newNode,*lastNode; if(pNode==NULL) { printf("Node is NULL,canot operate!\n"); return FALSE; } newNode = (dLinkNode*)malloc(sizeof(dLinkNode)); if(!newNode){printf("assigned memory failed!\n");return FALSE;}/*分配失败*/ newNode->data=e;/*插入数据*/ if(pNode->last==NULL) { newNode->last=NULL; } else { lastNode = pNode->last; lastNode->next = newNode; newNode->last = lastNode; } newNode->next=pNode; pNode->last=newNode; return TRUE; }int InsertNNode(dLinkNode *pNode,ELEMTYPE e)/*在某节点后面插入节点,*/{ dLinkNode *newNode,*nextNode; if(pNode==NULL) { printf("Node is NULL,canot operate!\n"); return FALSE; } newNode = (dLinkNode*)malloc(sizeof(dLinkNode)); if(!newNode){printf("assigned memory failed!\n");return FALSE;}/*分配失败*/ newNode->data = e;/*插入数据*/ if(pNode->next==NULL) { newNode->next=NULL; } else { nextNode=pNode->next; newNode->next = nextNode; nextNode->last=newNode; } pNode->next=newNode; newNode->last=pNode; return TRUE;}int DeleteNode(dLinkNode *pNode)/*删除节点,返回FALSE或TRUE*/{ ELEMTYPE e; dLinkNode *LastNode,*NextNode; if(pNode==NULL){printf("Node is NULL,cannot operate it!\n");return FALSE;} LastNode=pNode->last; NextNode = pNode->next; e=pNode->data; if(pNode->next ==NULL && NULL == pNode->last) { free(pNode); printf("%d is deleted,this LinkList now is NULL!\n",e); return TRUE; } else if(pNode->next == NULL) { LastNode->next = NULL; } else if(pNode->last == NULL) { NextNode->last=NULL; } else { LastNode->next=NextNode; NextNode->last=LastNode; } free(pNode); printf("%d is deleted.\n",e); return TRUE;}void DestroyDLlist(dLinkList dl)/*销毁链*/{ dLinkNode *pNode=dl; dLinkNode *temp; if(pNode==NULL) {printf("the LinkList has been already destroyed!\n");return;} while(pNode->next != NULL) { temp=pNode; pNode=temp->next; DeleteNode(temp); } DeleteNode(pNode);}void LNTravel(dLinkList dl)/*从前向后遍历*/{ dLinkList pNode; if(dl==NULL){printf("LinkList is NULL!\n");return;} pNode=dl; printf("Travel this LinkList InOrder: "); do { printf("%d\t",pNode->data); pNode=pNode->next; }while(pNode!=NULL); printf("\n");}void NLTravel(dLinkList dl)/*从后向前遍历*/{ dLinkList pNode; if(dl==NULL){printf("LinkList is NULL!\n");return;} pNode=dl; printf("Travel this LinkList PostOrder: "); while(pNode->next!=NULL)pNode=pNode->next; do { printf("%d\t",pNode->data); pNode=pNode->last; }while(pNode!=NULL); printf("\n");}
-
摘自: http://topic.csdn.net/u/20081030/15/250b5b04-319b-45fe-a8a7-b079b6380743.html
原文链接: https://www.cnblogs.com/djcsch2001/archive/2011/05/16/2048097.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/25579
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!