# Include <iostream>
# Include <stdlib. h>
Template <class T>
Struct node {// Node Type
T data;
Node * Next; // The frontend node.
Node * Prev; // The successor node.
};
Template <class T>
Class lnklist {// linked list type
Node <t> * head, * tail;
Public:
Lnklist () {// initialize the linked list
Head = tail = new node <t>;
Head-> next = NULL;
}
~ Lnklist () {// destructor, delete the linked list
Node <t> * TMP;
While (Head! = NULL ){
TMP = head;
Head = head-> next;
Delete TMP;
}
}
Bool insert (const int N, T value );
Bool Delete (const int N, T & value );
Bool tailappend (const T value );
Bool headappend (const T value );
Bool getvalue (const int N, T & value );
Void print ();
};
Template <class T>
Bool lnklist <t>: headappend (const T value) {// header Insertion Method
Node <t> * P = new node <t>;
P-> DATA = value;
P-> next = head-> next;
P-> Prev = head;
Head-> next-> Prev = P;
Head-> next = P;
Return true;
}
Template <class T>
Bool lnklist <t>: tailappend (const T value) {// end Method
Node <t> * P = tail;
Node <t> * q = new node <t>;
Q-> DATA = value;
Q-> Prev = Q;
Q-> next = NULL;
P-> next = Q;
Tail = Q;
Return true;
}
Template <class T>
Void lnklist <t >:: print () {// print the output linked list
Node <t> * P = head-> next;
While (P! = NULL ){
STD: cout <p-> data <STD: Endl;
P = p-> next;
}
}
Template <class T>
Bool lnklist <t>: insert (const int N, T value) {// insert an element at the N position of the linked list
Node <t> * q = head;
Int COUNT = 1;
While (Q & count <n ){
Q = Q-> next;
++ Count;
}
If (! Q | count> N ){
Return false;
}
Node <t> * P = new node <t>;
P-> DATA = value;
P-> Prev = Q;
P-> next = Q-> next;
Q-> next-> Prev = P;
Q-> next = P;
Return true;
}
Template <class T>
Bool lnklist <t>: delete (const int N, T & Value) {// Delete the nth position element of the linked list
Node <t> * q = head;
Int COUNT = 1;
While (Q & count <n ){
Q = Q-> next;
++ Count;
}
Node <t> * P = Q-> next;
Q-> next = p-> next;
P-> next-> Prev = Q;
Value = p-> data;
Delete P;
Return true;
}
Template <class T>
Bool lnklist <t>: getvalue (const int N, T & Value) {// obtain the nth element of the linked list.
Node <t> * q = head;
Int COUNT = 1;
While (Q & count <n ){
Q = Q-> next;
++ Count;
}
If (n <1 |! Q ){
Return false;
}
Value = Q-> next-> data;
Return true;
}
// Test code
//////////////////////////////////////// //////////////////////////////////
Int main (INT argc, char * argv [])
{
Double m;
Lnklist <double> L;
L. tailappend (2 );
L. tailappend (3 );
L. tailappend (4 );
L. tailappend (5 );
L. headappend (1 );
L. headappend (0 );
L. insert (3, 1.5 );
L. getvalue (3, M );
L. Print ();
Return 0;
}
//////////////////////////////////////// //////////////////////////////////
// Passed the test on vs2008