C ++ implementation: reverse (sequential) operations on a single-chain table

Source: Internet
Author: User

[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:

 

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.