程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C++實現:單鏈表的反轉(序)操作

C++實現:單鏈表的反轉(序)操作

編輯:C++入門知識

[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(){}


運行結果:

 

\

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved