本程序主要实现循环链表的插入,删除,输出操作
#ifndef _List_H
#define _List_H
#include
template class List;
template
class ListNode{
friend class List;
private:
T data;
ListNode *link;
ListNode(T);
ListNode(){}
};
template
class List{
public:
List(){first = new ListNode; first->link = first;}
void InSert(T);
void Delete(T);
void Show();
private:
ListNode *first;
};
template
ListNode::ListNode(T element){
data = element;
link = 0;
}
template
void List::InSert(T k){
ListNode *newNode = new ListNode(k);
newNode->link = first->link;
first->link = newNode;
}
template
void List::Show(){
for(ListNode *current = first->link; current->link != first; current = current->link){
std::cout<data;
if(current)
std::cout<<" ";
}
std::cout<<std::endl;
}
template
void List::Delete(T k){
ListNode *previous = first;
ListNode *current ;
for(current=first->link;(current != first)&&(current->data !=k);
previous = current,current = current->link){
;
}
if (current != first)
{
previous->link = current->link;
delete current;
}
}
#endif
本文深入探讨了循环链表的基本操作,包括元素的插入、删除和显示。通过具体代码实现,详细讲解了如何在循环链表中进行节点的管理,为读者提供了清晰的算法思路和技术实践指导。





