Simple application of double linked list

Source: Internet
Author: User

/* When the single linked list is understood, the double linked list is much better understood, a single linked list is only one pointer in a struct or class that stores the address of the next node, and the double linked list has two pointers in it, one to store the address of the previous node, and one to store the next node's address. Such a linked list is called a double linked list. */

#include <iostream> using namespace std;
struct Node
{
int A;
Node *prior,*next; Front pointer, back pointer
};
int main ()
{
Node *first,*r,*p;
First=new Node (); Because inserting data into a doubly-linked list uses its previous node, or the latter node, so here R represents the second node of first, and the following
R=new Node ();
first->next=r;
r->prior=null;
for (int i=20;i>=0;i--)///21 p pointers are inserted into the list in the first header node in turn.
{
P=new Node ();
p->a=i;
p->next=first->next; The next pointer to the P stores the address of the next pointer store.
p->prior=first; The previous node of P is first, so the address of prior
first->next->prior=p; The original node now stores the address of P.
first->next=p; The next node in the second is P, and here are the sequence of the four statements that need to be noted.
}
p=first->next;
for (int i=0;i<=20;i+=2)
{
while (P->next!=null)
{
if (p->a==i)
{
p->prior->next=p->next; Here is a node that deletes a value of even, and the rest is an odd node
p->next->prior=p->prior;
cout<<p->a<< "";
Delete p;
Break
}
p=p->next;
}
}
p=first->next;
cout<<endl;
while (P->next!=null)
{
cout<<p->a<< ""; The output is an odd node
p=p->next;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.