C++实现 简单 单链表
2017-09-29 15:03
369 查看
转自: http://blog.csdn.net/wonggonghong/article/details/21527577
我们首先建立一个<List.h>头文件,声明一个单链表结构:
#include "List.h"
[cpp] view
plain copy
//创建一个单链表结构,包含一些常见的操作
#ifndef _List_H_
#define _List_H_
#include <iostream>
struct Node{
int element; //节点存储信息可以根据需要修改!
Node* next;
};
Node* CreateLists(); //创建一个空表,并返回表头
void DeleteLists(Node* head); //删除表头为head的该链表
bool IsLast(Node* P);
Node* Find(int X, Node* head);
Node* FindPrevious(int X, Node* head);
void Delete(int X, Node* head);
void Insert(Node* P, int X); //在节点P后面插入X
void OutputLists(Node* head); //输出链表中所有元素的值
#endif
然后在<List.cpp>文件里实现头文件中的链表操作:
#include "List.cpp"
[cpp] view
plain copy
#include "list.h"
Node* CreateLists()
{
Node* head = new Node;
head->next = NULL;
return head;
}
void DeleteLists(Node* head)
{
Node* P = head->next, *temp;
head->next = NULL;
while(P)
{
temp = P->next;
delete P;
P = temp;
}
}
bool IsLast(Node* P)
{
return P->next == NULL;
}
Node* Find(int X, Node* head)
{
Node* P = head->next;
while(P && P->element!=X)
P = P->next;
return P;
}
Node* FindPrevious(int X, Node* head)
{
Node* P=head;
while(P->next && P->next->element!=X)
P=P->next;
return P;
}
void Delete(int X, Node* head)
{
Node* P = FindPrevious(X,head), *temp; //如果没找到X,则返回的是链表最后一项
if(P->next)
{
temp = P->next;
P->next = temp->next;
delete temp;
}
}
void Insert(Node* P, int X)
{
Node* tempX = new Node;
tempX->element = X;
tempX->next = P->next;
P->next = tempX;
}
void OutputLists(Node* head)
{
Node* P = head->next;
while(P)
{
std::cout<<P->element<<" ";
P = P->next;
}
std::cout<<std::endl;
}
最后,我们用一段main代码验证一下正确性:
[cpp] view
plain copy
#include <iostream>
#include <assert.h>
#include "list.h"
using namespace std;
int main()
{
int Date[8] = {2,9,5,8,15,32,7,4};
Node *head = CreateLists();
Node *P = head;
for(int i=0;i<8;i++)
{
Insert(P,Date[i]);
P = P->next;
}
cout<<"打印出链表中所有元素:\n";
OutputLists(head);
if(IsLast(P))
cout<<"该Lists的最后一个节点为:"<<P->element<<endl;
else
cout<<"P不是最后一个节点!!P等于:"<<P->element<<endl;
if(Find(15,head))
cout<<"Find函数在该Lists中找到了值为15的节点!!!\n";
else
cout<<"Find函数没找到值为15的节点!!\n";
cout<<"FindPrevious函数找到节点15的前一个节点为:"<<FindPrevious(15,head)->element<<endl;
cout<<"而节点15的前一个节点应该为“8”!\n";
Delete(8, head);
if(Find(8,head))
cout<<"Delete(8, head)后,在该Lists中找到了值为8的节点!!!\n";
else
cout<<"Delete(8, head)后,Find函数没找到值为8的节点!!\n";
DeleteLists(head);
if(head->next)
cout<<"DeleteLists函数未成功删除链表!!\n";
else
cout<<"链表已经为空!DeleteLists函数没有问题!!!\n";
return 0;
}
结果如下:
我们首先建立一个<List.h>头文件,声明一个单链表结构:
#include "List.h"
[cpp] view
plain copy
//创建一个单链表结构,包含一些常见的操作
#ifndef _List_H_
#define _List_H_
#include <iostream>
struct Node{
int element; //节点存储信息可以根据需要修改!
Node* next;
};
Node* CreateLists(); //创建一个空表,并返回表头
void DeleteLists(Node* head); //删除表头为head的该链表
bool IsLast(Node* P);
Node* Find(int X, Node* head);
Node* FindPrevious(int X, Node* head);
void Delete(int X, Node* head);
void Insert(Node* P, int X); //在节点P后面插入X
void OutputLists(Node* head); //输出链表中所有元素的值
#endif
然后在<List.cpp>文件里实现头文件中的链表操作:
#include "List.cpp"
[cpp] view
plain copy
#include "list.h"
Node* CreateLists()
{
Node* head = new Node;
head->next = NULL;
return head;
}
void DeleteLists(Node* head)
{
Node* P = head->next, *temp;
head->next = NULL;
while(P)
{
temp = P->next;
delete P;
P = temp;
}
}
bool IsLast(Node* P)
{
return P->next == NULL;
}
Node* Find(int X, Node* head)
{
Node* P = head->next;
while(P && P->element!=X)
P = P->next;
return P;
}
Node* FindPrevious(int X, Node* head)
{
Node* P=head;
while(P->next && P->next->element!=X)
P=P->next;
return P;
}
void Delete(int X, Node* head)
{
Node* P = FindPrevious(X,head), *temp; //如果没找到X,则返回的是链表最后一项
if(P->next)
{
temp = P->next;
P->next = temp->next;
delete temp;
}
}
void Insert(Node* P, int X)
{
Node* tempX = new Node;
tempX->element = X;
tempX->next = P->next;
P->next = tempX;
}
void OutputLists(Node* head)
{
Node* P = head->next;
while(P)
{
std::cout<<P->element<<" ";
P = P->next;
}
std::cout<<std::endl;
}
最后,我们用一段main代码验证一下正确性:
[cpp] view
plain copy
#include <iostream>
#include <assert.h>
#include "list.h"
using namespace std;
int main()
{
int Date[8] = {2,9,5,8,15,32,7,4};
Node *head = CreateLists();
Node *P = head;
for(int i=0;i<8;i++)
{
Insert(P,Date[i]);
P = P->next;
}
cout<<"打印出链表中所有元素:\n";
OutputLists(head);
if(IsLast(P))
cout<<"该Lists的最后一个节点为:"<<P->element<<endl;
else
cout<<"P不是最后一个节点!!P等于:"<<P->element<<endl;
if(Find(15,head))
cout<<"Find函数在该Lists中找到了值为15的节点!!!\n";
else
cout<<"Find函数没找到值为15的节点!!\n";
cout<<"FindPrevious函数找到节点15的前一个节点为:"<<FindPrevious(15,head)->element<<endl;
cout<<"而节点15的前一个节点应该为“8”!\n";
Delete(8, head);
if(Find(8,head))
cout<<"Delete(8, head)后,在该Lists中找到了值为8的节点!!!\n";
else
cout<<"Delete(8, head)后,Find函数没找到值为8的节点!!\n";
DeleteLists(head);
if(head->next)
cout<<"DeleteLists函数未成功删除链表!!\n";
else
cout<<"链表已经为空!DeleteLists函数没有问题!!!\n";
return 0;
}
结果如下:
相关文章推荐
- C++中实现一个简单的单向链表
- c++简单链表实现
- 利用C++简单实现顺序表和单链表的示例代码
- 简单数据结构之循环链表(C++实现)
- 简单双向链表(C++模版技术实现)
- linkList C++简单链式链表的实现
- C++简单实现双链表
- 几种简单的数据结构的C++实现,树,图,链表,环形队列,栈等---环形队列篇
- (C++版)链表(四)——实现双向循环链表创建、插入、删除等简单操作
- C++实现简单的双向链表
- C++链表结构简单实现
- C++简单实现顺序表与单链表
- 几种简单的数据结构的C++实现,树,图,链表,环形队列,栈等---栈篇
- 一个简单的C++的链表实现(使用类模板)
- 实现一个简单的c++ list容器(含sort排序 链表归并算法实现)
- 简单数据结构之双向链表(C++实现)
- C++实现简单链表
- [C++] 模板类实现简单链表
- C++实现简单的单链表
- C++模板类实现的简单的单链表