[c++] list with head node
2016-10-25 20:29
302 查看
/* * g++ ListTest.cpp -o L * valgrind ./L */ #include <stdio.h> #include <stdlib.h> struct ListNode{ ListNode* pNext; int Value; }; class IntList{ public: IntList() throw(){ m_Front.pNext=NULL; m_Front.Value=-1; m_pRear = &m_Front; } ~IntList() throw(){ ListNode *p = m_Front.pNext; while(p!=NULL){ m_Front.pNext=p->pNext; free(p); p=m_Front.pNext; } } bool Empty() const throw() {return m_Front.pNext==NULL;} void Push(int v) throw(){ ListNode *p = (ListNode*) malloc(sizeof(ListNode)); if(p!=NULL){ p->pNext=NULL; p->Value=v; m_pRear->pNext=p; m_pRear=p; } } int Front() const throw(){ if(m_Front.pNext!=NULL) return m_Front.pNext->Value; else return -1; } int Rear() const throw(){return m_pRear->Value;} void Pop() throw(){ ListNode *p=m_Front.pNext; if(p!=NULL){ m_Front.pNext=p->pNext; free(p); if(m_Front.pNext==NULL) m_pRear=&m_Front; } } private: ListNode m_Front; ListNode* m_pRear; }; int main() { IntList int_list; int index=0; for(int i=0;i<100;++i) int_list.Push(i); while(int_list.Empty()==false){ if(int_list.Front()!=index){ printf("Error, index=%d,Front()=%d",index,int_list.Front()); } printf("value = %d\n",int_list.Front()); index = int_list.Front()+1; int_list.Pop(); } for(int i=0;i<100;++i) int_list.Push(i); return 0; }
相关文章推荐
- 1006. insert for single link list with head node
- 1007. remove for single link list with head node
- Linux内核 hlist_head/hlist_node结构解析
- Linux内核 hlist_head/hlist_node结构解析
- leetcode 237 Delete Node in a Linked List C++
- leetcode_c++:链表:Remove Nth Node From End of List(019)
- leetcode_c++:哈希: Copy List with Random Pointer(138)
- List with C++
- 【LeetCode-19】Remove Nth Node From End of List(C++)
- hlist_head/hlist_node
- LeetCode 之 Delete Node in a Linked List — C++ 实现
- Linux内核 hlist_head/hlist_node结构解析
- Linux内核 hlist_head/hlist_node结构解析
- **Leetcode_copy-list-with-random-pointer (c++ and python version)
- Leetcode_remove-nth-node-from-end-of-list(c++ and python version)
- hlist_head / hlist_node 设计浅析
- c++ X STL - Traverse List and Delete Node , Error list iterator not incrementable
- LeetCode 19 — Remove Nth Node From End of List(C++ Java Python)
- C++: delete a node from linked list
- [C++]List with iterator(链表的迭代器实现)