Insert or delete a double-Link Table

Source: Internet
Author: User

In fact, there is nothing in a double-chain table, but there is only one additional pre-chain.

Double-chain table definition

[Cpp]
Struct DNode
{
Int data;
Struct DNode * next;
Struct DNode * pre;
};
 

Single-chain table definition

[Cpp] view plaincopy
Struct DNode
{
Int data;
Struct DNode * next;
};

Others can be viewed in the previous blog, which is roughly the same

[Cpp]
# Ifndef HEAD_H
# Define HEAD_H
# Include <iostream>
Using namespace std;
# Include <cassert>
# Include <cstdlib>
# Include <cmath>
# Include <sstream>
# Include <fstream>
# Include <string>
# Include <algorithm>
# Include <list>
# Include <queue>
# Include <vector>
# Include <deque>
# Include <stack>
# Include <bitset>
# Include <set>
# Include <map>
# Endif
 
Struct DNode
{
Int data;
Struct DNode * next;
Struct DNode * pre;
};
 
 
DNode * Creat ()
{Www.2cto.com
DNode * head, * p, * s;
Head = (DNode *) malloc (sizeof (DNode ));
P = head;
Int temp;
While (cin> temp & temp)
{
S = (DNode *) malloc (sizeof (DNode ));
S-> data = temp;
P-> next = s;
S-> pre = p;
P = s;
}
Head = head-> next;
P-> next = NULL;
Head-> pre = NULL;
Return (head );
}
 
DNode * Insert (DNode * & head, int num)
{
DNode * p, * s;
S = (DNode *) malloc (sizeof (DNode ));
S-> data = num;
P = head;
While (NULL! = P-> next & num> p-> data)
{
P = p-> next;
}
If (num <= p-> data)
{
If (NULL = p-> pre)
{
S-> next = head;
Head-> pre = s;
Head = s;
Head-> pre = NULL;
}
Else
{
S-> pre = p-> pre;
P-> pre-> next = s;
S-> next = p;
P-> pre = s;
}
}
Else
{
P-> next = s;
S-> pre = p;
S-> next = NULL;
}
Return (head );
}
 
DNode * Del (DNode * & head, int num)
{
DNode * p;
P = head;
While (NULL! = P-> next & num! = P-> data)
{
P = p-> next;
}
If (num = p-> data)
{
If (NULL = p-> pre)
{
Head = p-> next;
P-> next-> pre = head;
Free (p );
}
Else if (NULL = p-> next)
{
P-> pre-> next = NULL;
Free (p );
}
Else
{
P-> pre-> next = p-> next;
P-> next-> pre = p-> pre;
Free (p );
}
}
Else
{
Cout <num <"cound not be found" <endl;
}
Return head;
}
 
Void Display (DNode * head)
{
DNode * p;
P = head;
While (NULL! = P)
{
Cout <(p-> data) <"";
P = p-> next;
}
Cout <endl;
}

 

[Cpp]
# Include "head. h"
 
Int main ()
{
DNode * head;
Head = Creat ();
Display (head );
# Ifndef DEBUG
Cout <"please input an num to insert :";
# Endif
Int insert_num;
While (cin> insert_num & insert_num)
{
Insert (head, insert_num );
Display (head );
}
# Ifndef DEBUG
Cout <"please input an number to delete :";
# Endif
Int delete_num;
While (cin> delete_num & delete_num)
{
Del (head, delete_num );
Display (head );
}
Return (EXIT_SUCCESS );
}

Related Article

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.