您的位置:首页 > 理论基础 > 数据结构算法

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.在下菜鸟,代码中难免有不妥之处,恳求大神批评指正。您的批评就是在下提高的起点,对于您的批评,在下将不胜感激!


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