您的位置:首页 > 编程语言 > C语言/C++

算法1:链表的基本操作

2017-02-28 18:07 337 查看

算法1:链表的基本操作

编程实现链表的基本操作,包括插入,删除,查找,逆序,打印,获取长度等基本操作。

用c++实现如下:

定义LinkList头文件

#include <iostream>
#include <string.h>

//链表数据结构体
struct Node
{
int data;
Node* next;
Node(int _data):data(_data),next(NULL){}
};

class LinkList
{
public:
//构造函数
LinkList();
//析构函数
~LinkList();
//链表头部插入节点
void InsertHead(int data);
//链表插入节点
void Insert(int data,int pos);
//链表删除节点
void Remove(int data);
//链表查找节点
int Find(int data);
//获取链表长度
int Length();
//打印链表
void Print();
//逆序操作
void Reverse();

private:
Node* head;
int length;
};


LinkList实现如下:

#include "stdafx.h"
#include "LinkList.h"

using namespace std;

LinkList::LinkList()
{
head = NULL;
length = 0;
}

LinkList::~LinkList()
{
Node* tmp;
while(head != NULL)
{
tmp = head;
head = head->next;
delete tmp;
}
}

void LinkList::Insert(int data,int pos)
{
if(pos < 0)
{
cout<<"please input correct position: "<<pos<<endl;
return;
}

Node* node = new Node(data);

//pos=0表示插入头节点
if(pos == 0)
{
node->next = head;
head = node;
length++;
return;
}

int index = 1;
Node* tmp = head;
while(tmp != NULL && index < pos)
{
tmp = tmp->next;
index++;
}

if(tmp == NULL)
{
cout<<"can't insert node in pos"<<pos<<endl;
return;
}

node->next = tmp->next;
tmp->next = node;
length++;
}

void LinkList::InsertHead(int data)
{
Insert(data,0);
}

int LinkList::Find(int data)
{
Node* tmp = head;

for(int i = 0; i < length; i++)
{
if(tmp != NULL && tmp->data == data)
{
return i+1;
}

tmp = tmp->next;
}

return -1;
}

void LinkList::Remove(int data)
{
int pos = Find(data);

if(pos < 0)
{
cout<<"this data is not exist, so can't remove it. data="<<data<<endl;
return;
}

//需要考虑删除头节点的问题
if(pos == 1)
{
head = head->next;
length--;
return;
}

int index = 2;
Node* tmp = head;
while(index < pos)
{
tmp = tmp->next;
index++;
}

tmp->next = tmp->next->next;
length--;
}

int LinkList::Length()
{
return length;
}

void LinkList::Print()
{
if(head == NULL)
{
cout<<"linklist is empty"<<endl;
return;
}

Node* tmp = head;

cout<<"linklist is ";
while(tmp != NULL)
{
cout<<tmp->data<<"  ";
tmp = tmp->next;
}
cout<<endl;
}

void LinkList::Reverse()
{
if(head == NULL)
{
cout<<"linklist is empty";
return;
}

Node* curNode = head;
Node* nextNode = head->next;
Node* tmp;

while(curNode != NULL && nextNode != NULL)
{
tmp = nextNode->next;
nextNode->next = curNode;
curNode = nextNode;
nextNode = tmp;
}

head->next = NULL;
head = curNode;
}


编写测试代码如下:

int _tmain(int argc, _TCHAR* argv[])
{
LinkList link;
cout<<"insert head: ";
int data;
cin>>data;
link.InsertHead(data);

int length = 8;
int index = 1;
cout<<"insert data:";
while(index < length)
{
cin>>data;
link.Insert(data,index);
index++;
}

cout<<"the result after insert:"<<endl;
link.Print();

cout<<"length = "<<link.Length()<<endl;

cout<<"please input an exist data for testing remove function"<<endl;
cin>>data;
link.Remove(data);
cout<<"length = "<<link.Length()<<endl;
cout<<"the result after remove:"<<endl;
link.Print();

cout<<"please input an non-existent data for testing remove function"<<endl;
cin>>data;
link.Remove(data);
cout<<"length = "<<link.Length()<<endl;
cout<<"the result after remove:"<<endl;
link.Print();

cout<<"please input an exist data for testing find function"<<endl;
cin>>data;
cout<<"length = "<<link.Find(data)<<endl;

cout<<"please input an non-existent data for testing find function"<<endl;
cin>>data;
cout<<"length = "<<link.Find(data)<<endl;

cout<<"the result after reverse"<<endl;
link.Reverse();
link.Print();

system("pause");

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言 算法