双链表--节点前后插入练习
2013-07-24 00:00
260 查看
试分别在带头结点head的双链表中的元素值为5的节点之前、之后插入元素值为4的节点。
算法1:元素值为5的节点之前插入元素值为4的节点。
算法2:元素值为5的节点之后插入元素值为4的节点。
算法1:元素值为5的节点之前插入元素值为4的节点。
#include <iostream.h> #define M 5 typedef struct DLinkList{ int x;//data x; struct DLinkList *next;//pointing to the next DLinkList; struct DLinkList *prior;//pointing to the prior DLinkList; }DLinkList; void main(){ //set an empty link; DLinkList *head,*pr; head=new DLinkList; head->next=head; head->next=head; pr=head;//pr points to the last node; //seting a link with m nodes; for(int i=1;i<=M;i++){ DLinkList *p=new DLinkList;//creat a new node; cin>>p->x;//Do input 5 only once; pr->next=p;//let the last node's next point to p; p->prior=pr;//let the newly-created node's prior point to the last node; p->next=head;//let the newly-created node's next point to the head node; head->prior=p;//let the head node 's prior point to the created node; pr=p;//let pr point to the newly-created node; } DLinkList *p=new DLinkList; p->x=4;//creating a new node which will be inserted into the link; //finding the node with x=5; pr=head; while(pr->x!=5){ pr=pr->next; } //inserting a node with x=4 before the node with x=5 of the link; pr->prior->next=p;//let the node *pr's prior points to point to the newly-created node's p->prior=pr->prior;//let the newly-created node's prior point to the node *pr's prior points to p->next=pr;//let *p's next point to *pr; pr->prior=p;//let *pr's prior point to the newly-created node; pr=head->next;//pr points to the node with data; while(pr!=head){//one circle cout<<pr->x<<" "; pr=pr->next; } cout<<endl; }
算法2:元素值为5的节点之后插入元素值为4的节点。
#include <iostream.h> #define M 5 typedef struct DLinkList{ int x;//data x; struct DLinkList *next;//pointing to the next DLinkList; struct DLinkList *prior;//pointing to the prior DLinkList; }DLinkList; void main(){ //set an empty link; DLinkList *head,*pr; head=new DLinkList; head->next=head; head->next=head; pr=head;//pr points to the last node; //seting a link with m nodes; for(int i=1;i<=M;i++){ DLinkList *p=new DLinkList;//creat a new node; cin>>p->x;//Do input 5 only once; pr->next=p;//let the last node's next point to p; p->prior=pr;//let the newly-created node's prior point to the last node; p->next=head;//let the newly-created node's next point to the head node; head->prior=p;//let the head node 's prior point to the created node; pr=p;//let pr point to the newly-created node; } DLinkList *p=new DLinkList; p->x=4;//creating a new node which will be inserted into the link; //finding the node with x=5; pr=head; while(pr->x!=5){ pr=pr->next; } //inserting a node with x=4 after the node with x=5 of the link; pr->next->prior=p;//let the node *pr's next 7fe0 points to point to the newly-created node's p->next=pr->next;//let the newly-created node's next point to the newly-created node pr->next=p;//let *pr's next point to *p; p->prior=pr;//let *p's prior point to the *pr; pr=head->next;//pr points to the node with data; while(pr!=head){//one circle cout<<pr->x<<" "; pr=pr->next; } cout<<endl; }
相关文章推荐
- golang基础-结构体、结构体链表前后插入、节点添加删除
- 单向链表小练习--》创建链表头,添加删除链表节点,释放链表内存,选择插入数据
- 单链表节点的插入
- c++基础之插入和删除链表节点
- C++链表插入,为什么头节点必须是二位指针
- C++链表插入节点
- 单向链表插入新节点
- 链表内节点插入
- 双向循环链表的建立,插入,删除(带头节点)
- 数据结构和算法设计专题之---单链表中在指定的节点前面插入以及删除一个节点
- 单向链表中,如何在给定节点前快速插入一个节点?
- 链表定义以及实现(遍历,节点计数,插入节点)--1(面向对象的方法实现)
- 链表中插入一个节点的三种情况
- 单链表创建、测长度、打印、删除节点、插入节点、排序、逆置
- C语言单向链表的创建、释放、插入、删除、翻转操作练习
- 数据结构和算法设计专题之---单链表中在指定的节点前面插入以及删除一个节点
- 单向链表中,如何在给定节点前快速插入一个节点?
- 单向链表的节点插入与删除
- C语言实现双向非循环链表(带头结点尾结点)的节点插入
- 双向链表的初始化,建立,添加节点和删除节点(注意插入和删除要分三种情况)