2 baixuefeng1996 baixuefeng1996 于 2016.04.28 19:22 提问

删除链表的节点,然后再查询,编号会错误

#include
#include
#include
#include
using namespace std;
typedef int datatype;
typedef struct person
{
datatype score;
char name[20];
int number;
struct person *llink, *rlink;
}PE;

void display(PE *head)
{
PE *p;
cout << endl;
p = head;
if (!p)
cout << "链表为空" << endl;
else
{
cout << "编号 姓名 成绩" << endl;
while (p)
{
cout << p->number << " " << p->name << " " << p->score< p = p->rlink;
}
cout << endl;
}
}

PE *find(PE *head, int number)
{
int j = 1;
PE *p = head;
while (p&&number != j)
{
p = p->rlink;
j++;
}
if (!p)
{
cout << "该编号不存在!" << endl;
return NULL;
}
return p;
}

PE insert(PE *head, int number, char a[], datatype x )
{
PE *p, *q;
p = (PE
)malloc(sizeof(PE));
p->score = x;
strcpy(p->name, a);
p->number = number;
q = head;
if (q == NULL)
{
p->llink = NULL;
p->rlink = NULL;
head = p;
}
else
{
while (q->rlink != NULL)
{
q = q->rlink;
}
p->llink = q;
p->rlink = NULL;
q->rlink = p;
}
return head;
}

PE *dele(PE *head, int number)
{
PE *q;
if (!head)
{
cout << "双链表为空,无法进行删除" << endl;
return head;
}
q = head;
while (q&&q->number != number)
q = q->rlink;
if (!q)
{
cout << "未找到该编号,删除失败" << endl;
}
if (q == head&&head->rlink)
{
head = head->rlink;
head->llink = NULL;
free(q);
return head;
}
if (q == head&&!head->rlink)
{
free(q);
return NULL;
}
else
{
if (!q->rlink)
{
q->llink->rlink = NULL;
free(q);
return head;
}
else
{
q->llink->rlink = q->rlink;
q->rlink->llink = q->llink;
free(q);
return head;
}
}
}

void alter(PE *head, datatype x, int number)
{
PE *p;
p = head;
while (p->number != number&&p->rlink != NULL)
{
p = p->rlink;
}
if (p != NULL)
{
p->score = x;
}
}

void menu()
{
cout << endl;
cout << " 1.插入成绩" << endl;
cout << " 2.删除成绩" << endl;
cout << " 3.修改成绩" << endl;
cout << " 4.列印成绩" << endl;
cout << " 5.查询成绩" << endl;
cout << " 6.成绩排序" << endl;
cout << " 7.退 出" << endl;
}

void destory(PE *head)
{
PE *p;
while (head != NULL)
{
p = head;
head = head->rlink;
free(p);
}
}

PE* sort(PE *head)
{
PE *p;
p = head;
int a[20];
char s[20][10];
int number[20];
int i = 0;
if (p == NULL)
return NULL;
else
{
do
{
a[i] = p->score;
number[i] = p->number;
strcpy(s[i], p->name);
p = p->rlink;
i++;
} while (p != NULL);
}
destory(head);
int q, j, temp;
char name[10];
for (q = 0; q for (j = q + 1; j if (a[q]>a[j])
{
temp = a[q];
a[q] = a[j];
a[j] = temp;
temp = number[q];
number[q] = number[j];
number[j] = temp;
strcpy(name, s[q]);
strcpy(s[q], s[j]);
strcpy(s[j], name);
}
head = NULL;
for (q = 0; q<i; q++)
{
head = insert(head, number[q], s[q], a[q] );
}
return head;

}

void save(PE *head)
{
FILE *fp = fopen("score.txt", "w");
if (!fp)
{
cout << "打开文件失败!" << endl;
return;
}
PE *p;
p = head;
while (p)
{
fprintf(fp, " %d %s %d ", p->number, p->name, p->score);
p = p->rlink;
}
fclose(fp);

}

PE* read()
{
PE *head;
head = NULL;
FILE *fp = fopen("score.txt", "r");
if (!fp)
{
cout << "打开文件失败!" << endl;
return NULL;
}
char name[20];
int number;
int score;
while (!feof(fp))
{
fscanf(fp, "%d %s %d ", &number, name, &score);
if (number>0)
head = insert(head, number, name, score);
}
fclose(fp);
return head;
}

int main()
{
int choice;
PE *head;
char name[20];
head = read();
while (1)
{
menu();
cout << "请输入选项:";
cin >> choice;
switch (choice)
{
case 1:
{
int a, score, number;
cout << "请输入要输入的资料笔数:";
cin >> a;
for (int i = 0; i < a; i++)
{
cout << endl << "请输入编号:";
cin >> number;
cout << endl << "请输入姓名:";
cin >> name;
cout << endl << "请输入成绩:";
cin >> score;
head = insert(head, number, name, score);
}
break;
}
case 2:
{
int number;
cout << "请输入要删除的资料编号:";
cin >> number;
head = dele(head, number);
save(head);
break;
}
case 3:
{
int number, score;
cout << "请输入要修改的资料编号:";
cin >> number;
cout << "请输入更改后的成绩:";
cin >> score;
alter(head, score, number);
break;
}
case 4:
display(head);
break;
case 5:
{
int number;
PE *p;
cout << "请输入要查询的资料编号:";
cin >> number;
if (p = find(head, number))
{
cout << "编号 " << "姓名 " << "成绩" << endl;
cout << number <<" "<< p->name <<" "<< p->score<<endl;
}
break;
}
case 6:
{
head = sort(head);
display(head);
break;
}
case 7:
{
save(head);
destory(head);
exit(0);
}
default:
break;
}
}
return 0;
}

当我删除编号为2的数据后,查询编号为4的数据,显示编号不存在。查询编号为3的数据,显示的反而是原本的编号4,查询编号2,显示的是原本的编号3.
也就是在查询这个功能中,已删除的节点之后的数据的编号都会前移。

图片说明

图片说明

图片说明

图片说明

图片说明

求问各位大神这个该怎么解决?

1个回答

CSDNXIAOD
CSDNXIAOD   2016.04.28 19:34

从无头链表中删除节点
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!