带头结点双向链表的内存分布情况
头文件
#pragma once
//双向链表
typedef struct DNode
{int data;DNode* next;DNode* prio;
}DNode , *DList ;//初始化
void InitList(DList plist);//头插法
bool Insert_head(DList plist,int val);//尾插法
bool Insert_tail(DList plist,int val);//在pos下标插入数据val
bool Insert_pos(DList plist,int pos,int val);//查找,找到返回节点地址,没有找到返回NULL
DNode *Search(DList plist,int key);//删除第一个key对应的节点
bool Delete(DList plist,int key);//删除第一个数据节点,并通过rtval获得删除的值
bool Delete_head(DList plist,int *rtval);//删除最后一个数据节点,并通过rtval获得删除的值
bool Delete_tail(DList plist,int *rtval);//获取长度,统计数据节点的个数
int GetLength(DList plist);//判空
bool IsEmpty(DList plist);//清除所以数据
void Clear(DList plist);//销毁所有节点
void Destroy(DList plist);//打印
void Show(DList plist);//反转
void Reverse(DList plist);
cpp文件
#include<stdlib.h>
#include<assert.h>
#include"dlist.h"
#include<stdio.h>
//初始化
void InitList(DList plist)
{assert(plist != NULL);if(plist == NULL){return ;}plist->next = NULL;plist->prio = NULL;
}
static DNode *BuyNode(int val)
{DNode* p = (DNode*) malloc (sizeof(DNode));p->data = val;return p;
}//头插法
bool Insert_head(DList plist,int val)
{DNode * p = BuyNode(val);assert(plist != NULL);if(plist == NULL){return false;}if(plist->next != NULL){plist->next->prio = p;}p->prio = plist;p->next = plist ->next;plist->next = p;return true;
}//尾插法
bool Insert_tail(DList plist,int val)
{DNode * p = BuyNode(val);assert(plist != NULL);if(plist == NULL){return false;}DNode *q;for(q = plist;q->next!= NULL;q = q->next);//将p 插在q 的后面q->next =p;p->prio =q;p->next = NULL;return true;
}//在pos下标插入数据val
bool Insert_pos(DList plist,int pos,int val)
{DNode * p = BuyNode(val);assert(plist != NULL);if(plist == NULL){return false;}DNode *q;int i = 0;for( q =plist->next; q!= NULL && i<pos;q = q->next);if(i < 0){return false;}p->next = q->next;p->prio = q;q->next = p;return true;
}//查找,找到返回节点地址,没有找到返回NULL
DNode *Search(DList plist,int key)
{assert(plist != NULL);if(plist == NULL){return false;}for(DNode *p = plist->next;p != NULL;p = p->next){if(p->data == key){return p;}}return NULL;}//删除第一个key对应的节点
bool Delete(DList plist,int key)
{assert(plist != NULL);if(plist == NULL){return false;}DNode * p =Search(plist,key);if(p == NULL){return false;}p->prio->next = p->next;if(p->next != NULL){p->next->prio = p->prio;}free(p);return true;}//删除第一个数据节点,并通过rtval获得删除的值
bool Delete_head(DList plist,int *rtval)
{assert(plist != NULL);if(plist != NULL || plist->next == NULL){return false;}if(rtval != NULL){*rtval = plist->data;}DNode *p = plist->next;plist->next = p->next;p->next ->prio = plist;free(p);return true;
}//删除最后一个数据节点,并通过rtval获得删除的值
bool Delete_tail(DList plist,int *rtval)
{assert(plist != NULL);if(plist == NULL || plist->next == NULL){return false;}DNode *p = plist ;DNode *q;for(q = plist; q->next != NULL;q = q->next);q->next = p;if(rtval != NULL){*rtval = p->data;}q->next = NULL;free(p);return true;
}//获取长度,统计数据节点的个数
int GetLength(DList plist)
{int length = 0;for(DNode *p=plist->next;p!=NULL;p=p->next){length++;}return length;
}//判空
bool IsEmpty(DList plist)
{return plist->next == NULL;
}//清除所有数据
void Clear(DList plist)
{Destroy(plist);
}//销毁所有节点
void Destroy(DList plist)
{DNode *p;while(plist->next != NULL){p = plist->next;plist->next = p->next;free(p);}
}//打印
void Show(DList plist)
{for(DNode *p=plist->next;p!=NULL;p=p->next){printf("%d ",p->data);}printf("\n");
}//反转
void Reverse(DList plist)
{DNode * q;DNode * p = plist->next;while(p != NULL){q = p;p = p->next;q->next = plist ->next;plist ->next = p;}}
主函数
#include<stdio.h>
#include<stdlib.h>
#include"dlist.h"
int main()
{DNode dlist;InitList(&dlist);for(int i=0;i<10;i++){Insert_head(&dlist,i);}Show(&dlist);Reverse(&dlist);Show(&dlist);return 0;
}