数据结构中关于链表的删除元素操作的问题(不删除尾节点,只删除中间的某个节点),有问题。

代码#include<iostream>usingnamespacestd;typedefstructStudent{intscore;Student*pnext;}ST... 代码
#include <iostream>
using namespace std;
typedef struct Student
{
int score;
Student * pnext;
}ST,* PST;
PST Create_List()
{
int n;
cout<<"节点个数:"<<endl;
cin>>n;
PST phead = new ST;
PST pTail = phead;
pTail->pnext = NULL;
for (int i = 0; i < n; i++)
{
PST pnew = new ST;
cout<<"输入学生分数:"<<endl;
cin>>pnew->score;
pTail->pnext = pnew;
pnew->pnext = NULL;
pTail = pnew;
}
return phead;
}
void traverse(PST phead)
{
PST p = phead->pnext;
while (p!=NULL)
{
cout<<p->score<<endl;
p = p->pnext;
}
}
bool Insert_List(PST phead, int position, int n)
{
int i = 0;
PST p = phead;
while (p != NULL && i < position-1)
{
p = p->pnext;
++i;
}
if (p == NULL)
return false;
PST pnew = new ST;
pnew->score = n;

PST ptemp = p->pnext;
p->pnext = pnew;
pnew->pnext = ptemp;
return true;
}
bool Delete_List(PST phead, int position)
{
int i = 0;
PST p = phead;
while (p != NULL && i < position-1)
{
p = p->pnext;
++i;
}
if (p == NULL)
return false;
PST temp = new ST;
temp = p->pnext;
p = temp->pnext;
delete temp;
temp = NULL;
return true;
}
int main()
{
PST phead = Create_List();

// Insert_List(phead, 3, 46);
Delete_List(phead, 2);
traverse(phead);
return 0;
}
展开
 我来答
majieliang001
2013-10-20 · 超过28用户采纳过TA的回答
知道答主
回答量:103
采纳率:0%
帮助的人:49.7万
展开全部
#include <iostream>
using namespace std;
typedef struct Student
{

int score;

Student * pnext;
}ST,* PST;
PST Create_List()
{

int n;

cout<<"节点个数:"<<endl;

cin>>n;

PST phead = new ST;

PST pTail = phead;

pTail->pnext = NULL;

for (int i = 0; i < n; i++)

{

PST pnew = new ST;

cout<<"输入学生分数:"<<endl;

cin>>pnew->score;

pTail->pnext = pnew;

pnew->pnext = NULL;

pTail = pnew;

}

return phead;
}
void traverse(PST phead)
{

PST p = phead->pnext;

while (p!=NULL)

{

cout<<p->score<<endl;

p = p->pnext;

}
}
bool Insert_List(PST phead, int position, int n)
{

int i = 0;

PST p = phead;

while (p != NULL && i < position-1)

{

p = p->pnext;

++i;

}

if (p == NULL)

return false;

PST pnew = new ST;

pnew->score = n;

PST ptemp = p->pnext;

p->pnext = pnew;

pnew->pnext = ptemp;

return true;
}
bool Delete_List(PST phead, int position)
{

int i = 0;

PST p = phead;

while (p != NULL && i < position-1)

{

p = p->pnext;

++i;

}

if (p == NULL)

return false;

PST temp = p->pnext;

p->pnext = temp->pnext;

delete temp;

temp = NULL;

return true;
}
int main()
{

PST phead = Create_List();

// Insert_List(phead, 3, 46);

Delete_List(phead, 2);

traverse(phead);

return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式