由买买提看人间百态

topics

全部话题 - 话题: pnode
1 (共1页)
w****x
发帖数: 2483
1
来自主题: JobHunting版 - 做了一下merge BST
//Merge two BST
//O(n) solution
//1. Change BST into linked list
//2. Merge 2 linked lists
//3. Change linked list into a balanced BST
struct NODE
{
int nVal;
NODE* pLft;
NODE* pRgt;
NODE(int n) : nVal(n), pLft(NULL), pRgt(NULL) {}
};
////////////////////change BST to linked list///////////////////////////////
void _inner_chng_link(NODE* pNode, NODE*& ph, NODE*& pt)
{
if (NULL == pNode) return;
NODE* pLft1 = pNode;
NODE* pLft2 = pNode;
_inner_chng_link(pNode->pLft... 阅读全帖
w****x
发帖数: 2483
2
来自主题: JobHunting版 - 攒人品,Twitter 电面题目

写一个
//Merge two BST
//O(n) solution
//1. Change BST into linked list
//2. Merge 2 linked lists
//3. Change linked list into a balanced BST
struct NODE
{
int nVal;
NODE* pLft;
NODE* pRgt;
NODE(int n) : nVal(n), pLft(NULL), pRgt(NULL) {}
};
////////////////////change BST to linked list///////////////////////////////
void _inner_chng_link(NODE* pNode, NODE*& ph, NODE*& pt)
{
if (NULL == pNode) return;
NODE* pLft1 = pNode;
NODE* pLft2 = pNode;
_inner_chng_link(pNode-... 阅读全帖
d**s
发帖数: 98
3
http://zhedahht.blog.163.com/blog/static/2541117420071271047592
程序员面试题精选100题(01)-把二元查找树转变成排序的双向链表[数据结构]
2007-02-27 22:47:59| 分类: 树 | 标签:就业 找工作 编程 数据结构 算法
|字号大中小 订阅
题目:输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。要求不
能创建任何新的结点,只调整指针的指向。
比如将二元查找树
10
/ \
6 14
/ \ /  \
 4 8 12   16
转换成双向链表
... 阅读全帖
w****x
发帖数: 2483
4
来自主题: JobHunting版 - 谷歌面经
struct NODE
{
string str;
NODE* pLft;
NODE* pRgt;
NODE(const char* szStr = "") : str(szStr), pLft(NULL), pRgt(NULL) {}
};
void serialize(NODE* pNode, char*& p)
{
if (p == NULL) return;
if (pNode == NULL)
{
*((int*)p) = 0;
p += sizeof(int);
return;
}

int nLen = pNode->str.length();
*((int*)p) = 1;
p += sizeof(int);
strcpy(p, pNode->str.c_str());
p += nLen+1;

serialize(pNode->pLft, p);
serialize(pNode->pRgt... 阅读全帖
w****x
发帖数: 2483
5
来自主题: JobHunting版 - 刚才重新回顾了一下那题
struct NODE
{
vector vecGUID;
NODE* nodes[256];

NODE() { memset(nodes, 0, sizeof(nodes)); }
};
void _inner_get_guid(NODE* pNode, const char* str, int k, vector& vec)
{
if (NULL == pNode)
return;
if (k <= 0)
{
vec = pNode->vecGUID;
return;
}
_inner_get_guid(pNode->nodes[*str], str+1, k-1, vec);
}
vector getGUID(const char* str, NODE* pRoot, int k)
{
vector vecRet;
if (NULL == pRoot || NULL == str || *str == 0 ... 阅读全帖
b***m
发帖数: 5987
6
来自主题: JobHunting版 - LeetCode题Binary Tree Inorder Traversal
我一般用这个:
void InOrder(Node *pRoot)
{
Stack s;
Node *pNode = pRoot;

while( pNode || !s.Empty() )
{
while( pNode )
{
s.push(pNode);
pNode = pNode->pLeft;
}
if( !s.Empty() )
{
pNode = s.pop();
Visit(pNode);
pNode = pNode->pRight;
}
}
}
w****x
发帖数: 2483
7
来自主题: JobHunting版 - 今天面的太惨了....
struct NODE
{
int nVal;
NODE* pLft;
NODE* pRgt;
NODE(int n) : nVal(n), pLft(NULL), pRgt(NULL) {}
};
int getHeight(NODE* pNode, bool bLft)
{
int nRet = 0;
NODE* pIter = pNode;
while (NULL != pIter)
{
if (bLft)
pIter = pIter->pLft;
else
pIter = pIter->pRgt;
nRet++;
}
return nRet;
}
int getNum(int h)
{
return pow((double)2, h) -1;
}
int getNumberOfNodes(NODE* pRoot)
{
NODE* pNode = pRoot;
int h = get... 阅读全帖
w****x
发帖数: 2483
8
来自主题: JobHunting版 - MS on-site 面经&求分析(口头offer)
不用队列实现next连接
void LinkRightFromParent(NODE* pNode, NODE* pParent)
{
if (pNode == NULL) return;
if (pParent != NULL && pParent->pLft == pNode && pParent->pRgt
!= NULL)
pNode->pSibling = pParent->pRgt;
else
{
NODE* pIter = pParent == NULL ? NULL : pParent-
>pSibling;
NODE* pRgtCon = NULL;
while (pIter != NULL && pRgtCon == NULL)
{
if (pIter->pLft != NULL)
pRgtCon = pIter->pLft;
else if (pIter->pRgt != NULL)
pRgtCon = pIter->pRgt;
pIter = pIter->pSibling;
}
pNode->pSibling = pRgtCon;
}
LinkRightFro... 阅读全帖
w****x
发帖数: 2483
9
/*
add sibling pointer to the right sibling of each node in a tree, O(n) time,
O(1) space.
5 minutes thinking, 10 minutes coding, a hint he gave me: recursion
*/
struct NODE
{
int nVal;
NODE* pLft;
NODE* pRgt;
NODE* pSibling;
NODE(int n) : nVal(n), pLft(NULL), pRgt(NULL), pSibling(NULL) {}
};
void LinkRightFromParent(NODE* pNode, NODE* pParent)
{
if (pNode == NULL) return;
//Under this situation, the current node will connect to the child of
current parent
if (pPa... 阅读全帖
w****x
发帖数: 2483
10
/*
add sibling pointer to the right sibling of each node in a tree, O(n) time,
O(1) space.
5 minutes thinking, 10 minutes coding, a hint he gave me: recursion
*/
struct NODE
{
int nVal;
NODE* pLft;
NODE* pRgt;
NODE* pSibling;
NODE(int n) : nVal(n), pLft(NULL), pRgt(NULL), pSibling(NULL) {}
};
void LinkRightFromParent(NODE* pNode, NODE* pParent)
{
if (pNode == NULL) return;
//Under this situation, the current node will connect to the child of
current parent
if (pPa... 阅读全帖
n****t
发帖数: 241
11
来自主题: JobHunting版 - Google校园面试题
我看到貌似正确的一个解法,加了很多限制条件。。。
面试题之二叉搜索树的中位数 收藏
这个问题不算是很常见的问题,基本上在中文的论坛社区没有看到过,遇见这个是因为
偶尔在http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi 上面注册了账号而看到的,题目如下:
Given a BST (Binary search Tree) how will you find median in that?
Constraints:
* No extra memory.
* Function should be reentrant (No static, global variables allowed.)
* Median for even no of nodes will be the average of 2 middle elements and
for odd no of terms will be middle element only.
* Algorithm should be efficient in terms of comple... 阅读全帖
D**f
发帖数: 439
12
来自主题: JobHunting版 - Binary Tree Maximum Path Sum
MaxSum(pNode) = max {MaxSum(pNode->L), MaxSum(pNode->R), pNode->V +
MaxTopDown(pNode->L) + MaxTopDown(pNode->R) }
MaxTopDown(pNode) is the max sum of path from root to leaf in this tree.
b***m
发帖数: 5987
13
来自主题: JobHunting版 - Binary Tree Maximum Path Sum
我的这个code貌似跟二爷的意思一样?
int g_nMax = MIN_INT;
int MaxPathSum(Node *pNode)
{
if( !pNode ) return 0;

int nMaxLeft = MaxPathSum(pNode->pLeft);
int nMaxRight = MaxPathSum(pNode->pRight);
int nMaxCurrent = pNode->data + max(0, nMaxLeft) + max(0, nMaxRight);
g_nMax = max(g_nMax, nMaxCurrent);

int nMaxLeftRight = max(nMaxLeft, nMaxRight);
return nMaxLeftRight > 0 ? nMaxLeftRight + pNode->data : pNode->data;
}
w****x
发帖数: 2483
14
来自主题: JobHunting版 - 几道F家面试题

的却是很好的一个方案,不需要用heap, 因为一定是连续的
void _inner_mclosest(NODE* pNode, int nTg, int m, deque& que)
{
if (NULL == pNode)
return;
_inner_mclosest(pNode->pLft, nTg, m, que);
if (que.size() < m)
que.push_back(pNode);
else
{
if (abs(pNode->nVal - nTg) < abs(que.front()->nVal - nTg))
{
que.pop_front();
que.push_back(pNode);
}
}
_inner_mclosest(pNode->pRgt, nTg, m, que);
}
bool getMClosest2(NODE* pRoot, int nTg... 阅读全帖
l********n
发帖数: 1038
15
来自主题: JobHunting版 - 反转链表有几种方法
现有两种方法,一种递归,一种直接存指针反转。哪种好?
Java版
static List Reverse(List L)
{
//firstly check if L is empty or only has one element then return
if(L==null || L.next==null)
return L;
//otherwise, we can use our recursive method
List remainingReverse = Reverse(L.next);
//next we have two step steps, firstly we need update the tail of
remaining reverse as our head L
L.next.next = L;//this (L.next) is the key to get te tail in constant
time!
//Very important, we need set L.next ... 阅读全帖
j*****z
发帖数: 11
16
来自主题: Programming版 - Three C/C++ Programming Questions
Can anyone help me to answer the following questions:
1) Given the following code snippet, what does the function DoesWhat() do?
And what, if any, assumptions are made about the input to the function.
struct _tagElement
{
int m_cRef;
unsigned char m_bData[20];
struct _tagElement * m_next;

} NODE, * PNODE;
PNODE DoesWhat (PNODE pn1, PNODE pn2)
{
PNODE * ppnV = &pn1;
PNODE * ppn1 = &pn1;
PNODE * ppn2 = &pn2;
for ( ; *ppn1 || *ppn2; ppn1 = &((
w****x
发帖数: 2483
17
来自主题: JobHunting版 - binary tree的in-order iterator怎么写?
这题作的不下5遍了, 说实话, 第一次做还挺不容易的:
============带parent===========================
void InOrderPrint(NODE* pRoot)
{
assert(pRoot);
NODE* pCur = pRoot;
while (pCur != NULL)
{
while (pCur->pLft != NULL)
{
cout<nVal< pCur = pCur->pLft;
}
cout<nVal< //The ending condition is tricky but simple
while (pCur != NULL)//use "pCur != NULL" rather than "pCur->pParent
!= NULL"
... 阅读全帖
w****x
发帖数: 2483
18
来自主题: JobHunting版 - 发几个面试题
/*
Serialize/DeSerialize a tree
*/
struct NODE
{
int nVal;
vector vec;
NODE(int n) : nVal(n) {}
};
void _inner_serial(NODE* pNode, char*& p)
{
if (NULL == pNode)
return;
*p++ = pNode->vec.size();
*p++ = pNode->nVal;
for (vector::iterator it = pNode->vec.begin();
it != pNode->vec.end(); it++)
_inner_serial(*it, p);
}
const char* Serialize(NODE* pRoot, char mem[])
{
if (NULL == mem || NULL == pRoot)
return NULL;
char... 阅读全帖
w****x
发帖数: 2483
19
/*
Serialize/DeSerialize a tree
*/
struct NODE
{
int nVal;
vector vec;
NODE(int n) : nVal(n) {}
};
void _inner_serial(NODE* pNode, char*& p)
{
if (NULL == pNode)
return;
*p++ = pNode->vec.size();
*p++ = pNode->nVal;
for (vector::iterator it = pNode->vec.begin();
it != pNode->vec.end(); it++)
_inner_serial(*it, p);
}
const char* Serialize(NODE* pRoot, char mem[])
{
if (NULL == mem || NULL == pRoot)
return NULL;
char... 阅读全帖
w****x
发帖数: 2483
20
/*
Serialize/DeSerialize a tree
*/
struct NODE
{
int nVal;
vector vec;
NODE(int n) : nVal(n) {}
};
void _inner_serial(NODE* pNode, char*& p)
{
if (NULL == pNode)
return;
*p++ = pNode->vec.size();
*p++ = pNode->nVal;
for (vector::iterator it = pNode->vec.begin();
it != pNode->vec.end(); it++)
_inner_serial(*it, p);
}
const char* Serialize(NODE* pRoot, char mem[])
{
if (NULL == mem || NULL == pRoot)
return NULL;
char... 阅读全帖
w****x
发帖数: 2483
21

没意思
单链表的quick sort就可以了
struct NODE
{
int nVal;
NODE* pNext;
NODE(int n) : nVal(n), pNext(NULL) {}
};
void sort(NODE* pNode, int nLen)
{
if (NULL == pNode || nLen <= 0)
return;
int nCount = 0;
int nPivot = pNode->nVal;
NODE* pPrev = pNode;
NODE* pIterBeg = pPrev->pNext;
NODE* pIterEnd = pIterBeg;
while (NULL != pIterEnd)
{
if (pIterEnd->nVal < nPivot)
{
swap(pIterBeg->nVal, pIterEnd->nVal);
pPrev = pPrev-... 阅读全帖
x*********w
发帖数: 533
22
quick sort版本
struct NODE
{
int nVal;
NODE* pNext;
NODE(int n) : nVal(n), pNext(NULL) {}
};
void sort(NODE* pNode, int nLen)
{
if (NULL == pNode || nLen <= 0)
return;
int nCount = 0;
int nPivot = pNode->nVal;
NODE* pPrev = pNode;
NODE* pIterBeg = pPrev->pNext;
NODE* pIterEnd = pIterBeg;
while (NULL != pIterEnd)
{
if (pIterEnd->nVal < nPivot)
{
swap(pIterBeg->nVal, pIterEnd->nVal);
pPrev = pPrev->pNext;
... 阅读全帖
w**x
发帖数: 362
23
来自主题: JobHunting版 - bst中序遍历c++ class iterator
your code is not C++, but C.
No constructor.
No operator()++.
No destructor.
No current iter.
why void PushLefts(NODE* pNode) ?
Left is left not lefts.
struct NODE
{
int nVal;
NODE* pLft;
NODE* pRgt;
NODE(int n) : nVal(n), pLft(NULL), pRgt(NULL) {}
};
class BTIterator
{
public:
void Init(NODE* pRoot)
{ PushLefts(pRoot); }
NODE* Next()
{
if (m_stk.empty())
return NULL;
NODE* pRet = m_stk.top();
m_stk.pop();
PushLefts(pRet... 阅读全帖
r*c
发帖数: 167
24
来自主题: JobHunting版 - 问一道题(6)
贴个pattern字符串有重复字符的解法, 是dek,cpp1等大牛的解法的扩展。
#include
#include
#include
#include
using namespace std;
#define INT_MAX 2147483647
#define INT_MIN -2147483648
class MinWindowSolution
{
public:
struct TreeNode
{
TreeNode *parent;
int val;
vector children;
TreeNode(int i, TreeNode *p) : val(i), parent(p){}
};
void FindMinWindow_Tree(const vector& input , const vector&
query , int& nStart,... 阅读全帖
p*****3
发帖数: 488
25
来自主题: JobHunting版 - 请问如何sort一个linked list?
给个我以前写的吧,快速排序版本的
struct NODE
{
    int nVal;
    NODE* pNext;
 
    NODE(int n) : nVal(n), pNext(NULL) {}
};
 
void sort(NODE* pNode, int nLen)
{
    if (NULL == pNode || nLen <= 0)
        return;
 
    int nCount = 0;
    int nPivot = pNode->nVal;
    NODE* pPrev = pNode;
    NODE* ... 阅读全帖
I****h
发帖数: 33
26
来自主题: JobHunting版 - 请教一道单链表问题
PNode trim_list(PNode begin)
{
Node head = {0};
PNode anchor = NULL;
PNode prev = NULL;
PNode curr = NULL;
int trim = 0;
if ((begin == NULL) || (begin->key != 0))
{
head.key = 0;
}
else
{
head.key = 1;
}
head.next = begin;
anchor = &head;
prev = &head;
curr = begin;
while (curr != NULL)
{
if (prev->key != curr->key)
{
if (trim == 0)
{
anchor = prev;
... 阅读全帖
w****x
发帖数: 2483
27
来自主题: JobHunting版 - FB面试题:binary tree inorder successor
5 minutes
struct NODE
{
int nVal;
NODE* pLft;
NODE* pRgt;
NODE(int n) : nVal(n), pLft(NULL), pRgt(NULL) {}
};
class BTIterator
{
public:
void Init(NODE* pRoot)
{ PushLefts(pRoot); }
NODE* Next()
{
if (m_stk.empty())
return NULL;
NODE* pRet = m_stk.top();
m_stk.pop();
PushLefts(pRet->pRgt);
return pRet;
}
private:
void PushLefts(NODE* pNode)
{
while (NULL != pNode)
{
m_stk.p... 阅读全帖
D**f
发帖数: 439
28
来自主题: JobHunting版 - 一个Naive的问题,tree的destruction
有点太简单,都不好意思放上来,就是帮我看看这个destructor会不会有内存泄漏?我
觉得似乎要来个delete this?
在constructor里new了一堆Node按照树结构组织好。
class SuffixNode
{
public:
SuffixNode(const char*);
~SuffixNode();
private:
SuffixNode* pFirstChild;
SuffixNode* pNextSibling;
};
SuffixNode::~SuffixNode()
{
SuffixNode* pNode = pFirstChild;
SuffixNode* pToDelete = 0;
while(pNode)
{
pToDelete = pNode;
pNode = pNode->pNextSibling;
delete pToDelete;
}
}
int main()
{
const char* s = "Hello ... 阅读全帖
h*****f
发帖数: 248
29
来自主题: JobHunting版 - 请教这么一个题:BST maximum sum path
#include
struct Node {
int value;
Node* pLeft;
Node* pRight;
};
int findMaxSubTree(Node* pNode, int& maxSoFar) {
if (!pNode) return 0;
int leftSum = findMaxSubTree(pNode->pLeft, maxSoFar);
int rightSum = findMaxSubTree(pNode->pRight, maxSoFar);
int currentSum = pNode->value + std::max(0, leftSum) + std::max(0,
rightSum);
maxSoFar = std::max(currentSum, maxSoFar);
return currentSum;
}
int main() {
Node node4 = {4, NULL, NULL};
Node node3 = {3, NULL, NULL... 阅读全帖
w****x
发帖数: 2483
30
来自主题: JobHunting版 - g 两轮店面面经 失败告终
那个烙印这些题根本就是想灭你嘛, 楼主不要自责。
中国人最后那题可能是要这个解法:
void inner_get_rightmost(NODE* pNode, vector& vec, int nLev)
{
if (NULL == pNode)
return;
if (nLev >= vec.size())
vec.push_back(pNode);
inner_get_rightmost(pNode->pRgt, vec, nLev+1);
inner_get_rightmost(pNode->pLft, vec, nLev+1);
}
vector getRightMost(NODE* pRoot)
{
vector vec;
inner_get_rightmost(pRoot, vec, 0);
return vec;
}
p*****3
发帖数: 488
31
来自主题: JobHunting版 - bst中序遍历c++ class iterator
struct NODE
{
int nVal;
NODE* pLft;
NODE* pRgt;
NODE(int n) : nVal(n), pLft(NULL), pRgt(NULL) {}
};
class BTIterator
{
public:
void Init(NODE* pRoot)
{ PushLefts(pRoot); }
NODE* Next()
{
if (m_stk.empty())
return NULL;
NODE* pRet = m_stk.top();
m_stk.pop();
PushLefts(pRet->pRgt);
return pRet;
}
private:
void PushLefts(NODE* pNode)
{
while (NULL != pNode)
{
m_stk.push(pNode)... 阅读全帖
q******0
发帖数: 15
32
来自主题: JobHunting版 - 贡献G电 估计挂了
//N is the number of nodes
HashMap hmap;
Node* pNode[N];
bool isClusterHeader[N]
for (int i=0; i hmap[pNode[i]] = i;
isClusterHeader[i] = true;
}
for (int i=0; i if (lookup(hmap, pNode[i]->next) { //its next is in hashmap
isClusterHeader[hmap[pNode[i]->next] = false; //link to next
}
}
for (int i=0; i if (isClusterHeader[i]) {
print(pNode[i])
//continue print its next until lookup(hmap, next) == true,
//which means a new cluster starts... 阅读全帖
c*****e
发帖数: 74
33
题目:一个board上,每个cell是一个字母,从一个cell出发,可以到它相邻的8个cell
。这样可以在board上walk。但是一个walk上一个cell不能出现2次。
要求:找出所有的可以由walk得到到的不同的单词。编程中可以自己假定一些已经实现
的方法。
以前面试被问到过,时间浪费在一些细节(怎么解决8个相邻cell的问题,怎么检查是
否一个cell重复出现)上了,当时也没有充分利用try,没答好,主要还是对递归、Try
的接口不熟。今天好好想了想该怎么写,下面是我的代码,花了不少时间,觉得收获不
少。好好写写这道题应该对面试很有帮助的。
有问题帮忙指出一下。
---------------------------------
class CTryNode;
class CTry
{
public:
CTryNode* Advance(CTryNode* pNode, char ch);
CTryNode* Back(CTryNode* pNode);
string GetValue(pTryNode* pNode);
bool IsCurr... 阅读全帖
p*****2
发帖数: 21240
34
来自主题: JobHunting版 - 一道挺简单的题给搞砸了
被出了个经典题reverse linkedlist, 我说我刚刚被问道了。他说是吗?你做出来了吗
?我说做出来了。他说那你再写一遍吧,既然已经做了一次了,这次应该perfect才对
吧。然后去倒茶了,让我自己写。大概1,2分钟写完,大概这个样子。
PNode reverse(PNode head)
{
PNode newhead=null;
while(head)
{
PNode n=head;
head=head->next;
n->next=newhead;
newhead=n;
}
return newhead;
}
等了几分钟那人一进门就说,“二爷,你怎么写的这么复杂呢?你怎么用了这么多指针
呢?你能不能少用一些指针呢?“,
我说”你的意思是让我用一个指针变量来完成“,他说”是的,你用了太多指针了“。
我说”好吧,我看看“。
结果不看还好,越看越心惊,压力无限大,脑子短路,啥思路也没有,当即就跪了。
本来一道挺简单的题,咋做成个这样子呢?昨晚一晚没睡好,郁闷呀。
w****x
发帖数: 2483
35
来自主题: JobHunting版 - F家面经
//Get all trees according to preorder
struct NODE
{
int nVal;
NODE* pLft;
NODE* pRgt;
NODE(int n) : nVal(n), pLft(NULL), pRgt(NULL) {}
};
void GetComb(int a[], int n, vector& vec)
{
if (n <= 0)
return ;
if (n == 1)
{
vec.push_back(new NODE(a[0]));
return;
}
vector vecTmp1;
GetComb(a+1, n-1, vecTmp1);
for (vector::iterator it = vecTmp1.begin();
it != vecTmp1.end(); it++)
{
NODE* pRoot = ne... 阅读全帖
w****x
发帖数: 2483
36
来自主题: JobHunting版 - 几道F家面试题
struct NODE
{
int nVal;
NODE* pLft;
NODE* pRgt;
NODE(int x) : nVal(x), pLft(NULL), pRgt(NULL) {}
};
struct TreeIter
{
stack m_stk;
void movNext()
{
if (m_stk.empty())
return;
NODE* pCur = m_stk.top();
if (pCur->pRgt != NULL)
{
pushLft(pCur->pRgt);
return;
}

while (!m_stk.empty() && pCur != m_stk.top()->pLft)
{
pCur = m_stk.top();
m_stk.pop... 阅读全帖
z***e
发帖数: 5393
37
来自主题: JobHunting版 - can a pointer point to itself in c++?
"this" is private, you cannot call this->this->... outside the class.
inside the class, yes you can.
example:
struct Node
{
Node* next;
}
Node* pNode = new Node();
pNode->next = pNode;
s*********e
发帖数: 17
38
来自主题: Programming版 - how to reverse a HUGE list?
How can you print singly linked list in reverse order? (it's a huge list and
you cant use recursion) ?
大家有没有更好的方法 print “HUGE” list in reverse order? 谢谢!
BOOL ReverseList(node** pphead)
{
if(*pphead == NULL)
return FALSE;
node* pNode = NULL;
node* pTmp;

while(*pphead != NULL)
{
// tmp storage of header pointer
pTmp = (*pphead)->pNext;

// reverse
(*pphead)->pNext = pNode;

// pNode pointer moves one
c****p
发帖数: 32
39
“找下一个要打印的数”?这又回到我开始问的问题:是否有算法可以直接算出“下一
个”,而无需比较该数已经被删除?
post一个我用linked list实现的土法:D
void RingDelete(int* values, unsigned int size, int M)
{
if (size==0) return;
printf("step: %d\n", M);
struct Node
{
int value;
Node* next;
};
Node head;
head.next = NULL;
Node* pLastNode = NULL;
// costruct a circular linked list
for(int i=0; i {
Node* pNode = new Node();
pNode->value=values[i];
if (i==0)
{
w****x
发帖数: 2483
40
来自主题: JobHunting版 - 问道G家的面试题。
struct NODE
{
int nVal;
NODE* pLft;
NODE* pRgt;

NODE(int n) : nVal(n), pLft(NULL), pRgt(NULL) {}
};
class CIterator
{
public:
CIterator(NODE* pRoot)
{
pushLft(pRoot);
}

NODE* GetNext()
{
if (m_stk.empty()) return NULL;

NODE* pRet = stk.top();
stk.pop();

pushLft(pRet->pRgt);

return pRet
}

private:
void pushLft(NODE* pNode)
{
NODE* pIt... 阅读全帖
F*******1
发帖数: 75
41
来自主题: Statistics版 - 问个SAS 数据读入的问题
请教一个SAS 数据读入的问题. 我有个样本文件. 见附件.
我只需要第6行到第11行的数据. 在data statement 中, 我可以用firstobs=6 来定位
起事行. 那用什么来定位末位行呢? 我试了lastobs=11 不work. 谢谢!
如果我先读入所有数据,再提取需要的数据.我的sas script 如下. 但结果不对, 第7行
到第11行的数据丢了. 是什么原因呢? 谢谢!
data RT1;
%let _EFIERR_ = 0; /* set the ERROR detection macro variable */
infile "\\mkscsas01\saswork\20090108_asm_rtmcp_final.csv" delimiter =
',' MISSOVER DSD lrecl=55010 firstobs=6 ;
format Pnode $12. MCPType $12.;
INPUT Pnode $ Zone $ MCPType $ HE1 - HE24;
run;
w****x
发帖数: 2483
42
class Node {
public:
std::map children;
};
void insert(const char* str, Node* n)
{
if (0 == *str || NULL == pNode)
return;

if (children.find(*str) == children.end())
children[*str] = new Node;

insert(str+1, children[*str]);
}
bool contains(const char* str, Node* n)
{
if (0 == *str) return true;
if (children.find(*str) == end())
return false;
contains(str+1, children[*str]);
}
m*******1
发帖数: 77
43
来自主题: JobHunting版 - Binary Tree Maximum Path Sum
MaxTopDown(pNode) 为什么一定要从root 到leaf呢?只要从root开始到任意一个位置
都可以啊
l*****a
发帖数: 14598
44
来自主题: JobHunting版 - LeetCode题Binary Tree Inorder Traversal
等等,我搞错了,被误导了
以为你用了visit flag...
直接cout<data<
w****x
发帖数: 2483
45
来自主题: JobHunting版 - binary tree的 serialization

a
我晕,你看我unserialize的代码就知道了,写个serialize的:
if (NULL == pNode)
*p = true;
else
{
*p = false;
p += sizeof(bool);
从p开始写入字符串
}
j*****q
发帖数: 26
46
来自主题: XML版 - MS XML node insert question
MSXML2::DOMNodeType pType;
pNode->get_nodeType(&pType);
1 (共1页)