标签:let ons cst 遍历 list.sh app end 链式 单循环链表
#include<iostream> #include<cstdlib> #include<stdexcept> using namespace std; struct ListNode { int element; ListNode* next; ListNode(int e=0,ListNode* nxt=0):element(e),next(nxt) { } }; class CircleList { private: ListNode headNode; //头结点 ListNode* p_rear; //尾结点指针 int size; //表长度 public: CircleList():headNode(0,0),p_rear(0),size(0) { p_rear = &headNode; //尾结点指针初始化时也保存头结点的地址 headNode.next = &headNode; //形成环 } ~CircleList() { ListNode * p = headNode.next; ListNode *t ; while( p!= &headNode ) { t = p; p = p->next; delete t; } } void append(int e) { ListNode *new_node = new ListNode(e,&headNode); //新结点创建 ,新结点的next指向头结点。 p_rear->next = new_node; p_rear = new_node; size++; } void show()const { ListNode *p = headNode.next; cout<<‘[‘; while(p!=&headNode) { if(p!=headNode.next) cout<<‘,‘; cout<<p->element; p = p->next; } cout<<‘]‘; } }; int main() { CircleList clist; clist.append(12); clist.append(13); clist.append(14); clist.append(15); clist.append(16); clist.show(); }
标签:let ons cst 遍历 list.sh app end 链式 单循环链表
原文地址:http://www.cnblogs.com/lulipro/p/7420936.html