基于C++模板 单链表基本操作
2017-03-16 19:10
344 查看
涵盖单链表的基本操作方法,如有不足,欢迎提意见!
list.h
#ifndef _LIST_H_
#define _LIST_H_
/*节点类*/
template<class T>
class Node
{
public:
T data;
Node* node;
};
/*链表类*/
template<class T>
class List
{
private:
Node* head; //头节点
public:
List (T a);
T GetHeadVal(); //获取链表首部值
T GetTailVal(); //获取链表尾部值
void Add(T a); //在链表尾部添加元素
void Insert(T a); //插入一个链表
int Find(T val) ; //获得有此值的链表的索引号
bool IsEmpty(); //判断链表是否为空
int Size() ; //返回链表总元素个数
void show(); //打印链表中元素
~List();
};
#endiflist.cpp
#include"list.h"
#include<iostream>
using namespace std;
int main()
{
List<int>list(1);
list.Add(2);
list.Insert(0);
list.Insert(10);
list.Insert(20);
list.show();
cout<<endl<<endl;
cout<<"size ="<<list.Size()<<endl;
return 0;
}
list.h
#ifndef _LIST_H_
#define _LIST_H_
/*节点类*/
template<class T>
class Node
{
public:
T data;
Node* node;
};
/*链表类*/
template<class T>
class List
{
private:
Node* head; //头节点
public:
List (T a);
T GetHeadVal(); //获取链表首部值
T GetTailVal(); //获取链表尾部值
void Add(T a); //在链表尾部添加元素
void Insert(T a); //插入一个链表
int Find(T val) ; //获得有此值的链表的索引号
bool IsEmpty(); //判断链表是否为空
int Size() ; //返回链表总元素个数
void show(); //打印链表中元素
~List();
};
#endiflist.cpp
#include"list.h" #include<iostream> using namespace std; /*构造函数*/ template<class T> List::List(T a) { head = new Node; head->data=a; head->node=NULL; } /*获得首链表的值*/ template<class T> T List::GetHeadVal() { T p=head->data; return p; } /*获得链表末尾元素的值*/ template<class T> T List::GetTailVal() { Node* current=head; for(;current;current=current->node) { ; } return (current->data); } /*在链表中插入元素*/ template<class T> void List::Insert(T a) //在索引为pos位置添加元素 { Node* listnode = new Node; listnode->data=a; listnode->node=head->node; head->node=listnode; } /*获得有此值的链表的索引号*/ template<class T> int List::Find(T val) { Node* current =head; int num = 0; for(;current;current = current->node) { if(current->data == val) { break; } num ++; } return num; } /*判断链表是否为空*/ template<class T> bool List::IsEmpty() { bool result=false; if(head->node == NULL) { result=true; } return result; } /*在链表尾部插入元素*/ template<class T> void List::Add(T a) { Node* newnode= new Node; while(head->node) { head->node=head->node->node; } head->node = newnode; newnode->node=NULL; newnode->data=a; } /*返回链表总元素个数*/ template<class T> int List::Size() { Node *p=head; int count=0; for(;p;p=p->node) count++; return count; } /*打印链表中数据元素*/ template<class T> void List::show() { for(Node* current = head;current;current=current->node) { cout<<"list ="<<current->data<<endl; } } /*析构,清空链表*/ template<class T> List::~List() { Node* p= head; while(p) { Node* q =p->node; delete p; p=q; } }main.cpp
#include"list.h"
#include<iostream>
using namespace std;
int main()
{
List<int>list(1);
list.Add(2);
list.Insert(0);
list.Insert(10);
list.Insert(20);
list.show();
cout<<endl<<endl;
cout<<"size ="<<list.Size()<<endl;
return 0;
}
相关文章推荐
- C++ 实现链表的基本操作之一:链表插入
- 链表常用操作的实现-C++模板实现
- 【C++数据结构】模版类实现双循环链表的基本操作
- C++中单链表的基本操作:判空、增、删、显示
- C++实现链表基本操作
- c++链表基本操作
- C++-----单链表基本操作
- 基于c++的单链表,双向链表操作以及环
- C++数据结构 链表的基本操作
- 链表的基本操作C/c++
- [C++]数据结构:有序链表SortedChain的基本实现与操作
- c++模板实现自定义链表及操作
- 使用C++实现链表的基本操作
- 线性表的基本操作实现(基于链表)
- C++写的模板类链表基本操作
- C++中单链表的建立与基本操作
- 单链表结点读取、插入、删除操作(基本操作)-C++
- c++实现链表的基本操作
- C++实现链表的基本操作及测试用例
- 线性表的基本操作实现(基于链表,有头结点)