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 );
}