双链表的插入删除操作
来源:互联网 发布:淘宝上的谭光树蜂蜜 编辑:程序博客网 时间:2024/05/20 18:44
源码如下:
#include<iostream>using namespace std;typedef struct tnode{int data;struct tnode *next;struct tnode *pre;}dnode;dnode *create(){dnode *head,*p,*s;int x;int circle=1;head=(dnode*)malloc(sizeof(dnode));p=head;while(circle){printf("\nplease input the data:");scanf("%d",&x);if(x!=0){s=(dnode*)malloc(sizeof(dnode));s->data=x;printf("\n%d",s->data);p->next=s;s->pre=p;p=s;}else{circle=0;}}head=head->next;head->pre=NULL;p->next=NULL;printf("\n%d",head->data);return(head);}void print(dnode *head){dnode *p;p=head;while(p!=NULL){printf("\n%d",p->data);p=p->next;}}dnode *del(dnode *head,int num){dnode *p1,*p2;p1=head;while(num!=p1->data&&p1->next!=NULL){p1=p1->next;}if(num==p1->data){if(p1==head){head=head->next;head->pre=NULL;free(p1);}else if(p1->next==NULL){p1->pre->next=NULL;free(p1);}else{p1->next->pre=p1->pre;p1->pre->next=p1->next;}}elseprintf("\n%d could not been found",num);return(head);}dnode *insert(dnode *head,int num,int m){dnode *p0,*p1,*p2;p1=head;p2=head;p0=(dnode*)malloc(sizeof(dnode));p0->data=num;int i;for(i=0;i<m;i++){p1=p1->next;}for(i=0;i<m-1;i++){p2=p2->next;}p2->next=p0;p0->pre=p2;p0->next=p1;p1->pre=p0;return(head);}int main(){dnode *head;int n,del_num,insert_num,x;head=create();print(head);cout<<"\nplease input the delete data:";cin>>del_num;head=del(head,del_num);print(head);cout<<"\nplease input the insert data:";cin>>insert_num;cout<<"\nplease input the position:";cin>>x;head=insert(head,insert_num,x);print(head);return 0;}
运行结果:
- 双链表的插入删除操作
- 小顶堆的插入删除操作
- AVL的插入,删除操作
- 双链表的各种操作 创建 插入 删除 查找
- 双链表的实现以及插入删除等操作
- 双链表的插入 删除
- 双链表的插入删除
- 单链表的创建、插入、删除、倒置操作
- 单链表的创建、插入、删除、倒置操作
- 支持插入删除操作的bitset
- Cassandra 读/插入/删除操作的实现
- B_树的插入、删除操作
- 链表的创建、插入、删除操作
- B树的插入、删除操作
- NHibernate的插入、删除、修改操作
- 单链表的创建、插入、删除等操作
- B_树的插入、删除操作
- 单链表的初始化,插入删除等操作
- iOS6的framework运行在iOS5或者以下版本机器上报错的解决办法
- Google Maps API编程资源大全
- Ubuntu 12.04中文输入法的安装
- [转]Android xmlns 的作用及其自定义
- 使用ASP.NET上传图片汇总
- 双链表的插入删除操作
- LA_3602DNA Consensus String ( hash, 貪心 )
- Codeforces Beta Round #77 (Div. 2 Only)——A,B,C
- C# using关键字
- IOS 之Google Analytics SDK预研
- 设计 布局管理+封装
- python 字符串模板用法string.Template
- Linux下好用的命令行拍照程序camshot
- 小波稀疏化