双向链表的创建和相关操作

http://blog.csdn.net/jw903/article/details/38947753

   双向链表其实是单链表的改进。 当我们对单链表进行操作时,有时你要对某个结点的直接前驱进行操作时,又必须从表头开始查找。这是由单链表结点的结构所限制的。因为单链表每个结点只有一个存储直接后继结点地址的链域,那么能不能定义一个既有存储直接后继结点地址的链域,又有存储直接前驱结点地址的链域的这样一个双链域结点结构呢?这就是双向链表。

   在双向链表中,结点除含有数据域外,还有两个链域,一个存储直接后继结点地址,一般称之为右链域;一个存储直接前驱结点地址,一般称之为左链域。

   在c语言中双向链表结点类型可以定义为:

[cpp] view plain copy
  1. typedef struct Node  
  2. {  
  3.     int item;  
  4.     struct Node *pre;  
  5.     struct Node *next;  
  6. }DListNode,*DList;  

下面的代码就是对双向链表的创建和相关操作:

[cpp] view plain copy
  1. /*************************************************************************** 
  2.  *name:jae chia                                                            * 
  3.  *date:2014.8.29                                                           * 
  4.  *version: 1.0                                                             * 
  5.  **************************************************************************/  
  6. #include<iostream>  
  7. #include<cassert>  
  8. using namespace std;  
  9.   
  10. //双向链表的建立与操作  
  11. typedef struct Node  
  12. {  
  13.     int item;  
  14.     struct Node *pre;  
  15.     struct Node *next;  
  16. }DListNode,*DList;  
  17. DList InsertNodeToTail(DList head,int data)//将节点插入到双向链表的尾部  
  18. {  
  19.     if(head==NULL)  
  20.     {  
  21.         head=(DList)malloc(sizeof(DListNode));  
  22.         assert(head!=NULL);  
  23.         head->item=data;  
  24.         head->next=NULL;  
  25.         head->pre=NULL;  
  26.     }  
  27.     else  
  28.     {  
  29.         DListNode *newnode=(DList)malloc(sizeof(DListNode));//创建新的链表节点  
  30.         assert(newnode!=NULL);  
  31.         newnode->item=data;  
  32.         newnode->next=NULL;  
  33.         newnode->pre=NULL;  
  34.   
  35.         DListNode *p=head;  
  36.         while(p->next!=NULL)  
  37.         {  
  38.             p=p->next;  
  39.         }  
  40.         p->next=newnode;  
  41.         newnode->pre=p;  
  42.     }  
  43.     return head;  
  44. }  
  45.   
  46. DList InsertDListByOrder(DList head,int data)//这里的插入操作是按序插入(保证双向链表中的节点以递增有序)  
  47. {  
  48.     DListNode *newnode=(DList)malloc(sizeof(DListNode));  
  49.     assert(newnode);  
  50.     newnode->item=data;  
  51.     //newnode->next=NULL;  
  52.     //newnode->pre=NULL;  
  53.   
  54.     DListNode *p=head;  
  55.     DListNode *prenode;  
  56.     for(;p!=NULL;p=p->next)  
  57.     {     
  58.         prenode=p;  
  59.         if(newnode->item <=p->item)  
  60.             break;       
  61.     }  
  62.     if(p==NULL)//如果遍历整个链表,结点的值都比要插入的小,那么只能在尾端插入  
  63.     {  
  64.         prenode->next=newnode;  
  65.         newnode->pre=prenode;  
  66.         newnode->next=NULL;  
  67.     }  
  68.     else if(p==head)//如果链表中的数都比要插入的数大则在头部插入;  
  69.     {  
  70.         newnode->pre=NULL;  
  71.         newnode->next=head;  
  72.         head=newnode;  
  73.     }  
  74.     else   //在中间插入  
  75.     {  
  76.         p->pre->next=newnode;  
  77.         newnode->pre=p->pre;  
  78.   
  79.         newnode->next=p;  
  80.         p->pre=newnode;  
  81.     }  
  82.     return head;  
  83. }  
  84.   
  85. bool FindNode(DList head,int data)//查找链表中含有某元素的节点是否存在  
  86. {  
  87.     if(head==NULL)  
  88.     {  
  89.         cout<<"the Dlist is NULL"<<endl;  
  90.         return false;  
  91.     }  
  92.     DListNode *p=head;  
  93.     while(p!=NULL)  
  94.     {  
  95.         if(p->item==data)  
  96.             return true;  
  97.         p=p->next;  
  98.     }  
  99.     return false;  
  100. }  
  101.   
  102. DList DeleteNode(DList head,int data)//删除节点  
  103. {  
  104.     DListNode *p=head;  
  105.     while(p!=NULL)  
  106.     {  
  107.         if(p->item==data)  
  108.             break;  
  109.         p=p->next;  
  110.           
  111.     }  
  112.     if(p==NULL)  
  113.     {  
  114.         cout<<"the node with data is not existed in the List"<<endl;  
  115.         exit(0);  
  116.     }  
  117.     if(p==head)//要删除的结点恰好是双向链表的头结点  
  118.     {  
  119.         DListNode *tmp=head;  
  120.         head=head->next;  
  121.         head->pre=NULL;//---------------------------------------------------  
  122.         free(tmp);  
  123.     }  
  124.     else if(p->next==NULL)//如果要删除的节点是链表的最后一个节点  
  125.     {  
  126.         p->pre->next=NULL;  
  127.         free(p);  
  128.     }  
  129.     else //删除中间节点  
  130.     {  
  131.         p->pre->next=p->next;  
  132.         p->next->pre=p->pre;  
  133.     }  
  134.     return head;    
  135. }  
  136. void PrintDList(DList head)//打印  
  137. {  
  138.     cout<<"现在,链表如下:"<<endl;  
  139.     if(head==NULL)  
  140.     {  
  141.         cout<<"the Dlist is NULL"<<endl;  
  142.         return ;  
  143.     }  
  144.     DListNode *p=head;  
  145.     while(p!=NULL)  
  146.     {  
  147.         cout<<p->item<<" ";  
  148.         p=p->next;  
  149.     }  
  150.     cout<<endl<<endl;  
  151. }  
  152. void DestroyDList(DList head)//销毁双向链表  
  153. {  
  154.     DListNode *p=head;  
  155.     while(p!=NULL)  
  156.     {  
  157.         DListNode *tmp=p;  
  158.         p=p->next;  
  159.         free(tmp);  
  160.     }  
  161. }  
  162.   
  163. void Test()  
  164. {  
  165.     DListNode *head=NULL;  
  166.   
  167.     for(int i=0;i<10;i++)   /*利用尾部插入来构造双向链表*/  
  168.         head=InsertNodeToTail(head,i);  
  169.     PrintDList(head);  
  170.       
  171.     int a;  
  172.     cout<<"输入要查找的结点的值"<<endl;  
  173.     cin>>a;  
  174.     if(FindNode(head,a))  
  175.         cout<<"结点存在!"<<endl<<endl;  
  176.     else  
  177.         cout<<"结点不存在!"<<endl<<endl;  
  178.   
  179.     cout<<"删除结点4..."<<endl;    /*删除指定节点*/  
  180.     head=DeleteNode(head,4);  
  181.     PrintDList(head);  
  182.   
  183.     cout<<"插入结点4..."<<endl;     /*按序插入*/  
  184.     head=InsertDListByOrder(head,4);  
  185.     PrintDList(head);  
  186.       
  187.     cout<<"删除头结点..."<<endl;    /*删除指定节点*/  
  188.     head=DeleteNode(head,0);  
  189.     PrintDList(head);  
  190.       
  191.     cout<<"删除尾结点..."<<endl;  
  192.     head=DeleteNode(head,9);  
  193.     PrintDList(head);  
  194.       
  195.     cout<<"插入头结点..."<<endl;     /*按序插入*/  
  196.     head=InsertDListByOrder(head,0);  
  197.     PrintDList(head);  
  198.       
  199.     cout<<"插入尾结点..."<<endl;     /*按序插入*/  
  200.     head=InsertDListByOrder(head,10);  
  201.     PrintDList(head);  
  202.   
  203.     DestroyDList(head);  
  204. }  
  205. int main(void)  
  206. {  
  207.     Test();  
  208. }  

运行:


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/384251.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

链表各类操作详解

http://blog.csdn.net/hackbuteer1/article/details/6591486/ 链表概述    链表是一种常见的重要的数据结构。它是动态地进行存储分配的一种结构。它可以根据需要开辟内存单元。链表有一个“头指针”变量&#xff0c;以head表示&#xff0c;它存放一个地址。该地址指向一个元…

信号和槽

信号槽是 Qt 框架引以为豪的机制之一。所谓信号槽&#xff0c;实际就是观察者模式。当某个事件发生之后&#xff0c;比如&#xff0c;按钮检测到自己被点击了一下&#xff0c;它就会发出一个信号&#xff08;signal&#xff09;。这种发出是没有目的的&#xff0c;类似广播。如…

登陆界面

界面展示&#xff1a; <!DOCTYPE html> <html><head><meta charset"utf-8"><title>电子邮件登录</title><link href"style.css" type"text/css" rel"stylesheet"></head><body>…

C语言实现双向链表删除、插入、双向输出

http://www.cnblogs.com/dyllove98/archive/2013/07/31/3228857.html #include<cstdio> #include<cstdlib> typedef struct DoubleLinkedList {int data;struct DoubleLinkedList *pre;struct DoubleLinkedList *next; }DlinkedList_Node; //建立链表 DlinkedList_…

servlet概述

一、什么是Servlet呢&#xff1f; servlet 是由sun公司提供的动态web资源开发技术&#xff0c;本质上就是一段Java程序&#xff0c;这段java程序无法独立运行&#xff0c;必须放在Servlet容器&#xff08;比如&#xff1a;tomcat服务器&#xff09;中运行&#xff0c;由容器调用…

运用递归将两个链表进行连接

http://blog.csdn.net/zjut_ym/article/details/45008259 建立2个数据项按从大到小排列的链表&#xff0c;实现2个链表的合并&#xff0c;并输出合并后链表的数据项。 函数代码如下 #include<iostream> using namespace std; struct node{int data;node *next; }; node …

C++ 类的深拷贝与浅拷贝||深拷贝通过重载拷贝构造函数与重载赋值运算符实现

http://blog.csdn.net/wangshihui512/article/details/9842225 在面向对象程序设计中&#xff0c;对象间的相互拷贝和赋值是经常进行的操作。 如果对象在申明的同时马上进行的初始化操作&#xff0c;则称之为拷贝运算。例如&#xff1a; class1 A("Time"); class1…

C++ 类的const成员函数

http://blog.csdn.net/wangshihui512/article/details/9823739 我们定义的类的成员函数中&#xff0c;常常有一些成员函数不改变类的数据成员&#xff0c;也就是说&#xff0c;这些函数是"只读"函数&#xff0c;而有一些函数要修改类数据成员的值。如果把不改变数据…

用servlet校验密码2

js //创建Ajax对象&#xff0c;不同浏览器有不同的创建方法&#xff0c;其实本函数就是一个简单的new语句而已。 function createXMLHttpRequest() {var XMLHttpRequest1;if (window.XMLHttpRequest) {XMLHttpRequest_test new XMLHttpRequest();} else if (window.ActiveXOb…

【笔试】:编程实现C++string 类成员函数

http://blog.csdn.net/wangshihui512/article/details/9792309 已知String类声明如下&#xff1a; [cpp] view plaincopy print?class String { public: String(const char *str NULL); // 通用构造函数 String(const String &another); // 拷贝…

Qt中字符串之间的转换

//QString -> C string -> char * str.ToStdString().data(); //先转换为C的标准编码//QString -> QByteArray QString buf "123456"; QByteArray a buf.toUtf8();//中文 a buf.toLocal8Bit(); //转换为本地编码 //QByteArray -> char * char *b …

(C语言版)栈和队列(一)——实现链式栈和链式队列的基本操作以及遇到的问题

http://blog.csdn.net/fisherwan/article/details/20055179 首先要感谢这位大牛的一篇博客&#xff0c;地址如下&#xff1a;http://blog.csdn.net/hguisu/article/details/7674195 当然还有网上一些其他的资料&#xff0c;今天自己写了一下链式栈和链式队列的程序。其中在释放…

Cookie的使用

ookie简介 1. 定义 cookie是由服务器发送给客户端&#xff08;浏览器&#xff09;的小量信息。 2. 作用 cookie是键值对形式存储的少量信息&#xff0c;那它有什么作用呢&#xff1f; 我们知道&#xff0c;平时上网时都是使用无状态的HTTP协议传输出数据&#xff0c;这意味着客…

循环链表解决约瑟夫问题(简化版)

http://blog.csdn.net/jw903/article/details/38965477 约瑟夫环是一个经典的数学的应用问题&#xff1a;已知N个人&#xff08;以编号1&#xff0c;2&#xff0c;3...N分别表示&#xff09;围坐在一张圆桌周围。从编号为1的人开始报数&#xff0c;数到M的那个人出列&#xff1…

Linux平台上SQLite数据库教程(一)——终端使用篇

http://blog.csdn.net/u011192270/article/details/48031763 SQLite是一款轻型的数据库&#xff0c;它的设计目标是嵌入式的&#xff0c;而且目前已经在很多嵌入式产品中使用了它&#xff0c;它占用资源非常的低&#xff0c;可能只需要几百K的内存就够了。能够支持Windows/Lin…

多路IO转接服务器 epoll

创建一个epoll句柄&#xff0c;参数size用来告诉内核监听的文件描述符的个数&#xff0c;跟内存大小有关。 #include <sys/epoll.h> int epoll_create(int size)   size&#xff1a;监听数目 通过创建一个size大小的红黑数来实现epoll句柄&#xff0c;返回epfd是该…

Linux平台上SQLite数据库教程(二)——C语言API介绍

http://blog.csdn.net/u011192270/article/details/48086961 前言&#xff1a;本文将介绍几个基本的SQLite3数据库的C语言API接口&#xff0c;主要用到两个文件&#xff1a;sqlite3.c、sqlite3.h。源码地址&#xff1a;https://github.com/AnSwErYWJ/SQLite。 打开数据库 1.原型…

epoll非阻塞IO

设置connfd套接字为非阻塞 flag fcntl(connfd, F_GETFL); flag | O_NONBLOCK; fcntl(connfd, F_SETFL, flag); 转载于:https://www.cnblogs.com/lr1402585172/p/10758740.html

小白创建网站的曲折之路

小白创建网站的曲折之路 在虚拟机上创建网站 顾名思义&#xff0c;首先要有一个虚拟机。在网上百度一下后&#xff0c;我发现大家都在说使用一种叫做VMware Workstation的软件进行虚拟机的构建 在这位好心人的帮助下我找到了Vmware Workstation的下载资源&#xff0c;并成功下…

Ubuntu 14.04数据库服务器--mysql的安装和配置

https://jingyan.baidu.com/article/425e69e6bbc6c7be14fc1640.html mysql是Oracle公司的一种开放源代码的关系型数据库管理系统&#xff0c;被广泛应用于各中小网站&#xff0c;是一种跨平台的数据库管理系统&#xff0c;现在介绍一下如何在Ubuntu 14.04上安装和配置mysql 工具…