#include<iostream>
using namespace std;
typedef int elemType;
template <typename elemType>
class linkList {
private:
struct Node //结点类型
{
elemType data; //结点数据域
Node* next; //结点指针域
Node(const elemType value, Node* p = NULL) {
data = value;
next = p;
}
Node(Node* p = NULL) {
next = p;
}
};
Node* head; //链表头结点
Node* tail; //链表尾结点
int curLength; //单链表当前长度,
Node* getPosition(int i) const; //返回指向位序为i的结点的指针
public:
linkList();
~linkList();
void clear(); //清空单链表,使之成为空表
bool empty() const {
//带有头结点的单链表,判空
return head->next == NULL;
}
int size() const {
//返回单链表的当前实际长度
return curLength;
}
void insert(int i, const elemType& value); //位序i处插入值为value的结点,表长加1
void remove(int i); //移除位序为i处的结点,表长减1
int search(const elemType& value) const; //查找值为value的结点第一次出现的位序
int prior(const elemType& value) const; //查找值为value的结点的前驱的位序
void traverse() const; //遍历单链表
void headCreate(); //头插法建立单链表
void tailCreate(); //尾插法建立单链表
void inverse(); //逆置单链表
linkList<elemType>* Union(linkList<elemType>* lb); //合并单链表
};
/*
查找位序为i的结点的内存地址(思想):
1.首先检查要查找位序的合法性,若非法返回NULL,i==0表示查找首元结点,i==-1表示查找头结点
2.设置工作指针,指向头结点,设置计数器,进行计数,知道p指向位序为i的结点
*/
template<class elemType>
typename linkList<elemType>::Node* linkList<elemType>::getPosition(int i) const
{
if (i <-1 || i > curLength-1) {
//合法检查范围为[-1...curLength-1]
return NULL;
}
Node
数据结构之单链表基本操作(C++)
最新推荐文章于 2024-04-23 16:50:23 发布