双链表基本操作
来源:互联网 发布:淘宝店女装店招图片 编辑:程序博客网 时间:2024/05/22 00:00
看欧立奇的《程序员面试宝典》的双向链表部分,发现其中创建双向链表和删除链表中得某一点的程序存在问题,现将已经调试通过的程序贴在下面:
#include <iostream>using namespace std;#define LEN sizeof(DNode)typedef struct DoubleNode{ int data; struct DoubleNode *next; struct DoubleNode *pre;}DNode;DNode *create(){ DNode *head,*p_new, *p_old; int cycle=1; int n=0; int data; head = (DNode*)malloc(sizeof(DNode)); p_old = head; while(cycle) { cout<<"Please input the num"<<endl; cin>>data; if(data != 0) { n=n+1; if (n==1) { p_old->data=data; p_old->next=NULL; p_old->pre=NULL; } else { p_new = (DNode*)malloc(sizeof(DNode)); p_new->data =data; p_old->next = p_new; p_new->pre = p_old; p_old = p_new; } } else { cycle = 0; } } p_old->next = NULL; head ->pre = NULL; return head;}DNode *del(DNode *head,int num){ DNode *p; p = (DNode*)malloc(sizeof(DNode)); p=head; while(num != p->data && p->next !=NULL) { p =p->next; } if(num == p->data) { if(p == head) { free(head); p = p->next; head = p; } else if(p->next ==NULL) { p->pre->next=NULL; free(p); } else { p->pre->next = p->next; p->next->pre =p->pre; free(p); } } else { cout<<"cann't find the num"<<endl; } return head;} DNode *insert(DNode *head,int num){ DNode *p,*p_new; p=(DNode*)malloc(LEN); p_new=(DNode*)malloc(LEN); p_new->data = num; p=head; while(num > p->data && p->next !=NULL) { p = p->next; } if(num <= p->data) { if(head == p) { p_new->next =p; p->pre = p_new; head = p_new; head ->pre = NULL; } else { p->pre->next = p_new; p_new->next = p; p->pre = p_new; p_new->pre = p->pre; } } else { p->next = p_new; p_new->pre = p; p_new->next = NULL; } return head;}void main(){ DNode *head; head = (DNode*)malloc(LEN); head =create(); int num; num = head->next->next->pre->data; //int num1=17; //head=del(head,17); //int num2=head->next->data; head = insert(head ,21); int num3=head->next->next->next->next->data;}
- 双链表基本操作
- 双链表基本操作
- 双链表基本操作
- 双链表基本操作
- 双链表基本操作。
- 双链表的基本操作
- 双链表基本操作
- 双链表基本操作
- 双链表基本操作
- 双链表的基本操作
- 【中级】双链表基本操作
- 双链表基本操作
- 双链表的基本操作
- 双链表基本操作
- 双链表的基本操作
- c++ 实现双链表基本操作
- 双链表基本操作C++实现
- C++ 双链表的基本操作
- 腾讯 你能为世界做点什么
- jackyrongvip
- lucene介绍及入门实例
- pdf里面打字
- [翻译]使用asp.net 2.0中的SqlBulkCopy类批量复制数据
- 双链表基本操作
- 【分享】针对Windows的权威文件编辑器EmEditor v10.1.1 [附插件+注册码]
- iPhone目录结构
- 纠结~!
- 走进REDUI —— 架构预览
- SqlBulkCopy类数据导入
- Application Bundle中读取文件
- UI测试-DPI
- Ubuntu中PHPmyAdmin无法访问故障一例