双向链表插入删除基本应用介绍

所属分类: 软件编程 / C 语言 阅读数: 143
收藏 0 赞 0 分享
双链表其实 也没什么 只是多了一个前置链而已
双链表的定义
复制代码 代码如下:

struct DNode
{
int data;
struct DNode *next;
struct DNode *pre;
};

单链表的定义
复制代码 代码如下:

view plaincopy
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>
#endif
struct 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 DEBUG
cout<<"please input an num to insert:";
#endif
int insert_num;
while (cin>>insert_num&&insert_num)
{
Insert(head,insert_num);
Display(head);
}
#ifndef DEBUG
cout<<"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);
}
更多精彩内容其他人还在看

深入解析C++中派生类的构造函数

这篇文章主要介绍了深入解析C++中派生类的构造函数,是C++入门学习中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

解析C++中多层派生时的构造函数及一些特殊形式

这篇文章主要介绍了解析C++中多层派生时的构造函数及一些特殊形式,特殊形式主要针对基类和子对象类型的构造函数内容,需要的朋友可以参考下
收藏 0 赞 0 分享

简单介绍C++编程中派生类的析构函数

这篇文章主要介绍了C++编程中派生类的析构函数,析构函数平时一般使用较少,需要的朋友可以参考下
收藏 0 赞 0 分享

深入解析C++中类的多重继承

这篇文章主要介绍了深入解析C++中类的多重继承,包括多重继承相关的二义性问题,需要的朋友可以参考下
收藏 0 赞 0 分享

详解C++中基类与派生类的转换以及虚基类

这篇文章主要介绍了详解C++中基类与派生类的转换以及虚基类,是C++入门学习中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

解析C++编程中的继承方面的运用

这篇文章主要介绍了解析C++编程中的继承方面的运用,是C++入门学习中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

详解C++编程的多态性概念

这篇文章主要介绍了C++编程的多态性概念,是C++入门学习中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

c++统计文件中字符个数代码汇总

本文给大家汇总介绍了3种使用C++实现统计文件中的字符个数的方法,非常的简单实用,有需要的小伙伴可以参考下。
收藏 0 赞 0 分享

详解C++编程中的虚函数

这篇文章主要介绍了详解C++编程中的虚函数,包括在什么情况下应当声明虚函数的相关讲解,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言putenv()函数和getenv()函数的使用详解

这篇文章主要介绍了C语言putenv()函数和getenv()函数的使用详解,用来进行环境变量的相关操作,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多