
数据结构中关于链表的删除元素操作的问题(不删除尾节点,只删除中间的某个节点),有问题。
代码#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;
} 展开
#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;
} 展开
1个回答
展开全部
#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;
}
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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询