Redian新闻
>
请大家推荐位装修师傅
avatar
请大家推荐位装修师傅# Living
c*g
1
Given a linked list and a value, delete all the nodes having the value equal
to that value. (Use C++)
class Node
{
public:
int data;
Node* next;
};
void f(int a, Node* head)
{
while(head != NULL)
{
if(head->data == a) // if match, delete the node
{
Node* temp = head;
head->next = head->next->next;
delete temp;
}
head = head->next;
}
}
请问if条件下的作法对吗? 一般来说需要delete那个node吗? (若用java好像就不用)
如果把Node* temp = head;在while loop 之前宣告, 这样会不会比每次都要在if条件
下宣告一次来的好些? 谢谢了
avatar
s*g
2
人在南湾,最近在找装修的contractor,请问大家有没有好的推荐啊。朋友说有很多帐
做的不干净的师傅,因为上班忙,不可能每天盯得很紧。多谢了。
★ Sent from iPhone App: iReader Mitbbs Lite 7.56
avatar
c*a
3
你需要一个previous pointer吧....
或者你用
while(head->next !=null)
if head->next.data == a
然后删掉 head.next
avatar
c*g
4
Thanks for correction. 我改了一些 先假设a数值不发生在第一个node, 想请问这样
的delete node方式是不是正确的
void f(int a, Node* head)
{
while(head->next != NULL)
{
if(head->next->data == a) // if match, delete the node
{
Node* temp = head->next;
head->next = head->next->next;
delete temp;
}
head = head->next;
}
}

【在 c*****a 的大作中提到】
: 你需要一个previous pointer吧....
: 或者你用
: while(head->next !=null)
: if head->next.data == a
: 然后删掉 head.next

avatar
m*z
5
你这个删完了,head在哪呢,链表就没了吧
in C
void del(int a)
{
Node* pre, temp;
for(pre = head; pre&&pre->next; pre = pre->next) {
if(pre->data == a)
head = head->next;
else {
temp = pre->next;
if (temp->item == a)
pre->next = temp->next;
//free(temp);
}
}
}
avatar
x*0
6
这个和我上学期考试题目一样啊
//recursively remove ALL nodes with value
PINTLISTNODE RemoveAll1(PINTLISTNODE pHead, int iRemoveValue)
{
if (pHead == NULL)
{
return NULL;
}
if (pHead->iValue == iRemoveValue)
{
PINTLISTNODE pToDelete = pHead;
pHead = pHead->pNext;
delete pToDelete;
return RemoveAll1(pHead, iRemoveValue);
}
else
{
pHead->pNext = RemoveAll1(pHead->pNext, iRemoveValue);
return pHead;
}
}
//use a dummyHead
PINTLISTNODE RemoveAll2(PINTLISTNODE pHead, int iRemoveValue)
{
INTLISTNODE dummyHead;
dummyHead.pNext = pHead;
PINTLISTNODE pCurrent = &dummyHead;
while (pCurrent->pNext != NULL)
{
if (pCurrent->pNext->iValue == iRemoveValue)
{
PINTLISTNODE pToDelete = pCurrent->pNext;
pCurrent->pNext = pCurrent->pNext->pNext;
delete pToDelete;
}
else
{
pCurrent = pCurrent->pNext;
}
}
return dummyHead.pNext;
}
//ppCurrent
PINTLISTNODE RemoveAll3(PINTLISTNODE pHead, int iRemoveValue)
{
PINTLISTNODE *ppCurrent = &pHead;
while (*ppCurrent != NULL)
{
if ((*ppCurrent)->iValue == iRemoveValue)
{
PINTLISTNODE pToDelete = *ppCurrent;
*ppCurrent = (*ppCurrent)->pNext;
delete pToDelete;
}
else
{
ppCurrent = &((*ppCurrent)->pNext);
}
}
return pHead;
}
//iteratively remove All nodes with value
PINTLISTNODE RemoveAll4(PINTLISTNODE pHead, int iRemoveValue)
{
if (pHead == NULL)
{
return NULL;
}
while ( pHead != NULL && pHead->iValue == iRemoveValue)
{
PINTLISTNODE pToDelete = pHead;
pHead = pHead->pNext;
delete pToDelete;
}
// pHead->iValue != iRemoveValue
PINTLISTNODE cur = pHead;
PINTLISTNODE pre = NULL;
while (cur != NULL)
{
if (cur->iValue == iRemoveValue)
{
PINTLISTNODE pToDelete = cur;
pre->pNext = cur->pNext;
cur = cur->pNext;
delete pToDelete;
}
else
{
pre = cur;
cur = cur->pNext;
}
}
return pHead;
}
avatar
s*s
7
if only have one node n, n.val == value
the head->next->next access the null pointer.

equal

【在 c*****g 的大作中提到】
: Given a linked list and a value, delete all the nodes having the value equal
: to that value. (Use C++)
: class Node
: {
: public:
: int data;
: Node* next;
: };
: void f(int a, Node* head)
: {

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。