首先头文件,定义了链表的常用函数:
typedef struct node
{
int data;
struct node* next;
} Node;
class LinkListUtil
{
public:
LinkListUtil();
~LinkListUtil();
//create
Node* createByArray(int arr[], int len);
int getLenght(Node *head);
void println(Node *head);
Node* remove(Node *head, int num);
//Insert element into the link list by the index.
//if index is -1 then insert the num into the link list end.
Node* insert(Node *head, int num, int index = -1);
Node* sort(Node *head);
Node* reserve(Node *head);
Node* removeHead(Node *head);
};
然后是实现:
#include "stdafx.h"
#include "LinkListUtil.h"
LinkListUtil::LinkListUtil()
{
}
LinkListUtil::~LinkListUtil()
{
}
Node* LinkListUtil::createByArray(int arr[], int len)
{
//Declare head node, previous node, new node.
Node *head, *p, *n;
//Init head node.
head = new Node();
p = head;
for (int i = 0; i < len; i++)
{
n = new Node();
n->data = arr[i];
p->next = n;
p = n;
}
head = head->next;
//head->next = nullptr; //?
return head;
}
int LinkListUtil::getLenght(Node *node)
{
int len = 0;
Node *temp = node;
while (temp != nullptr)
{
temp = temp->next;
len++;
}
return len;
}
void LinkListUtil::println(Node *node)
{
Node *temp = node;
while (temp != nullptr)
{
printf("the node data is : %d\n", temp->data);
temp = temp->next;
}
}
Node* LinkListUtil::remove(Node *head, int num)
{
Node *temp = head;
Node *pre = nullptr;
//find the node
while (temp != nullptr)
{
if (temp->data != num)
{
pre = temp;
temp = temp->next;
}
else
{
break;
}
}
if (pre == nullptr)
{
head = head->next;
}
else {
pre->next = temp->next;
}
delete temp;
return head;
}
Node* LinkListUtil::insert(Node *head, int num, int index)
{
int len = getLenght(head);
if (index >= len)
{
printf("The index is greater than the length of link list!\n");
return head;
}
Node *temp = head;
Node *pre = nullptr;
//if index is -1 then insert the num into the link list end.
int tempIndex = 0;
if (index <= -1)
index = len;
//find the insert point.
while (temp != nullptr && index != tempIndex)
{
tempIndex++;
pre = temp;
temp = temp->next;
}
//insert the num.
Node *nd;
nd = new Node();
nd->data = num;
if (pre == nullptr)
{
nd->next = head;
delete pre, temp;
return nd;
}
else {
nd->next = temp;
pre->next = nd;
return head;
}
}
Node* LinkListUtil::sort(Node *head)
{
//if only has one element or none elements
if (head == nullptr || head->next == nullptr)
return head;
Node *p0, *p1;
int temp = 0;
p0 = head;
//bubble sort
while (p0 != nullptr && p0->next != nullptr)
{
p1 = p0->next;
while (p1 != nullptr)
{
if (p0->data > p1->data)
{
temp = p0->data;
p0->data = p1->data;
p1->data = temp;
}
p1 = p1->next;
}
p0 = p0->next;
}
return head;
}
Node* LinkListUtil::reserve(Node *head)
{
//if only has one element or none elements
if (head == nullptr || head->next == nullptr)
return head;
Node *p0, *p1, *p2;
p0 = head;
p1 = p0->next;
while (p1 != nullptr)
{
p2 = p1->next;
p1->next = p0;
p0 = p1;
p1 = p2;
}
head->next = nullptr;
return p0;
}
Node* LinkListUtil::removeHead(Node *head)
{
//if only has one element or none elements
if (head == nullptr || head->next == nullptr)
return head;
Node *p0;
p0 = head->next;
head->next = p0->next;
head = p0;
return head;
}
最后是main测试:
// Win32Project2.cpp
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <assert.h>
#include "LinkListUtil.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//
int arr[5] {3, 1, 9, 4, 6};
auto linkList = new LinkListUtil();
auto head = linkList->createByArray(arr, 5);
//Get linkList size;
auto len = linkList->getLenght(head);
printf("LinkList's length:%d\n", len);
printf("Print LinkList:\n");
linkList->println(head);
printf("Remove element 1 and print LinkList:\n");
head = linkList->remove(head, 1);
linkList->println(head);
printf("Insert element 5 into the index 1 and print LinkList:\n");
head = linkList->insert(head, 5, 1);
linkList->println(head);
printf("Insert element 2 into the end and print LinkList:\n");
head = linkList->insert(head, 2);
linkList->println(head);
printf("Bubble sort:\n");
head = linkList->sort(head);
linkList->println(head);
printf("reserve:\n");
head = linkList->reserve(head);
linkList->println(head);
printf("remove head:\n");
head = linkList->removeHead(head);
linkList->println(head);
//clear
delete linkList;
//
system("pause");
return 0;
}