数据结构之双向链表
来源:互联网 发布:图片识别手写数字算法 编辑:程序博客网 时间:2024/05/22 11:39
双向链表(以带头节点的双向链表)的结构如下:
由数据域,指向后继的指针,以及纸箱前驱的指针构成。
1. 该双向链表的定义如下:
typedef struct DNode{ int data;//数据域 struct DNode *next;//指向后继 struct DNode *prio;//指向前驱}DNode ,*DList;
2. 双向链表的初始化
void InitList(DList plist){plist->next = NULL;plist->prio = NULL;}
3. 利用头插法插入
bool Insert_Head(DList plist,int val){DNode *p = (DNode *)malloc(sizeof(DNode));p->data = val;p->next = plist->next;//1plist->next = p;//2p->prio = plist;if(p->next != NULL)//特殊情况,p是第一次插入的节点{p->next->prio = p;}return true;}
4. 利用尾插法插入
bool Insert_Tail(DList plist,int val){DNode *p = (DNode *)malloc(sizeof(DNode));p->data = val;DNode *q;for(q=plist;q->next!=NULL;q=q->next) ;//将p插在q的后面q->next = p;p->prio = q;p->next = NULL;return true;}
5. 按关键字查找
DNode *Search(DList plist,int key){for(DNode *p=plist->next; p!=NULL; p=p->next){if(p->data == key){return p;}}return NULL;}
6. 按关键字删除
bool Delete(DList plist,int key){DNode *p = Search(plist,key);if(p == NULL){return false;}p->prio->next = p->next;if(p->next != NULL)//p为尾节点{p->next->prio = p->prio;}free(p);return true;}
7. 判空
bool IsEmpty(DList plist){if (plist->next==NULL && plist->prio==NULL){return true;}else{return false;}}
8. 求长
int GetLength(DList plist){int count = 0;for (DNode *p=plist->next; p!=NULL; p=p->next){count ++;}return count;}
9. 打印链表
void Show(DList plist){for (DNode *p=plist->next; p!=NULL; p=p->next){printf("%d ", p->data);}printf("\n");}
10. 清楚链表
void Clear(DList plist){Destroy(plist);}
11. 摧毁链表
void Destroy(DList plist){DNode *p;while(plist->next != NULL){p = plist->next;plist->next = p->next;free(p);}}
阅读全文
1 0
- 数据结构之双向链表
- 数据结构之双向链表
- 数据结构之双向链表
- 数据结构之双向链表
- 数据结构之双向链表
- 数据结构之双向链表
- 数据结构之双向链表
- 数据结构之双向链表
- 数据结构之双向链表
- 数据结构学习之双向链表
- 数据结构之双向链表的实现
- 数据结构学习之双向链表结构
- C语言数据结构之双向链表
- 数据结构之双向链表实现
- 数据结构之双向循环链表06
- 第十九节 数据结构之双向链表
- 数据结构之双向循环链表
- 企业级数据结构之-双向链表
- HDU 1045 Fire Net
- Caused by: java.net.SocketException: Software caused connection abort: socket write error
- Java知识:HashSet详解
- css居中,进度条
- css的各种居中
- 数据结构之双向链表
- STM32F429HAL库ADCDMA学习笔记
- Mac 下MAMP Pro MySQL无法启动报 Error: could not open single-table tablespace file ./mysql/innodb_table_sta
- 详解内部类
- 关于eclipse中部署到service中时显示部署名称不正确的解决办法
- Java 集合框架(转载)
- Photoshop-iPhone x适配
- 微信门店小程序接口文档
- C++面试必知必会(3)-程序改错