CareerCup Program an iterator for a Linked List which may include nodes which are nested within othe
来源:互联网 发布:中网络电视台 编辑:程序博客网 时间:2024/05/17 09:11
Program an iterator for a Linked List which may include nodes which are nested within other nodes. i.e. (1)->(2)->(3(4))->((5)(6). Iterator returns 1->2->3->4->5->6
-------------------------------------------------------------
C++ solution. This one also uses a stack, similar to previous answers
I omitted some code which is not relevant to the question itself, as well as some "friend" commands to make it more readable.
NestedNode contains a value and two pointers: to the nested element and to the next element.
template <class C>class NestedNode{public:NestedNode(C value, NestedNode<C> *pNested = NULL) : m_value(value), m_pNested(pNested), m_pNext(NULL) {}C value() const { return m_value; }protected:C m_value;NestedNode<C> *m_pNested;NestedNode<C> *m_pNext;};
NestedList is a simple list containing head and tail pointers:
template <class C>class NestedList{public:NestedList() : m_pHead(NULL), m_pLast(NULL) {}NestedNode<C> *head() { return m_pHead; }void push_back(NestedNode<C> *pNode) { /* omitted */ }protected:NestedNode<C> *m_pHead;NestedNode<C> *m_pLast;};
Here is the iterator.
template <class C>class NestedIter {public:NestedIter(NestedNode<C> *pNode){m_stack.push(pNode);}NestedNode<C> *next(){if (m_stack.size() == 0)return NULL;NestedNode<C> *pCur = m_stack.top();m_stack.pop();if (pCur->m_pNext)m_stack.push(pCur->m_pNext);if (pCur->m_pNested)m_stack.push(pCur->m_pNested);return pCur;}protected:stack<NestedNode<C> *> m_stack;};
And a sample "main"
int main(){NestedList<int> aList;aList.push_back(new NestedNode<int>(1) );aList.push_back(new NestedNode<int>(2) );aList.push_back(new NestedNode<int>(3, new NestedNode<int>(4) ));aList.push_back(new NestedNode<int>(5, new NestedNode<int>(6) ));NestedIter<int> nestedIter(aList.head());NestedNode<int> *pNode;while ((pNode = nestedIter.next()) != NULL) cout << pNode->value() << " -> ";cout << "|" << endl;}
0 0
- CareerCup Program an iterator for a Linked List which may include nodes which are nested within othe
- 4 4 Given a binary search tree, design an algorithm which creates a linked list of all the nodes at
- CareerCup Given a sorted array which contains scores. Write a program to find occurrence
- CareerCup Given an array having positive integers, find a subarray which adds to a given number
- Finding all elements(which are integer) in an array whose sum is equals to the element exists within
- Which parts of an XML document are case-sensitive?
- 141.Which tablespaces are mandatory in an Oracle database for it to be operational? (Choose all that
- Which OS Are You?
- Which are in?
- 50:Which two statements are true about alerts? (Choose two.) 选项 A.Clearing an alert sends the alert
- The books which are worth reading for me
- which
- which
- which
- error: '>>' should be '> >' within a nested template argument list
- error:'>>' should be '> >' within a nested template argument list
- linux error: ‘>>’ should be ‘> >’ within a nested template argument list
- leetcode :Find the contiguous subarray within an array (containing at least one number) which has th
- 太基础的数据类型大小
- ffmpeg
- Android中如何实现EditText的自动换行
- IOS面试题2
- 第二周作业
- CareerCup Program an iterator for a Linked List which may include nodes which are nested within othe
- 【Dijkstra】-LightOJ-1002-Country Roads
- 很全的Linux 音频ALSA技术说明
- sql优化记录
- 生活要怎么过
- 黑马程序员--老毕的“模版方法设计模式”
- HDU 2602 Bone Collector (简单的0-1背包)
- HDU 3573 Buy Sticks 解题报告(贪心)
- 第一个十年计划--无情感、实用的高智能全能助理