c++ 链表模板类的实现

合集下载

C语言链表数据结构实现

C语言链表数据结构实现

C语言链表数据结构实现链表是一种常见的数据结构,用于存储和操作数据集合。

它由节点(Node)组成,每个节点包含数据部分和指向下一个节点的指针。

在C语言中,我们可以使用指针来实现链表数据结构。

1. 定义节点结构体首先,我们需要定义链表的节点结构体。

节点结构体由数据部分和指向下一个节点的指针组成。

下面是一个示例:```typedef struct Node {int data;struct Node* next;} Node;```2. 创建链表接下来,我们可以编写创建链表的函数。

该函数将返回链表的头节点,并初始化为空。

示例代码如下:```Node* createLinkedList() {return NULL;}```3. 插入节点链表的插入操作包括在链表的任意位置插入新节点。

我们可以编写一个插入节点的函数,根据需求选择在链表的头部、尾部或指定位置插入节点。

示例代码如下:```void insertNode(Node** head, int data, int position) {Node* newNode = (Node*)malloc(sizeof(Node));newNode->data = data;if (position == 0) {newNode->next = *head;*head = newNode;} else {Node* current = *head;int currentPosition = 0;while (currentPosition < position - 1 && current != NULL) {current = current->next;currentPosition++;}if (current != NULL) {newNode->next = current->next;current->next = newNode;}}}```4. 删除节点链表的删除操作可以删除链表的任意节点。

C语言构建一个链表以及操作链表

C语言构建一个链表以及操作链表
p = Байду номын сангаас->pNext; }
return; }
int main(void) {
struct Node * pHead=NULL; //头指针(存放链表头结点地址) pHead = createList(); //构建一个链表 TraverseList(pHead); //操作链表 }
int i;
int val;
//用来临时存放用户输入的节点的值
struct Node * pHead = (struct Node *)malloc(sizeof(struct Node)); if (NULL == pHead) { printf("分配失败程序终止!\n"); exit(-1); } struct Node * pTail = pHead; pTail->pNext=NULL;
printf("分配失败程序终止!\n"); exit(-1); }
pNew->data = val; pTail->pNext = pNew; pNew->pNext = NULL; pTail = pNew; } return pHead; };
bool empty_list(struct Node * pHead) { if (pHead->pNext == NULL) { return true; }else{ return false; }
#include <stdio.h> #include <malloc.h> #include <stdlib.h>
struct Node{ int data; struct Node * pNext;

c++ 链表模板类的实现

c++ 链表模板类的实现

#include <iostream>#include <string>#include <assert.h>using namespace std;typedef struct Student{string name;int id;char sex;}Snode;typedef void* POS;template <class T>class CMyList{typedef struct _node{T node;_node *pNext;_node *pPrev;}NODE;public:POS AddTail(const T &node);//insert a node from the tail of the list;从链表尾插入一个节点POS AddHead(const T &node);//insert a node from the head of the list 从链表头插入一个节点T RemoveTail(); //从链表尾开始删除一个节点,尾向上移动,返回值是删除的节点的数据部分T RemoveHead();//从链表头开始删除一个节点,头往下移动,返回值是删掉的节点的数据部分T GetTail() const;//return the data of the tail node ;返回尾节点的数据T& GetTail();//返回尾节点的数据的引用T GetHead() const;//return the data of the head node ;返回头结点的数据T& GetHead();//返回头结点数据的一个引用T GetNext(POS &pos) const;//得到当前节点的下一个节点,并当前节点也变成了下一个节点返回数据部分T& GetNext(POS &pos);//得到当前节点的下一个节点,并当前节点也变成了下一个节点,返回数据部分的引用T GetPrev(POS &pos) const;//得到当前节点的前一个节点,并当前节点也变成前一个节点,返回前一个节点的数据部分T& GetPrev(POS &pos);//得到当前节点的前一个节点,并当前节点也变成前一个节点,返回值是前一个节点数据部分的引用POS GetHeadPosition() const;//return list head piont and (void *)head ;返回头指针并转换成void *类型POS GetTailPosition() const;//return list tail piont and (void *)tail ;返回尾指针并转换成void *类型T GetAt(POS pos) const;//return the data of the node to the position;返回当前节点的数据部分T& GetAt(POS pos);//返回当前节点的数据部分的一个引用POS InsertBefore(POS pos, const T &node);//new出一个节点并赋其数据部分以node,再把它插入pos节点之前,返回插入的位置POS InsertAfter(POS pos,const T &node);//new出一个节点并赋其数据部分以node,再把它插入到pos节点之后,返回插入的位置void SetAt(POS pos,T newnode);//为当前节点的数据部分赋值,数据部分不应含有指针类型的数据POS Find(T node,POS startAfter);//在某个位置之后查找一个数据部分和node 相等的节点,如果statAfter 为空那么从链表头开始查找,返回找到的位置void RemoveAll();// 删除链表中的所有节点,释放资源int GetCount() const;//得到整个链表的节点数bool IsEmpty() const;//测试list 是否为空void RemoveAt(POS pos);//删除pos 这个节点CMyList(); //初始化私有数据virtual ~CMyList();// 删除链表中的所有节点,释放资源private:NODE *m_pHead;NODE *m_pTail;int m_Count;};template <class T>CMyList<T>::CMyList(){m_pHead=NULL;m_pTail=NULL;m_Count=0;}template <class T>CMyList<T>::~CMyList(){RemoveAll();}template <class T>int CMyList<T>::GetCount() const{return m_Count;}template <class T>bool CMyList<T>::IsEmpty() const{return (m_Count == 0);}template <class T>T CMyList<T>::GetHead() const{assert(m_pHead != NULL);return m_pHead->node;}template <class T>T& CMyList<T>::GetHead(){assert(m_pHead != NULL);return m_pHead->node;}template <class T>T CMyList<T>::GetTail() const{assert(m_pTail != NULL);return m_pTail->node;}template <class T>T& CMyList<T>::GetTail(){assert(m_pTail != NULL);return m_pTail->node;}template <class T>POS CMyList<T>::GetHeadPosition() const {return (POS) m_pHead;}template <class T>POS CMyList<T>::GetTailPosition() const {return (POS) m_pTail;}template <class T>T CMyList<T>::GetNext(POS &pos) const {NODE* pNode = (NODE*) pos;pos = (POS) pNode->pNext;return pNode->node;}template <class T>T& CMyList<T>::GetNext(POS &pos){NODE* pNode = (NODE*) pos;pos = (POS) pNode->pNext;return pNode->node;}template <class T>T CMyList<T>::GetPrev(POS &pos) const {NODE* pNode = (NODE*) pos;pos = (POS) pNode->pPrev;return pNode->node;}template <class T>T& CMyList<T>::GetPrev(POS &pos){NODE* pNode = (NODE*) pos;pos = (POS) pNode->pPrev;return pNode->node;}template <class T>T CMyList<T>::GetAt(POS pos) const{NODE* pNode = (NODE*) pos;}template <class T>T& CMyList<T>::GetAt(POS pos){NODE* pNode = (NODE*) pos;return pNode->node;}template <class T>void CMyList<T>::SetAt(POS pos,T newnode) {NODE* pNode = (NODE*) pos;pNode->node=newnode;}template <class T>void CMyList<T>::RemoveAll(){assert(this!=NULL);NODE* pNode= m_pHead;while(pNode!=NULL){NODE* temp=pNode;pNode=pNode->pNext;delete temp;temp = NULL;}m_pHead=NULL;m_pTail=NULL;m_Count=0;}template <class T>POS CMyList<T>::AddHead(const T &node) {assert(this != NULL);NODE *pNewNode=new NODE;pNewNode->pPrev=NULL;pNewNode->pNext=m_pHead;if (m_pHead != NULL)m_pHead->pPrev = pNewNode;elsem_pTail = pNewNode;m_pHead = pNewNode;m_Count++;return (POS) pNewNode;}template <class T>POS CMyList<T>::AddTail(const T &node){assert(this != NULL);NODE *pNewNode=new NODE;pNewNode->pPrev=m_pTail;pNewNode->pNext=NULL;pNewNode->node = node;if (m_pTail != NULL)m_pTail->pNext = pNewNode;elsem_pHead = pNewNode;m_pTail = pNewNode;m_Count++;return (POS) pNewNode;}template <class T>T CMyList<T>::RemoveTail(){assert(this);assert(m_pTail != NULL); // don't call on empty list !!!NODE* pOldNode = m_pTail;T returnValue = pOldNode->node;m_pTail = pOldNode->pPrev;if (m_pTail != NULL)m_pTail->pNext = NULL;elsem_pHead = NULL;delete pOldNode;pOldNode = NULL;m_Count -- ;return returnValue;}template <class T>T CMyList<T>::RemoveHead(){assert(this);assert(m_pHead != NULL); // don't call on empty list !!!NODE* pOldNode = m_pHead;T returnValue = pOldNode->node;m_pHead = pOldNode->pNext;if (m_pHead != NULL)m_pHead->pPrev = NULL;elsem_pTail = NULL;delete pOldNode;pOldNode = NULL;m_Count -- ;return returnValue;}template <class T>void CMyList<T>::RemoveAt(POS pos){assert(this!=NULL);NODE* pOldNode = (NODE*) pos;// remove pOldNode from listif (pOldNode == m_pHead){m_pHead = pOldNode->pNext;}else{pOldNode->pPrev->pNext = pOldNode->pNext;}if (pOldNode == m_pTail){m_pTail = pOldNode->pPrev;}else{pOldNode->pNext->pPrev = pOldNode->pPrev;}delete pOldNode;pOldNode = NULL;m_Count--;}template <class T>POS CMyList<T>::InsertBefore(POS pos, const T &node){assert(this!=NULL);if (pos == NULL)return AddHead(node); // insert before nothing -> head of the list// Insert it before positionNODE* pOldNode = (NODE*) pos;NODE* pNewNode = new NODE;pNewNode->pPrev=pOldNode->pPrev;pNewNode->pNext=pOldNode;pNewNode->node = node;if (pOldNode->pPrev != NULL){pOldNode->pPrev->pNext = pNewNode;}else{assert(pOldNode == m_pHead);m_pHead = pNewNode;}pOldNode->pPrev = pNewNode;m_Count++;return (POS) pNewNode;}template <class T>POS CMyList<T>::InsertAfter(POS pos, const T &node){assert(this!=NULL);if (pos == NULL)return AddTail(node); // insert after nothing -> tail of the list// Insert it before positionNODE* pOldNode = (NODE*) pos;NODE* pNewNode =new NODE;pNewNode->pPrev=pOldNode;pNewNode->pNext=pOldNode->pNext;pNewNode->node = node;if (pOldNode->pNext != NULL){pOldNode->pNext->pPrev = pNewNode;}else{assert(pOldNode == m_pTail);m_pTail = pNewNode;}pOldNode->pNext = pNewNode;m_Count++;return (POS) pNewNode;}template <class T>POS CMyList<T>::Find(T node,POS startAfter){assert(this!=NULL);NODE* pNode = (NODE*) startAfter;if (pNode == NULL){pNode = m_pHead; // start at head}else{pNode = pNode->pNext; // start after the one specified }for (; pNode != NULL; pNode = pNode->pNext){if (pNode->node==node)return (POS)pNode;}return NULL;}int main(){Snode s,s1,s2,s3,s4;s.id = 10101; = "yanggentao1";s.sex = 'm';s1.id = 10102; = "yanggentao2";s1.sex = 'm';s2.id = 10103; = "yanggentao3";s2.sex = 'm';s3.id = 10104; = "yanggentao4";s3.sex = 'm';s4.id = 10105; = "yanggentao5";s4.sex = 'm';CMyList<Snode> stu_list;stu_list.AddTail(s);stu_list.AddTail(s1);stu_list.AddTail(s2);stu_list.AddTail(s3);stu_list.AddTail(s4);cout<<stu_list.GetCount()<<endl;POS pos2 = stu_list.GetHeadPosition();//cout<<stu_list.GetAt(stu_list.Find(s4,NULL)).name<<endl;cout<<stu_list.GetAt(pos2).name<<endl;stu_list.GetNext(pos2);cout<<stu_list.GetAt(pos2).name<<endl; stu_list.GetNext(pos2);cout<<stu_list.GetAt(pos2).name<<endl; stu_list.GetNext(pos2);cout<<stu_list.GetAt(pos2).name<<endl; stu_list.GetNext(pos2);cout<<stu_list.GetAt(pos2).name<<endl;POS pos = stu_list.GetHeadPosition(); if(pos == NULL)cout<<"null"<<endl;cout<<stu_list.GetAt(pos).name<<endl; Snode &s5 = stu_list.GetHead(); = "return & yin yong";cout<<stu_list.GetHead().name<<endl; Snode &s6 = stu_list.GetTail(); = "return tail & yinyong"; cout<<stu_list.GetTail().name<<endl; POS pos1 = stu_list.GetTailPosition();stu_list.GetPrev(pos1);cout<<stu_list.GetAt(pos1).name<<endl; stu_list.GetPrev(pos1);cout<<stu_list.GetAt(pos1).name<<endl; stu_list.GetPrev(pos1);cout<<stu_list.GetAt(pos1).name<<endl; stu_list.GetPrev(pos1);cout<<stu_list.GetAt(pos1).name<<endl; if(pos1 == NULL){cout<<"pos1 is NULL"<<endl;return 0;}Snode s7; = "zheshi s7 node";s7.id = 10107; s7.sex。

c语言结构体链表实现

c语言结构体链表实现

在C语言中,可以使用结构体和链表来实现一个简单的链表。

以下是一个示例:c复制代码#include<stdio.h>#include<stdlib.h>// 定义链表节点结构体typedef struct Node {int data;struct Node* next;} Node;// 定义链表结构体typedef struct LinkedList {Node* head;Node* tail;} LinkedList;// 创建新节点Node* createNode(int data) {Node* newNode = (Node*)malloc(sizeof(Node));if(newNode == NULL) {printf("Memory allocation failed\n");exit(0);}newNode->data = data;newNode->next = NULL;return newNode;}// 在链表末尾添加节点void addNode(LinkedList* list, int data) {Node* newNode = createNode(data);if(list->head == NULL) {list->head = newNode;list->tail = newNode;} else {list->tail->next = newNode;list->tail = newNode;}}// 打印链表中的所有节点void printList(LinkedList* list) {Node* temp = list->head;while(temp != NULL) {printf("%d -> ", temp->data);temp = temp->next;}printf("NULL\n");}int main() {LinkedList list;list.head = NULL;list.tail = NULL;addNode(&list, 1); // 在链表末尾添加节点1addNode(&list, 2); // 在链表末尾添加节点2addNode(&list, 3); // 在链表末尾添加节点3printList(&list); // 打印链表中的所有节点,输出应为3 -> 2 -> 1 -> NULLreturn0;}在这个示例中,我们首先定义了两个结构体,一个用于表示链表中的节点,另一个用于表示整个链表。

链表的C语言实现

链表的C语言实现

链表的C语言实现分类:计算机学习2006.12.29 09:06 作者:ybxycy | 评论:0 | 阅读:652数组作为存放同类数据的集合,给我们在程序设计时带来很多的方便,增加了灵活性。

但数组也同样存在一些弊病。

如数组的大小在定义时要事先规定,不能在程序中进行调整,这样一来,在程序设计中针对不同问题有时需要3 0个大小的数组,有时需要5 0个数组的大小,难于统一。

我们只能够根据可能的最大需求来定义数组,常常会造成一定存储空间的浪费。

我们希望构造动态的数组,随时可以调整数组的大小,以满足不同问题的需要。

链表就是我们需要的动态数组。

它是在程序的执行过程中根据需要有数据存储就向系统要求申请存储空间,决不构成对存储区的浪费。

链表是一种复杂的数据结构,其数据之间的相互关系使链表分成三种:单链表、循环链表、双向链表,下面将逐一介绍。

7.4.1 单链表图7 - 3是单链表的结构。

单链表有一个头节点h e a d,指向链表在内存的首地址。

链表中的每一个节点的数据类型为结构体类型,节点有两个成员:整型成员(实际需要保存的数据)和指向下一个结构体类型节点的指针即下一个节点的地址(事实上,此单链表是用于存放整型数据的动态数组)。

链表按此结构对各节点的访问需从链表的头找起,后续节点的地址由当前节点给出。

无论在表中访问那一个节点,都需要从链表的头开始,顺序向后查找。

链表的尾节点由于无后续节点,其指针域为空,写作为N U L L。

图7 - 3还给出这样一层含义,链表中的各节点在内存的存储地址不是连续的,其各节点的地址是在需要时向系统申请分配的,系统根据内存的当前情况,既可以连续分配地址,也可以跳跃式分配地址。

看一下链表节点的数据结构定义:struct node{int num;struct node *p;} ;在链表节点的定义中,除一个整型的成员外,成员p是指向与节点类型完全相同的指针。

在链表节点的数据结构中,非常特殊的一点就是结构体内的指针域的数据类型使用了未定义成功的数据类型。

c 模板的实现原理

c 模板的实现原理

c 模板的实现原理
C模板的实现原理是通过预处理器和编译器进行完成的。

在C 语言中,模板的实现主要是通过宏定义和宏展开来实现的。

首先,在C语言中,我们可以使用宏定义来定义一个通用的模板。

这个模板可以包含参数,通过使用特定的占位符表示。

例如,我们可以定义一个通用的泛型数组类型:
```c
#define ARRAY(T) struct { T* data; int size; }
```
这里的T就是我们的参数,表示数组元素的类型。

通过定义这个宏,我们可以根据不同的类型来创建相应的数组类型。

例如,我们可以使用ARRAY(int)来定义一个整型数组类型。

接下来,当我们使用这个宏进行编译时,预处理器会将宏展开为相应的代码。

例如,当我们使用ARRAY(int)时,预处理器会将宏展开为:
```c
struct { int* data; int size; }
```
预处理器会将宏展开到所有使用这个宏的地方,从而生成相应的代码。

然后,编译器会对展开后的代码进行编译和优化,生成最终的
可执行文件。

总结起来,C模板的实现原理是通过预处理器进行宏展开来实现的。

预处理器会根据定义的宏来替换相应的代码。

这样就可以根据不同的参数,生成不同的代码,实现泛型编程的效果。

用C语言结构体类型指针实现数据结构链表操作

用C语言结构体类型指针实现数据结构链表操作

用C语言结构体类型指针实现数据结构链表操作我很抱歉,作为AI语言模型,不支持写长篇大论的文章。

但是我可以给你提供一个简单的链表实现,供参考。

链表是一种常用的数据结构,它可以动态地存储和组织数据。

链表由一个节点列表组成,每个节点包含了存储的数据和一个指向下一个节点的指针。

链表最常见的操作有添加,删除和查找。

C语言结构体类型指针是实现链表的一种常见方法。

这种方法利用结构体类型的成员变量指向自身的特性,将每个节点作为一个结构体类型,通过指向下一个结构体类型的指针实现节点之间的链接。

首先,我们定义一个结构体类型来表示链表节点:```typedef struct Node{int data;struct Node* next;}Node;```这个结构体类型包含了一个整型数据成员和一个指向下一个节点的指针成员。

可以看出,这个指针成员实现了链表结构的关键。

接下来,我们定义链表的常见操作函数。

## 添加节点添加节点操作可以分为在头部和在尾部添加。

在头部添加时,我们需要注意链表头部指针的变化。

```void add_head(Node** head, int data){Node* new_node = (Node*)malloc(sizeof(Node));new_node->data = data;new_node->next = (*head);(*head) = new_node;}void add_tail(Node** head, int data){Node* new_node = (Node*)malloc(sizeof(Node));new_node->data = data;new_node->next = NULL;if(*head == NULL){*head = new_node;return;}Node* tail = *head;while(tail->next != NULL){tail = tail->next;}tail->next = new_node;}```添加头部节点时,我们首先新建一个节点,然后把它的next指针指向原链表头部指针指向的节点,并且把链表头部指针指向新节点。

线性表--链表(C语言实现)

线性表--链表(C语言实现)

线性表--链表(C语⾔实现)线性表是最基本的⼀种数据结构,是0个以上数据元素的有限序列。

由线性表的定义,我们可以得出线性表的两个特性。

⾸先它是⼀个序列,即元素之间是有序的。

除了第⼀个元素外,每⼀个元素都有且只有⼀个直接前驱元素;除了最后⼀个元素外,每⼀个元素都有且只有⼀个直接后驱元素。

也就是说,元素之间是⼀对⼀连接起来的。

其次它的元素是有限的,最少为0个,当元素为0个时,称之为空表。

根据线性表的序列特性,计算机可以⽤两种⽅法来实现线性表:1、开辟⼀块连续的内存,将数据元素⼀个⼀个放⼊进去,说⽩了也就是⽤数组来实现线性表。

这种实现⽅式对于读数据来说⽐较快,但对于插⼊和删除来说,就⽐较尴尬了。

⼀般来说,不会⽤这种⽅法实现线性表这种数据结构。

2、链表。

(重点来了)链表是⼀种递归的数据结构。

链表中的元素为结点,结点由两部分组成,⼀是存储元素值的数据域、⼆是存储指向下⼀个结点地址的指针域(或者是NULL),如果该结点的指针域为NULL,则称该结点为尾结点,也就是链表的结尾。

以下简单的代码让同学们更加直观的感受⼀下链表。

#include<stdio.h>#include<malloc.h>#include<stdlib.h>typedef int ElemType;typedef struct node{ ElemType data; struct node* next;}Node, *PNode;// 声明创建链表函数PNode CreateList(void);// 声明遍历链表函数void PrintfList(PNode List);int main(){ PNode List = CreateList(); PrintfList(List); getchar(); return 0;}PNode CreateNode(){ PNode newNode = (PNode)malloc(sizeof(Node)); if (newNode == NULL) { printf("out of memory.\n"); exit(1); } newNode->next = NULL; return newNode;}PNode CreateList(void){ int length; //链表个数,作为循环结束的条件 ElemType data; //链表结点数据域的值 PNode Head = CreateNode(); PNode s = Head; printf("输⼊链表个数:"); scanf_s("%d", &length); for (int i = 1; i <= length; i++){ PNode P = CreateNode(); printf("第%d个结点的data值:", i); scanf_s("%d", &data); while (s->next) { s = s->next; } P->data = data; s->next = P; } getchar(); printf("创建链表成功\n"); return Head;}void PrintfList(PNode List){ PNode P = List->next; printf("链表为:"); if (P == NULL) { printf("链表为空"); } while (P != NULL){ printf("%d", P->data); P = P->next; } printf("\n");}。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include <iostream>#include <string>#include <assert.h>using namespace std;typedef struct Student{string name;int id;char sex;}Snode;typedef void* POS;template <class T>class CMyList{typedef struct _node{T node;_node *pNext;_node *pPrev;}NODE;public:POS AddTail(const T &node);//insert a node from the tail of the list;从链表尾插入一个节点POS AddHead(const T &node);//insert a node from the head of the list 从链表头插入一个节点T RemoveTail(); //从链表尾开始删除一个节点,尾向上移动,返回值是删除的节点的数据部分T RemoveHead();//从链表头开始删除一个节点,头往下移动,返回值是删掉的节点的数据部分T GetTail() const;//return the data of the tail node ;返回尾节点的数据T& GetTail();//返回尾节点的数据的引用T GetHead() const;//return the data of the head node ;返回头结点的数据T& GetHead();//返回头结点数据的一个引用T GetNext(POS &pos) const;//得到当前节点的下一个节点,并当前节点也变成了下一个节点返回数据部分T& GetNext(POS &pos);//得到当前节点的下一个节点,并当前节点也变成了下一个节点,返回数据部分的引用T GetPrev(POS &pos) const;//得到当前节点的前一个节点,并当前节点也变成前一个节点,返回前一个节点的数据部分T& GetPrev(POS &pos);//得到当前节点的前一个节点,并当前节点也变成前一个节点,返回值是前一个节点数据部分的引用POS GetHeadPosition() const;//return list head piont and (void *)head ;返回头指针并转换成void *类型POS GetTailPosition() const;//return list tail piont and (void *)tail ;返回尾指针并转换成void *类型T GetAt(POS pos) const;//return the data of the node to the position;返回当前节点的数据部分T& GetAt(POS pos);//返回当前节点的数据部分的一个引用POS InsertBefore(POS pos, const T &node);//new出一个节点并赋其数据部分以node,再把它插入pos节点之前,返回插入的位置POS InsertAfter(POS pos,const T &node);//new出一个节点并赋其数据部分以node,再把它插入到pos节点之后,返回插入的位置void SetAt(POS pos,T newnode);//为当前节点的数据部分赋值,数据部分不应含有指针类型的数据POS Find(T node,POS startAfter);//在某个位置之后查找一个数据部分和node 相等的节点,如果statAfter 为空那么从链表头开始查找,返回找到的位置void RemoveAll();// 删除链表中的所有节点,释放资源int GetCount() const;//得到整个链表的节点数bool IsEmpty() const;//测试list 是否为空void RemoveAt(POS pos);//删除pos 这个节点CMyList(); //初始化私有数据virtual ~CMyList();// 删除链表中的所有节点,释放资源private:NODE *m_pHead;NODE *m_pTail;int m_Count;};template <class T>CMyList<T>::CMyList(){m_pHead=NULL;m_pTail=NULL;m_Count=0;}template <class T>CMyList<T>::~CMyList(){RemoveAll();}template <class T>int CMyList<T>::GetCount() const{return m_Count;}template <class T>bool CMyList<T>::IsEmpty() const{return (m_Count == 0);}template <class T>T CMyList<T>::GetHead() const{assert(m_pHead != NULL);return m_pHead->node;}template <class T>T& CMyList<T>::GetHead(){assert(m_pHead != NULL);return m_pHead->node;}template <class T>T CMyList<T>::GetTail() const{assert(m_pTail != NULL);return m_pTail->node;}template <class T>T& CMyList<T>::GetTail(){assert(m_pTail != NULL);return m_pTail->node;}template <class T>POS CMyList<T>::GetHeadPosition() const {return (POS) m_pHead;}template <class T>POS CMyList<T>::GetTailPosition() const {return (POS) m_pTail;}template <class T>T CMyList<T>::GetNext(POS &pos) const {NODE* pNode = (NODE*) pos;pos = (POS) pNode->pNext;return pNode->node;}template <class T>T& CMyList<T>::GetNext(POS &pos){NODE* pNode = (NODE*) pos;pos = (POS) pNode->pNext;return pNode->node;}template <class T>T CMyList<T>::GetPrev(POS &pos) const {NODE* pNode = (NODE*) pos;pos = (POS) pNode->pPrev;return pNode->node;}template <class T>T& CMyList<T>::GetPrev(POS &pos){NODE* pNode = (NODE*) pos;pos = (POS) pNode->pPrev;return pNode->node;}template <class T>T CMyList<T>::GetAt(POS pos) const{NODE* pNode = (NODE*) pos;}template <class T>T& CMyList<T>::GetAt(POS pos){NODE* pNode = (NODE*) pos;return pNode->node;}template <class T>void CMyList<T>::SetAt(POS pos,T newnode) {NODE* pNode = (NODE*) pos;pNode->node=newnode;}template <class T>void CMyList<T>::RemoveAll(){assert(this!=NULL);NODE* pNode= m_pHead;while(pNode!=NULL){NODE* temp=pNode;pNode=pNode->pNext;delete temp;temp = NULL;}m_pHead=NULL;m_pTail=NULL;m_Count=0;}template <class T>POS CMyList<T>::AddHead(const T &node) {assert(this != NULL);NODE *pNewNode=new NODE;pNewNode->pPrev=NULL;pNewNode->pNext=m_pHead;if (m_pHead != NULL)m_pHead->pPrev = pNewNode;elsem_pTail = pNewNode;m_pHead = pNewNode;m_Count++;return (POS) pNewNode;}template <class T>POS CMyList<T>::AddTail(const T &node){assert(this != NULL);NODE *pNewNode=new NODE;pNewNode->pPrev=m_pTail;pNewNode->pNext=NULL;pNewNode->node = node;if (m_pTail != NULL)m_pTail->pNext = pNewNode;elsem_pHead = pNewNode;m_pTail = pNewNode;m_Count++;return (POS) pNewNode;}template <class T>T CMyList<T>::RemoveTail(){assert(this);assert(m_pTail != NULL); // don't call on empty list !!!NODE* pOldNode = m_pTail;T returnValue = pOldNode->node;m_pTail = pOldNode->pPrev;if (m_pTail != NULL)m_pTail->pNext = NULL;elsem_pHead = NULL;delete pOldNode;pOldNode = NULL;m_Count -- ;return returnValue;}template <class T>T CMyList<T>::RemoveHead(){assert(this);assert(m_pHead != NULL); // don't call on empty list !!!NODE* pOldNode = m_pHead;T returnValue = pOldNode->node;m_pHead = pOldNode->pNext;if (m_pHead != NULL)m_pHead->pPrev = NULL;elsem_pTail = NULL;delete pOldNode;pOldNode = NULL;m_Count -- ;return returnValue;}template <class T>void CMyList<T>::RemoveAt(POS pos){assert(this!=NULL);NODE* pOldNode = (NODE*) pos;// remove pOldNode from listif (pOldNode == m_pHead){m_pHead = pOldNode->pNext;}else{pOldNode->pPrev->pNext = pOldNode->pNext;}if (pOldNode == m_pTail){m_pTail = pOldNode->pPrev;}else{pOldNode->pNext->pPrev = pOldNode->pPrev;}delete pOldNode;pOldNode = NULL;m_Count--;}template <class T>POS CMyList<T>::InsertBefore(POS pos, const T &node){assert(this!=NULL);if (pos == NULL)return AddHead(node); // insert before nothing -> head of the list// Insert it before positionNODE* pOldNode = (NODE*) pos;NODE* pNewNode = new NODE;pNewNode->pPrev=pOldNode->pPrev;pNewNode->pNext=pOldNode;pNewNode->node = node;if (pOldNode->pPrev != NULL){pOldNode->pPrev->pNext = pNewNode;}else{assert(pOldNode == m_pHead);m_pHead = pNewNode;}pOldNode->pPrev = pNewNode;m_Count++;return (POS) pNewNode;}template <class T>POS CMyList<T>::InsertAfter(POS pos, const T &node){assert(this!=NULL);if (pos == NULL)return AddTail(node); // insert after nothing -> tail of the list// Insert it before positionNODE* pOldNode = (NODE*) pos;NODE* pNewNode =new NODE;pNewNode->pPrev=pOldNode;pNewNode->pNext=pOldNode->pNext;pNewNode->node = node;if (pOldNode->pNext != NULL){pOldNode->pNext->pPrev = pNewNode;}else{assert(pOldNode == m_pTail);m_pTail = pNewNode;}pOldNode->pNext = pNewNode;m_Count++;return (POS) pNewNode;}template <class T>POS CMyList<T>::Find(T node,POS startAfter){assert(this!=NULL);NODE* pNode = (NODE*) startAfter;if (pNode == NULL){pNode = m_pHead; // start at head}else{pNode = pNode->pNext; // start after the one specified }for (; pNode != NULL; pNode = pNode->pNext){if (pNode->node==node)return (POS)pNode;}return NULL;}int main(){Snode s,s1,s2,s3,s4;s.id = 10101; = "yanggentao1";s.sex = 'm';s1.id = 10102; = "yanggentao2";s1.sex = 'm';s2.id = 10103; = "yanggentao3";s2.sex = 'm';s3.id = 10104; = "yanggentao4";s3.sex = 'm';s4.id = 10105; = "yanggentao5";s4.sex = 'm';CMyList<Snode> stu_list;stu_list.AddTail(s);stu_list.AddTail(s1);stu_list.AddTail(s2);stu_list.AddTail(s3);stu_list.AddTail(s4);cout<<stu_list.GetCount()<<endl;POS pos2 = stu_list.GetHeadPosition();//cout<<stu_list.GetAt(stu_list.Find(s4,NULL)).name<<endl;cout<<stu_list.GetAt(pos2).name<<endl;stu_list.GetNext(pos2);cout<<stu_list.GetAt(pos2).name<<endl; stu_list.GetNext(pos2);cout<<stu_list.GetAt(pos2).name<<endl; stu_list.GetNext(pos2);cout<<stu_list.GetAt(pos2).name<<endl; stu_list.GetNext(pos2);cout<<stu_list.GetAt(pos2).name<<endl;POS pos = stu_list.GetHeadPosition(); if(pos == NULL)cout<<"null"<<endl;cout<<stu_list.GetAt(pos).name<<endl; Snode &s5 = stu_list.GetHead(); = "return & yin yong";cout<<stu_list.GetHead().name<<endl; Snode &s6 = stu_list.GetTail(); = "return tail & yinyong"; cout<<stu_list.GetTail().name<<endl; POS pos1 = stu_list.GetTailPosition();stu_list.GetPrev(pos1);cout<<stu_list.GetAt(pos1).name<<endl; stu_list.GetPrev(pos1);cout<<stu_list.GetAt(pos1).name<<endl; stu_list.GetPrev(pos1);cout<<stu_list.GetAt(pos1).name<<endl; stu_list.GetPrev(pos1);cout<<stu_list.GetAt(pos1).name<<endl; if(pos1 == NULL){cout<<"pos1 is NULL"<<endl;return 0;}Snode s7; = "zheshi s7 node";s7.id = 10107; s7.sex。

相关文档
最新文档