Double linked list Delete/Insert node

Source: Internet
Author: User

Double Linked list Delete node
Dnode *del (dnode *head, int num)
{
Dnode *p1, *P2;
P1 = head;
while (num! = P1->data && P1->next! = NULL)
{
P1 = p1->next;
}
if (num = = p1->data)
{
if (P1 = = head)
{
Head = head->next;
Head->pre = NULL;
Free (p1);
}
else if (P1->next = = NULL)
{
P1->pre->next = NULL;
Free (p1);
}
Else
{
P1->next->pre = p1->pre;
P1->pre->next = p1->next;
Free (p1);
}
}
Else
printf ("\ n%d could not been found", num);
return (head);

}

Double-linked list insertion node
Dnode * Insert (dnode *head, int num)
{
Dnode *p0, *P1;
P1 = head;
P0 = (Dnode *) malloc (sizeof (Dnode));
P0->data = num;
while (P0->data > P1->data && p1->next! = NULL)
{
P1 = p1->next;
}
if (P0->data <= p1->data)
{
if (head = = p1)
{
P0->next = p1;
P1->pre = P0;
head = P0;
}
Else
{
P1->pre->next = P0;
P0->next = p1;
P0->pre = p1->pre;
P1->pre = P0;
}
}
Else
{
P1->next = P0;
P0->pre = p1;
P0->next = NULL;
}
return head;
}
int main ()
{
Dnode *head, Stud;
int n, del_num, Insert_num;
Head = creat ();
Print (head);
cout << "\ n Int:";
Cin >> Del_num;
Head = Del (head, del_num);
Print (head);
cout << "\nplease input the Insert data:";
Cin >> Insert_num;
Head = insert (head, insert_num);
Print (head);
return 0;
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Double linked list Delete/Insert node

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.