C++,数据结构,单向链表的实现及简单运用,运用模板
2014-11-01 16:38
716 查看
/*
代码功能:
1.链接存储方式实现单向链表(表头含有一个空结点作为哨位结点)
2.将另一数组中的数据读入该链表
3.链表数据排序(插入排序)
4.从外界插入一个数据,并使插入后的链表依然有序
5.实现过程中运用了模板
备注:按课程要求做的,有点糙。
*/
//.h文件
#ifndef LIST_H
#define LIST_H
#include<iostream>
template<typename T>
struct Node_for_List //声明结点
{
T data;
Node_for_List<T>* next;
Node_for_List(Node_for_List<T>* pnext = NULL){ next = pnext; } //构造空结点
Node_for_List(const T& item ) :data(item), next(NULL){};
Node_for_List(const T& item, Node_for_List<T>* pnext);
};
template<typename T> //单链表类
class Single_List
{
private:
Node_for_List<T> *head,*end,*p;
int size; //记录表结点个数
public:
Single_List();
Single_List(T& item);
~Single_List();
int Get_size()const;
bool If_Empty(){ return head->next == NULL; }
Node_for_List<T>* Create_List(const T s[], const int count);
T Show_Data(Node_for_List<T>* ptr);
Node_for_List<T>* Sort();
Node_for_List<T>* Insert_as_Order(const T item);
};
template<typename T> //构造表结点
Node_for_List<T>::Node_for_List(const T& item, Node_for_List<T>* pnext)
{
data = item;
next = pnext;
}
template<typename T> //无参构造函数,只创建含哨位结点的单链表
Single_List<T>::Single_List()
{
head = end = p = new Node_for_List<T>();
size = 0;
}
template<typename T> //构造函数,创建一个含哨位结点和一个表结点的单链表
Single_List<T>::Single_List(T& item)
{
p = end = new Node_for_List<T>(item);
head = new Node_for_List(p);
size = 1;
}
template<typename T> //析构函数
Single_List<T>::~Single_List()
{
while (!If_Empty())
{
p = head->next;
head->next = p->next;
delete p;
}
delete head;
}
template<typename T> //创建单链表
Node_for_List<T>* Single_List<T>::Create_List(const T s[], const int count)
{
Node_for_List<T>* ptemp;
for (int i = 0; i < count; i++)
{
ptemp = new Node_for_List<T>(s[i], NULL);
p->next = ptemp;
p = ptemp;
size++;
}
end = p=NULL;
return head;
}
template<typename T> //显示链表中数据
T Single_List<T>::Show_Data(Node_for_List<T>* ptr)
{
return ptr->data;
}
template<typename T> //链表排序(插入排序,从小到大)
Node_for_List<T>* Single_List<T>::Sort()
{
Node_for_List<T> *f, *t, *p = NULL, *q,*header;
header = head->next;
f = header->next;
header->next = NULL;
head->next = NULL;
while (f!=NULL)
{
for (t = f, q = header; (q != NULL && (q->data < t->data)); p = q, q = q->next);
f = f->next;
if (q == header) header = t;
else p->next = t;
t->next = q;
}
head->next = header;
return head;
}
template<typename T> //插入一个数据并重新排序
Node_for_List<T>* Single_List<T>::Insert_as_Order(const T item)
{
Node_for_List<T> *q, *p=NULL,*header;
Node_for_List<T>* ptr = new Node_for_List<T>(item, NULL);
header = head->next;
head->next = NULL;
for (q = header; (q != NULL && (q->data < item)); p = q, q = q->next);
if (q == header)
header = ptr;
else
p->next = ptr;
ptr->next = q;
size++;
head->next = header;
return head;
}
#endif
#include"List.h"
#include<iostream>
using namespace std;
int main()
{
int arr[] = { 16, 21, 2, 6, 45, 5, 26, 18, 8, 29 };
Single_List<int> singlelist;
Node_for_List<int> *point, *pt;
point=singlelist.Create_List(arr, 10);
for (pt=point->next; pt != NULL; pt = pt->next)
{
cout << singlelist.Show_Data(pt)<<" ";
}
cout << endl;
point = singlelist.Sort();
for (pt = point->next; pt != NULL; pt = pt->next)
{
cout << singlelist.Show_Data(pt) << " ";
}
cout << endl;
cout << "请输入您的一个数据:" << endl;
int a;
cin >> a;
point = singlelist.Insert_as_Order(a);
for (pt = point->next; pt != NULL; pt = pt->next)
{
cout << singlelist.Show_Data(pt) << " ";
}
cout << endl;
return 0;
}
代码已经过测试,在VS2013上成功运行!
发此文有两大目的:
1.和大家交流经验,供需要的人参考。
2.在下菜鸟,代码中难免有不妥之处,恳求大神批评指正。您的批评就是在下提高的起点,对于您的批评,在下将不胜感激!
代码功能:
1.链接存储方式实现单向链表(表头含有一个空结点作为哨位结点)
2.将另一数组中的数据读入该链表
3.链表数据排序(插入排序)
4.从外界插入一个数据,并使插入后的链表依然有序
5.实现过程中运用了模板
备注:按课程要求做的,有点糙。
*/
//.h文件
#ifndef LIST_H
#define LIST_H
#include<iostream>
template<typename T>
struct Node_for_List //声明结点
{
T data;
Node_for_List<T>* next;
Node_for_List(Node_for_List<T>* pnext = NULL){ next = pnext; } //构造空结点
Node_for_List(const T& item ) :data(item), next(NULL){};
Node_for_List(const T& item, Node_for_List<T>* pnext);
};
template<typename T> //单链表类
class Single_List
{
private:
Node_for_List<T> *head,*end,*p;
int size; //记录表结点个数
public:
Single_List();
Single_List(T& item);
~Single_List();
int Get_size()const;
bool If_Empty(){ return head->next == NULL; }
Node_for_List<T>* Create_List(const T s[], const int count);
T Show_Data(Node_for_List<T>* ptr);
Node_for_List<T>* Sort();
Node_for_List<T>* Insert_as_Order(const T item);
};
template<typename T> //构造表结点
Node_for_List<T>::Node_for_List(const T& item, Node_for_List<T>* pnext)
{
data = item;
next = pnext;
}
template<typename T> //无参构造函数,只创建含哨位结点的单链表
Single_List<T>::Single_List()
{
head = end = p = new Node_for_List<T>();
size = 0;
}
template<typename T> //构造函数,创建一个含哨位结点和一个表结点的单链表
Single_List<T>::Single_List(T& item)
{
p = end = new Node_for_List<T>(item);
head = new Node_for_List(p);
size = 1;
}
template<typename T> //析构函数
Single_List<T>::~Single_List()
{
while (!If_Empty())
{
p = head->next;
head->next = p->next;
delete p;
}
delete head;
}
template<typename T> //创建单链表
Node_for_List<T>* Single_List<T>::Create_List(const T s[], const int count)
{
Node_for_List<T>* ptemp;
for (int i = 0; i < count; i++)
{
ptemp = new Node_for_List<T>(s[i], NULL);
p->next = ptemp;
p = ptemp;
size++;
}
end = p=NULL;
return head;
}
template<typename T> //显示链表中数据
T Single_List<T>::Show_Data(Node_for_List<T>* ptr)
{
return ptr->data;
}
template<typename T> //链表排序(插入排序,从小到大)
Node_for_List<T>* Single_List<T>::Sort()
{
Node_for_List<T> *f, *t, *p = NULL, *q,*header;
header = head->next;
f = header->next;
header->next = NULL;
head->next = NULL;
while (f!=NULL)
{
for (t = f, q = header; (q != NULL && (q->data < t->data)); p = q, q = q->next);
f = f->next;
if (q == header) header = t;
else p->next = t;
t->next = q;
}
head->next = header;
return head;
}
template<typename T> //插入一个数据并重新排序
Node_for_List<T>* Single_List<T>::Insert_as_Order(const T item)
{
Node_for_List<T> *q, *p=NULL,*header;
Node_for_List<T>* ptr = new Node_for_List<T>(item, NULL);
header = head->next;
head->next = NULL;
for (q = header; (q != NULL && (q->data < item)); p = q, q = q->next);
if (q == header)
header = ptr;
else
p->next = ptr;
ptr->next = q;
size++;
head->next = header;
return head;
}
#endif
#include"List.h"
#include<iostream>
using namespace std;
int main()
{
int arr[] = { 16, 21, 2, 6, 45, 5, 26, 18, 8, 29 };
Single_List<int> singlelist;
Node_for_List<int> *point, *pt;
point=singlelist.Create_List(arr, 10);
for (pt=point->next; pt != NULL; pt = pt->next)
{
cout << singlelist.Show_Data(pt)<<" ";
}
cout << endl;
point = singlelist.Sort();
for (pt = point->next; pt != NULL; pt = pt->next)
{
cout << singlelist.Show_Data(pt) << " ";
}
cout << endl;
cout << "请输入您的一个数据:" << endl;
int a;
cin >> a;
point = singlelist.Insert_as_Order(a);
for (pt = point->next; pt != NULL; pt = pt->next)
{
cout << singlelist.Show_Data(pt) << " ";
}
cout << endl;
return 0;
}
代码已经过测试,在VS2013上成功运行!
发此文有两大目的:
1.和大家交流经验,供需要的人参考。
2.在下菜鸟,代码中难免有不妥之处,恳求大神批评指正。您的批评就是在下提高的起点,对于您的批评,在下将不胜感激!
相关文章推荐
- 数据结构学习系列三-单向循环链表(c++实现且应用模板)
- 数据结构C++模板实现之----------------单向链表
- Java数据结构之简单链表的定义与实现方法示例
- 数据结构之链表与数组(三)-单向链表上的简单操作
- 数据结构学习中的简单问题(一):用链表方式实现的队列输出杨辉三角
- C语言实现一个简单的单向链表list
- 数据结构——单向链表的链式存储及实现
- 简单数据结构――单向链表
- 和菜鸟一起学数据结构之简单单链表实现
- 数据结构学习的简单问题(三):用链表实现多项式相加
- C++类模板 实现双向循环链表的基本算法 《数据结构》(C++版 北京科海)中摘抄
- 数据结构的C实现_单向链表
- 简单单向链表(C++模版技术实现)
- 数据结构之链表与数组(三)-单向链表上的简单操作
- 单向链表的简单实现
- 数据结构之链表与数组(二) -单向链表上的简单操作问题
- 用c++实现简单的单向链表
- java数据结构之LinkedQueue(用链表实现的双端单向队列)
- C实现简单单向链表,一次遍历查找倒数第k个节点的值
- C++数据结构--单向循环链表(实现头插法,尾插法)