}
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."<
}
struct node* head = this->head;
struct node *cur = head;
cout<<"link ("<
while(cur){
cout<
cur = cur->next;
}
cout<
}
linklist::~linklist(){}
运行结果:
