双链表的插入 删除
来源:互联网 发布:mac版ps cs5序列号 编辑:程序博客网 时间:2024/06/08 05:54
双链表其实 也没什么 只是多了一个前置链而已
双链表的定义
struct DNode{int data;struct DNode *next;struct DNode *pre;};
单链表的定义
struct DNode{int data;struct DNode *next;};
其他的可以看上一篇博客 大致相同
#ifndef HEAD_H#define HEAD_H#include <iostream>using namespace std;#include <cassert>#include <cstdlib>#include <cmath>#include <sstream>#include <fstream>#include <string>#include <algorithm>#include <list>#include <queue>#include <vector>#include <deque>#include <stack>#include <bitset>#include <set>#include <map>#endifstruct DNode{int data;struct DNode *next;struct DNode *pre;};DNode *Creat(){DNode *head,*p,*s;head=(DNode *)malloc(sizeof(DNode));p=head;int temp;while (cin>>temp&&temp){s=(DNode *)malloc(sizeof(DNode));s->data=temp;p->next=s;s->pre=p;p=s;}head=head->next;p->next=NULL;head->pre=NULL;return (head);}DNode *Insert(DNode *&head,int num){DNode *p,*s;s=(DNode *)malloc(sizeof(DNode));s->data=num;p=head;while (NULL!=p->next&&num>p->data){p=p->next;}if (num<=p->data){if (NULL==p->pre){s->next=head;head->pre=s;head=s;head->pre=NULL;} else{s->pre=p->pre;p->pre->next=s;s->next=p;p->pre=s;}} else{p->next=s;s->pre=p;s->next=NULL;}return(head);}DNode *Del(DNode *&head,int num){DNode *p;p=head;while (NULL!=p->next&&num!=p->data){p=p->next;}if (num==p->data){if (NULL==p->pre){head=p->next;p->next->pre=head;free(p);} else if (NULL==p->next){p->pre->next=NULL;free(p);}else{p->pre->next=p->next;p->next->pre=p->pre;free(p);}} else{cout<<num<<" cound not be found"<<endl;}return head;}void Display(DNode *head){DNode *p;p=head;while (NULL!=p){cout<<(p->data)<<" ";p=p->next;}cout<<endl;}
#include "head.h"int main(){DNode *head;head=Creat();Display(head);#ifndef DEBUGcout<<"please input an num to insert:";#endifint insert_num;while (cin>>insert_num&&insert_num){Insert(head,insert_num);Display(head);}#ifndef DEBUGcout<<"please input an number to delete:";#endif int delete_num; while (cin>>delete_num&&delete_num) {Del(head,delete_num);Display(head); }return (EXIT_SUCCESS);}
- 双链表的插入 删除
- 双链表的插入删除
- 双链表的创建、插入、删除
- 双链表的创建、插入、删除
- 双链表的插入与删除
- 双链表的插入删除操作
- 双链表的创建、删除、插入
- 双链表的初始化,建立,插入,查找,删除
- 双链表的初始化,建立,插入,查找,删除
- 双链表的初始化,建立,插入,查找,删除
- 双链表的初始化,建立,插入,查找,删除。
- 双链表的建立、查找、删除、插入
- 数据结构之 双链表的创建,删除,插入
- 【数据结构】双链表的建立,排序,插入,删除
- 双链表的插入和删除实现
- 循环双链表的删除、插入、显示
- 编程实现双链表的建立、插入、删除
- 双链表的建立,删除及插入
- IIS下PHP的ISAPI和FastCGI比较
- 将图片以二进制代码形式存入数据库
- Oracle EBS R12 - Clone EBS R12.1.1 on Oracle Linux 64 5.7 to Oracle Linux 64 5.7
- excel 使用学习
- 网站资源博客
- 双链表的插入 删除
- ASP.NET中利用DataList实现图片无缝滚动
- 设计模式(16)-行为型-迭代器模式(Iterator)
- 设计模式(17)-行为型-中介者模式(Mediator)
- 过去的得失,别太在意。
- Windows Presentation Foundation Introduction(II) - User Interfaces Part I
- 享受编程
- 数组中出现次数超过一半的数字
- C++第13周项目1——Fibnacci序列