请各位高手帮帮忙 解答一下这两个问题
1.对于有n个元素的线性表 顺序结构 写个算法删除表中值为k的元素
2.已知一个单链表 插入一个元素后仍然有序的算法
[解决办法]
Node* Insert(Node* head, int idata)
{
Node* q = head;
Node* p = head-> next;
while (p != NULL)
{
if (idata <= p-> data)
{
Node* pnew = new Node;
pnew-> data = idata;
q-> next = pnew;
pnew-> next = p;
break; //sorry 拉了个break;
}
q = p;
p = p-> next;
}
return head;
}
[解决办法]
这里不好意思,里面有笔误.
void DeleteNode(rLineListNode *prListHead, int NodeCnt, int data)
{
rLineListNode *prPrev = NULL;
rLineListNode *prCurrent = NULL;
if (NULL == prListHead || 0 == NodeCnt)
{
return;
}
prPrev = prListHead;
prCurrent = prListHead-> prNext;
while(NULL != prCurrent);
{
if (data == prCurrent-> Value)
{
rLineListNode *prTemp = NULL;
// Find the needed node, then delete it.
prTemp = prCurrent;
prPrev-> prNext = prCurrent-> prNext;
prCurrent = prPrev-> prNext;
free(prTemp);
}
else
{
// Loop the list and continue to find.
prPrev = prCurrent;
prCurrent = prCurrent-> prNext;
}
}
}