It mainly implements the following functions:
Void addback (T Val );// Add elements at the end of the linked list
Void addfront (T Val );// Add elements to the linked list Header
Bool insertat (INT POs, t Val );// Insert an element in the specified index
Bool removeback ();// Delete the element at the end
Bool removefront ();// Delete element in the header
Bool removeat (int pos );// Delete the element at the specified index
Bool find (T Val );// If the element is found, the index is returned. If the index is not found,-1 is returned.
Void clear ();// Clear the linked list
Bool isempty ();//Judge whether the linked list is empty
Int size ();// Returns the number of elements in the linked list.
1. Define linked list nodes
Template <class T>
Class dnode
{
Public:
~ Dnode (void ){}
T val; // node Value
Dnode <t> * Next; // point to the following node pointer
Dnode <t> * Prior; // pointer to the previous Node
Dnode (T nval)
{
Val = nval;
Next = prior = NULL;
}
Dnode (void ){}
};
2. double-stranded table class definition
# Include "dnode. H"
# Include <iostream>
Using namespace STD;
Template <class T>
Class dlinklist
{
Int size;
Public:
Dnode <t> * head; // pointer to the header
Dnode <t> * tail; // point to the tail pointer
Dlinklist (void)
{
Head = tail = NULL;
Size = 0;
}
~ Dlinklist (void) {clear ();}
VoidAddback (T Val)// Add elements at the end
{
Dnode <t> * pnode = new dnode <t> (VAL );
If (Head = NULL) // when the linked list is empty
{
Head = tail = pnode;
}
Else {
Tail-> next = pnode;
Pnode-> prior = tail;
Tail = pnode; // The new node is the tail node.
}
Size ++;
}
VoidAddfront (T Val)// Add an element to the header
{
Dnode <t> * pnode = new dnode <t> (VAL );
If (Head = NULL) // when the linked list is empty
{
Head = tail = pnode;
}
Else {
Head-> prior = pnode;
Pnode-> next = head;
Head = pnode;
}
Size ++;
}
Bool insertat (int
POs, t Val)// Insert an element anywhere
{
Dnode <t> * pnode = NULL;
If (Pos <0 | POS> size ){
Cout <"out of range" <Endl;
Returnfalse;
}
Pnode = new dnode <t> (VAL );
If (Pos = 0) // Head Position
Addfront (VAL );
Elseif (Pos
= Size-1)
Addback (VAL );
Else {
Dnode <t> * priornode = getpointerat (POS-1 );
Pnode-> next = priornode-> next;
Pnode-> prior = priornode;
Priornode-> next-> prior = pnode;
Priornode-> next = pnode;
}
Size ++;
Return true;
}
Bool removeat (int
Pos)// Delete the element at the specified index
{
Dnode <t> * pnode = NULL;
If (size = 0 ){
Cout <"list is empty" <Endl;
Returnfalse;
}
If (Pos <0 | POS> size-1 ){
Cout <"out of range" <Endl;
Returnfalse;
}
If (size = 1)
Clear ();
Else {
If (Pos = 0) // Head Position
{
Pnode = head;
Head = head-> next;
Head-> prior = NULL;
Delete pnode;
}
Else {
Dnode <t> * priornode = getpointerat (POS-1 );
Pnode = priornode-> next;
Priornode-> next = pnode-> next;
Pnode-> next-> prior = priornode;
Delete pnode;
If (Pos = size-1) // if it is a tail Element
Tail = priornode;
}
}
}
Bool removeback (){Return
Removeat (size-1 );}
Bool removefront (){Return
Removeat (0 );}
Int find (T Val)
{
Int Index = 0;
Dnode <t> * IP = head;
While (IP! = NULL ){
If (IP-> val = Val)
Return Index;
IP = IP-> next;
Index ++;
}
Return-1;
}
Bool isempty (){Return
Size = 0? True: false ;}
Int size (){Return
Size ;}
Void clear (){
While (Head! = NULL ){
Dnode <t> * TMP = head-> next;
Delete head;
Head = TMP;
}
Tail = NULL;
Size = 0;
}
PRIVATE:
Dnode <t> * getpointerat (int pos)
{
Dnode <t> * pnode = NULL;
If (Pos <0 | POS> size-1)
Cout <"out of range" <Endl;
Else {
Pnode = head;
For (int
I = 1; I <= Pos; I ++)
Pnode = pnode-> next;
}
Return pnode;
}
};