编程题,将五个数字放入单项链表中,删除第三节点并打印该链表,怎么做
1个回答
展开全部
class node{
public:
node* m_pNext;
int i;
};
------------
node* pHead = new node; //第一个数
pHead->m_pNext = 0;
node* pTail = pHead;
pHead->i = 1;
for(int i = 2; i<6; i++){ //添加4个
node* pNew = new node;
pNew ->i = i;
pNew ->m_pNext = 0;
pTail->m_pNext = pNew;
pTail = pNew;
}
//找到第2个,第3个,第4个。
node* p2,*p3,*p4;
p2 = pHead->m_pNext;
p3 = p2->m_pNext;
p4 = p3->m_pNext;
//2连4,删掉3
p2->m_pNext = p4;
delete p3;
//打印
node* p = pHead;
while(p != 0)
{
printf("%d ",p->i);
p = p->m_pNext;
}
public:
node* m_pNext;
int i;
};
------------
node* pHead = new node; //第一个数
pHead->m_pNext = 0;
node* pTail = pHead;
pHead->i = 1;
for(int i = 2; i<6; i++){ //添加4个
node* pNew = new node;
pNew ->i = i;
pNew ->m_pNext = 0;
pTail->m_pNext = pNew;
pTail = pNew;
}
//找到第2个,第3个,第4个。
node* p2,*p3,*p4;
p2 = pHead->m_pNext;
p3 = p2->m_pNext;
p4 = p3->m_pNext;
//2连4,删掉3
p2->m_pNext = p4;
delete p3;
//打印
node* p = pHead;
while(p != 0)
{
printf("%d ",p->i);
p = p->m_pNext;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询