#include <stdio.h> #include <stdlib.h> #include "doubly.h" 不带头双向链表 typedef struct DoublyNode { DoublyNode*next; DoublyNode*prior; int data; }DoublyNode;
头插创建: 用指针first保存每次新插入的节点 DoublyNode*HeadCreate() { DoublyNode*first = (DoublyNode*)malloc(sizeof(DoublyNode)); first->next= NULL; first->prior = NULL; first->data = 0; int flag =1; for(int i=1;i<10;i++) { DoublyNode *p = (DoublyNode*)malloc(sizeof(DoublyNode)); p->data = i; p->next = NULL; p->prior = NULL; if (flag) { first = p; flag = 0; } else { p->next = first; first->prior = p; first = p; } } return first; } 尾插创建: 指针last保存每次新插入的节点,指针head保存第一个节点 DoublyNode*TailCreate() { DoublyNode*last = (DoublyNode*)malloc(sizeof(DoublyNode)); DoublyNode*head = (DoublyNode*)malloc(sizeof(DoublyNode)); last->next= NULL; last->prior = NULL; last->data = 0; int flag =1; for (int i=1;i<10;i++) { DoublyNode *p = (DoublyNode*)malloc(sizeof(DoublyNode)); p->data = i; p->next = NULL; p->prior = NULL; if (flag){ last = p; head=p; flag=0; }else{ last->next = p; p->prior = last; last = p; } } return head; } 打印 void PrintDoublyNode(DoublyNode*h) { if (h==NULL){ return; } DoublyNode*p=h; while(p!=NULL){ printf("%d ",p->data ); p=p->next; } printf("\n"); } 长度 int GetLenth(DoublyNode*h) { if (h==NULL){ return 0; } DoublyNode*p=h; int count = 0; while(p!=NULL){ count++; p=p->next; } return count; } 查找节点值为elem DoublyNode* SerachNode(DoublyNode*h,int elem) { if (h==NULL){ return NULL; } DoublyNode*p = h; while(p!=NULL) { if (p->data ==elem) { return p; } p = p->next; } return NULL; } 前插:在值等于elem的节点之前插入新节点的值为val 注意在第一个元素之前插要把头指向新插入的第一个 DoublyNode* InsertNode(DoublyNode*h,int elem ,int val) { if (h==NULL){ return NULL; } DoublyNode*p=h; while(p!=NULL) { if (p->data==elem) { DoublyNode *q = (DoublyNode*)malloc(sizeof(DoublyNode)); q->data = val; if (p->prior==NULL) { q->next =h; h->prior = q; h=q; }else{ p->prior->next=q; q->next = p; q->prior = p->prior; p->prior=q; } return h; } p=p->next; } return h; } void InsertNodeNoReturn(DoublyNode**h,int elem ,int val) { if (h==NULL||*h==NULL){ return ; } DoublyNode*p=*h; while(p!=NULL) { if (p->data==elem) { DoublyNode *q = (DoublyNode*)malloc(sizeof(DoublyNode)); q->data = val; if (p->prior==NULL) { q->next =*h; (*h)->prior = q; *h=q; }else{ p->prior->next=q; q->next = p; q->prior = p->prior; p->prior=q; } return ; } p=p->next; } } 删除指定节点 注意删除第一个节点和最后一个节点 DoublyNode* DeleteNode(DoublyNode*h,int elem){ if (h==NULL) { return NULL; } DoublyNode *p = h; while(p != NULL) { if (p->data == elem) { DoublyNode *q = p; if (p->prior != NULL) { p->prior->next = p->next; } else { // h = p->next; //p->next = NULL;// 不写会出错 p = p->next; h=p; } if (p->next != NULL){ p->next->prior = p->prior; } free(q); return h; } p = p->next; } return h; } 释放 void DestroyList(DoublyNode **h){ if (h==NULL||*h==NULL) { return ; } DoublyNode *p = *h; *h=NULL;//不置为空 就是野指针 while(p!=NULL) { DoublyNode *q = p; p=p->next; free(q); } } 逆置 h一直指向头插的新节点 q是要插入的节点 DoublyNode*Reverse(DoublyNode*h) { if (h==NULL||h->next==NULL) { return h; } DoublyNode*p=h; DoublyNode*q =p->next; while(q!=NULL) { p->next = q->next; if (q->next != NULL) { q->next->prior = p; } q->next = h; h->prior = q; h = q; q=p->next; } h->prior =NULL; return h; }