数据结构中最开始学习实现的就是链表:
代码:
(1)这个头文件"list_head"为创建结构体,用来存储数据
#include<iostream>
using namespace std;
template <class type>
struct Node//创建结构体
{
type date;
Node *next;
Node();//空构造函数
Node(type item,Node<type> *link=NULL);//有初始化的构造函数
};
template <class type>
Node<type>::Node()
{
next=NULL;
}
template <class type>
Node<type>::Node(type item,Node<type> *link)
{
date=item;
next=link;
}
(2):头文件“simple_list”此为实现链表的类,其中包括插入删除等操作;
#include"list_head.h"
template <class type>
class simplelist
{
protected:
Node<type> *head;
Node<type> *Getelem(int position)const;//返回第position个数
//初始化函数
public:
void Init();
simplelist();
virtual~simplelist();
void clear();//将线性表清空
void creat();//创建线性表
void output();//输出线性表
int length();//线性表的长度
bool empty();
type delet(int position);
void insert(int position,type &date);
};
template<class type>
Node<type>* simplelist<type>::Getelem(int position)const//返回第position个数
{
Node<type>*ptr=head;//用ptr遍历线性表
int pos=0;
while(ptr!=NULL&&pos<position)
{
ptr=ptr->next;
pos++;
}
if(ptr!=NULL&&pos==position)
{
return ptr;
}
else
{
return NULL;
}
}
template <class type>//初始化链表
void simplelist<type>::Init()
{
head=new Node<type>;
}
template <class type>
simplelist<type>::simplelist()//构造函数
{
cout<<"创建了一个head为头的链表"<<endl;
Init();
}
template <class type>
simplelist<type>::~simplelist()//析构函数
{
clear();
delete head;
}
template <class type>//将函数清空
void simplelist<type>::clear()
{
}
template <class type>//创建链表
void simplelist<type>::creat()
{
Node<type> *p1,*p2;
p2=head;
type data[10]={1,2,3,5,3,6,12,7,8,9};
for(int i(0);i<10;i++)
{
p1=new Node<type>();
p1->date=data[i];
p2->next=p1;
p2=p2->next;
}
}
template <class type>
void simplelist<type>::output()//线性表的输出
{
Node<type> *ptr=head;
for(ptr=ptr->next;ptr!=NULL;ptr=ptr->next)
{
cout<<ptr->date<<"\t";
}
}
template <class type>
int simplelist<type>::length()//线性表的长度
{int count=0;//计数器
for(Node<type> *ptr=head->next;ptr!=NULL;ptr=ptr->next)
count++;
return count;
}
template <class type>
bool simplelist<type>::empty()//判断线性表是否为空
{
Node <type> *ptr=head;
if(head!=NULL)
return true;
else
return false;
}
template <class type>
type simplelist<type>::delet(int position)//删除第position处的节点
{
Node<type>*ptr=head->next;
Node<type> *p1=head;
type date;
for(int i(0);i<length();i++)
{
if(i==(position-1))
{ date=ptr->date;
ptr=ptr->next;
p1->next=ptr;
}
else{
p1=p1->next;
ptr=ptr->next;
}
}
return date;
}
template <class type>
void simplelist<type>::insert(int position,type & date)//将指定元素插入到指定位置
{
Node <type>*ptr=head;
Node<type> *p1;
p1=new Node<type>;
Node<type> *p2;
for(int i(0);i<length();i++)
{
if(i==(position-1))
{
p1->date=date;
p2=ptr->next;
ptr->next=p1;
p1->next=p2;
}
else
{
ptr=ptr->next;
}
}
if(position>length())//如果要插入的位置大于线性表的长度,插入在最后
{
p1->date=date;
ptr->next=p1;
}
}
(3):链表实现的主函数:
#include"simple_list.h"
#include<iostream>
using namespace std;
void main()
{
simplelist<int> List;
List.Init();
List.creat();
List.output();
cout<<"判断线性表是否为空"<<List.length()<<endl;
if(List.empty())
cout<<"线性表不为空"<<endl;
cout<<"删除第四个位置的元素为"<<List.delet(4)<<endl;
List.output();
int date,position;
cout<<"请输入你想插入的位置及值"<<endl;
cin>>position>>date;
List.insert(position,date);
List.output();
}