Bilinear linked list code C ++ description

Source: Internet
Author: User

# 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

 

 

 

 

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.