[Cpp]
// Main. cpp
# Include <iostream>
# Include <ctime>
# Include <cstdlib>
# Include <string>
# Include <vector>
# Include "linklist. cpp"
# Define MAXINTLEN 10
Using namespace std;
Int main (int argc, char * argv [])
{
Int tmp [MAXINTLEN];
Double runtime = 0;
Clock_t s, e;
Linklist sl;
For (int I = 0; I <MAXINTLEN; I ++)
{
// Srand (unsigned) time (I ));
Tmp [I] = rand () %100;
}
S = clock ();
Sl. construct_link (tmp, MAXINTLEN );
Cout <"this linklist..." <endl;
Sl. print_link ();
Sl. reverse ();
Cout <"after reverse..." <endl;
Sl. print_link ();
E = clock ();
Runtime = (double) (e-s); // CLOCKS_PER_SEC;
Cout <"runtime is:" <runtime <endl;
Return 0;
}
// Main. cpp
# Include <iostream>
# Include <ctime>
# Include <cstdlib>
# Include <string>
# Include <vector>
# Include "linklist. cpp"
# Define MAXINTLEN 10
Using namespace std;
Int main (int argc, char * argv [])
{
Int tmp [MAXINTLEN];
Double runtime = 0;
Clock_t s, e;
Linklist sl;
For (int I = 0; I <MAXINTLEN; I ++)
{
// Srand (unsigned) time (I ));
Tmp [I] = rand () %100;
}
S = clock ();
Sl. construct_link (tmp, MAXINTLEN );
Cout <"this linklist..." <endl;
Sl. print_link ();
Sl. reverse ();
Cout <"after reverse..." <endl;
Sl. print_link ();
E = clock ();
Runtime = (double) (e-s); // CLOCKS_PER_SEC;
Cout <"runtime is:" <runtime <endl;
Return 0;
}
[Cpp]
// Linklist. hxx
# Ifndef LINKLIST_H _
# Define LINKLIST_H _
# Include <iostream>
Using namespace std;
Struct node {
Int data;
Struct node * next;
};
Class linklist
{
Private:
Struct node * head;
Int length;
Public:
Linklist ();
~ Linklist ();
Struct node * get_link (void );
Void construct_link (int * arr, int num );
Int get_length (void );
Void reverse (void );
Void print_link (void );
};
# Endif
// Linklist. hxx
# Ifndef LINKLIST_H _
# Define LINKLIST_H _
# Include <iostream>
Using namespace std;
Struct node {
Int data;
Struct node * next;
};
Class linklist
{
Private:
Struct node * head;
Int length;
Public:
Linklist ();
~ Linklist ();
Struct node * get_link (void );
Void construct_link (int * arr, int num );
Int get_length (void );
Void reverse (void );
Void print_link (void );
};
# Endif
[Cpp]
// Linklist. cpp
# Include "linklist. hxx"
Linklist: linklist ()
{
This-> head = NULL;
This-> length = 0;
}
Struct node * linklist: get_link (void)
{
Return this-> head;
}
Void linklist: construct_link (int * arr, int num)
{// Use int array to construct linklist
Struct node * tmp, * cur;
For (int I = 0; I <num; ++ I)
{
Tmp = new (struct node );
Tmp-> data = arr [I];
Tmp-> next = NULL;
If (0 = I ){
This-> head = tmp;
} Else {
Cur-> next = tmp;
}
Cur = tmp;
This-> length ++;
}
Return;
}
Int linklist: get_length (void)
{
If (NULL = this-> head)
Return 0;
Struct node * cur = this-> head;
Int len = 0;
While (cur ){
Len ++;
Cur = cur-> next;
}
Return len;
}
Void linklist: reverse (void)
{// Reverse the linklist
If (! This-> head) return;
If (! This-> head-> next) return; // only one element
Struct node * cur = this-> head-> next;
Struct node * pre = this-> head;
Struct node * tmp = NULL;
Pre-> next = NULL;
While (cur ){
Tmp = cur-> next;
Cur-> next = pre;
Pre = cur;
Cur = tmp;
}
This-> head = pre;
Return;
}
Void linklist: print_link (void)
{// Print the linklist content
If (! This-> head ){
Cout <"this link is null." <endl;
Return;
}
Struct node * head = this-> head;
Struct node * cur = head;
Cout <"link (" <this-> length <"):";
While (cur ){
Cout <cur-> data <"";
Cur = cur-> next;
}
Cout <endl;
Return;
}
Linklist ::~ Linklist (){}
// Linklist. cpp
# Include "linklist. hxx"
Linklist: linklist ()
{
This-> head = NULL;
This-> length = 0;
}
Struct node * linklist: get_link (void)
{
Return this-> head;
}
Void linklist: construct_link (int * arr, int num)
{// Use int array to construct linklist
Struct node * tmp, * cur;
For (int I = 0; I <num; ++ I)
{
Tmp = new (struct node );
Tmp-> data = arr [I];
Tmp-> next = NULL;
If (0 = I ){
This-> head = tmp;
} Else {
Cur-> next = tmp;
}
Cur = tmp;
This-> length ++;
}
Return;
}
Int linklist: get_length (void)
{
If (NULL = this-> head)
Return 0;
Struct node * cur = this-> head;
Int len = 0;
While (cur ){
Len ++;
Cur = cur-> next;
}
Return len;
}
Void linklist: reverse (void)
{// Reverse the linklist
If (! This-> head) return;
If (! This-> head-> next) return; // only one element
Struct node * cur = this-> head-> next;
Struct node * pre = this-> head;
Struct node * tmp = NULL;
Pre-> next = NULL;
While (cur ){
Tmp = cur-> next;
Cur-> next = pre;
Pre = cur;
Cur = tmp;
}
This-> head = pre;
Return;
}
Void linklist: print_link (void)
{// Print the linklist content
If (! This-> head ){
Cout <"this link is null." <endl;
Return;
}
Struct node * head = this-> head;
Struct node * cur = head;
Cout <"link (" <this-> length <"):";
While (cur ){
Cout <cur-> data <"";
Cur = cur-> next;
}
Cout <endl;
Return;
}
Linklist ::~ Linklist (){}
Running result: