双链表--节点前后插入练习

试分别在带头结点head的双链表中的元素值为5的节点之前、之后插入元素值为4的节点。

算法1:元素值为5的节点之前插入元素值为4的节点。

#include 
#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<x<<" ";
	   pr=pr->next;
   }
   cout<

算法2:元素值为5的节点之后插入元素值为4的节点。

#include 
#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 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<x<<" ";
	   pr=pr->next;
   }
   cout<

转载于:https://my.oschina.net/u/877340/blog/146673

你可能感兴趣的:(双链表--节点前后插入练习)