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

用C++模板实现线性表的链式存储的基本算法(数据结构C++版 北京科海)

2012-11-07 11:06 856 查看
/*
文件名称:LinkList.h
文件内容:线性链表的定义
*/

#ifndef LINKLIST_H
#define LINKLIST_H

#include <iostream>
using namespace std;

template<class T> class LinkList;

//LNode类模板定义
template<class T>
class LNode
{
friend class LinkList<T>;
public:
LNode():next(NULL)
{

}

LNode(const T& item):data(item),next(NULL)
{

}

LNode(const T& item, LNode<T>* Next)
{
data = item;
next = Next;
}

~LNode()
{

}

public:
LNode<T>* Next();
void InsertAfter(LNode<T> *p);
LNode<T>* GetNode(const T& item, LNode<T>* Next);
LNode<T>* RemoveAfter();
private:
T data;
LNode<T> *next;

};

/*************************************************************/
//LinlList类模板定义
template<class T>
class LinkList
{
public:
LinkList()
{
tail = head = new LNode<T>(0);
}

LinkList(const T& val)
{
tail = head = new LNode<T>(val);
}

~LinkList()
{
Empty();
delete head;
}

public:
void Empty();
int LeadLength() const;
int NoLeadLength(LinkList L);
LNode<T>* FindByVal(T val);
LNode<T>* FindByIndex(int i);
void AddToHead(T val);
void AddToTail(T val);
int Insert(T val, int i);
T* Remove(int i);
T* Get(int i);
void Print();

private:
LNode<T>* head;
LNode<T>* tail;

};

#endif
/*************************************************************/
/*LNode类的操作实现程序*/
template<class T>
LNode<T>* LNode<T>::Next()
{
return next;
}

template<class T>
void LNode<T>::InsertAfter(LNode<T> *p)
{
p->next = next;
next = p;
}

template<class T>
LNode<T>* LNode<T>::GetNode(const T& item, LNode<T>* Next)
{
LNode<T>* newNode = new LNode<T>(item);
newNode->next = Next;
return newNode;
}

template<class T>
LNode<T>* LNode<T>::RemoveAfter()
{
LNode<T>* tempNodePtr = next;

if (next == NULL)
{
return NULL;
}

next = tempNodePtr->next;
return next;
}

/*************************************************************/
/*LinkList类的操作实现程序*/
//清空链表
template<class T>
void LinkList<T>::Empty()
{
LNode<T>* temp;

while (head->next != NULL)
{
temp = head->next;
head->next = temp->next;
delete temp;
}

tail = head;
}

//带头结点的单链表的表长
template<class T>
int LinkList<T>::LeadLength() const
{
int nlen = 0;
LNode<T>* temp = head->next;

while (temp)
{
temp = temp->next;
nlen++;
}

return nlen;
}

//不带头结点的单链表的表长
template<class T>
int LinkList<T>::NoLeadLength(LinkList L)
{
LNode* p = L;
if(p == NULL) return 0;
int j = 1;

while (p->next)
{
p = p->next;
j++;
}

return j;
}

//数据定位查找
template<class T>
LNode<T>* LinkList<T>::FindByVal(T val)
{
LNode<T>* temp = head->next;
while (temp && temp->data!=val)
{
temp = temp->next;
}
return temp;
}

//按序号查找
template<class T>
LNode<T>* LinkList<T>::FindByIndex(int i)
{
if(i<-1) return NULL;
if(i==-1) return head;
LNode<T>* temp = head;
int j = 0;

while(temp && (j<i))
{
temp = temp->next;
j++;
}

return temp;
}

/*
后插和前插(实更关心数据元素之间的逻辑关系,那么对于前插可以按后插操作,然后再数据交换)
*/

//在链表的头部插入结点建立单链表
template<class T>
void LinkList<T>::AddToHead(T val)
{
LNode<T> *newNode = new LNode<T>(val);
newNode->next = head->next;
head->next = newNode;

LNode<T> *p = head;

while (p->next != NULL)
{
p = p->next;
}
tail = p;

}

//在链表的尾部插入结点建立单链表
template<class T>
void LinkList<T>::AddToTail(T val)
{
LNode<T> *newNode = new LNode<T>(val);
tail->next = newNode;
tail = newNode;
}

//插入运算
template<class T>
int LinkList<T>::Insert(T val, int i)
{
LNode<T>* temp = FindByIndex(i-1);

if (temp==NULL)
{
return 0;
}

LNode<T>* newNode = new LNode<T>(val, temp->next);

if (temp->next==NULL)
{
tail = newNode;
}

temp->next = newNode;
return 1;
}

//删除结点
template<class T>
T* LinkList<T>::Remove(int i)
{
LNode<T> *q;
LNode<T> *p = FindByIndex(i-1);

if (p==NULL || p->next == NULL)
{
return NULL;
}

q = p->next;
p->next = q->next;
T *val = new T(q->data);

if (q == tail)
{
tail = p;
}

delete q;
return val;
}

//得到第i个结点
template<class T>
T* LinkList<T>::Get(int i)
{
LNode<T>* p = FindByIndex(i);

if (p==NULL || p==head)
{
return NULL;
}else
{
return &(p->data);
}

}

template<class T>
void LinkList<T>::Print()
{
LNode<T>* temp = head->next;
cout << "共有:" << LeadLength() << "个节点" << endl;

while (temp)
{
cout << temp->data << " ";
temp = temp->next;
}

cout << endl;
}

//测试

#include "LinkList.h"

int main()
{
LinkList<int> ls;
ls.AddToHead(2);
ls.AddToHead(4);
ls.AddToHead(8);
ls.Print();
cout << "--------------------------------" << endl;

ls.AddToTail(11);
ls.AddToTail(18);
ls.AddToTail(20);
ls.Print();
cout << "-------------------------------" << endl;

ls.Insert(100, 4);
ls.Insert(101,5);
ls.Print();
cout << "-------------------------------" << endl;

int *data = ls.Remove(1);
cout << "删除index=1的数据:" << *data << endl;
ls.Print();

int *temp = ls.Get(3);
cout << "index=3的数据:" << *temp << endl;

cout << "-------------------------------" << endl;
LNode<int> *addr_val = ls.FindByVal(100);
cout << "value=100的地址:" << addr_val << endl;

LNode<int> *addr_index = ls.FindByIndex(2);
cout << "index=2的地址:" << addr_index << endl;

ls.Print();

ls.Empty();
ls.Print();

system("pause");
return 0;
}


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