アルゴリズム(17)-単一チェーンテーブル-指定したノードの値-C++を削除
単一チェーンテーブルheadと値numを指定し、チェーンテーブルの値numの値を削除します.例えば1->2->3->4->5->null、num=3削除後:1->2->4->5.基本的にはcodingの問題です.考え方:numに等しくないノードをコンテナで集めて接続します.2つのバージョンを提供します:1.vector 2を使う.stack 1を使う.vector
2.stack
// , m_val list
Node* delVal(Node* head, int m_val)
{
Node* cur = head;
vector vc; //
int m_temp;
while (cur != NULL)
{
int m_temp;
m_temp = cur->m_value;
if (m_val != m_temp)
{
vc.push_back(cur); //
}
cur = cur->next;
}
int m_size = vc.size();
cout << "*********************" << endl;
for (int i = 0; i < m_size; i++)
{
cout<m_value="<< vc[i] ->m_value<=0; i--)//vector ,
{
Node* temp = vc[i]; //
temp->next= np;
np = temp;
}
cout << "*********************" << endl;
return np;
}
2.stack
Node* delVal(Node* head, int m_val)
{
Node* cur = head;
stack st; //
int m_temp;
while (cur != NULL)
{
int m_temp;
m_temp = cur->m_value;
if (m_val != m_temp)
{
st.push(cur); //
}
cur = cur->next;
}
Node* np = NULL; // head
while (!st.empty())
{
Node* temp = st.top(); //
st.pop(); //
temp->next = np;
np = temp;
}
cout << "*********************" << endl;
return np;
}