【发布时间】:2021-07-26 01:05:21
【问题描述】:
我尝试为链表创建几个函数。这是所有这些,但我对函数 delete_All 和 delete 有问题。
delete_All需要删除所有给定值的元素,只有delete首先。
我找不到的第一个错误是free(_)(在代码中标记为“HERE!!!”),当我们同时放入链接列表的head 时,delete_All 中的程序工作也无穷无尽在下一个需要删除的元素值中。
例如 (1,1,2,3,4,) 不能删除第一对“1”。 delete 和其他一些函数不能使用free 函数。
#include <stdlib.h>
#include <stdio.h>
struct uzel {
int n;
struct uzel *next;
};
struct uzel *
initializef(struct uzel *head)
{
static int c = 0;
int a;
printf("uzel[%d] = ", c);
scanf("%d", &a);
if (a == 0) {
head->next = NULL;
}
else {
c++;
head->n = a;
head->next = malloc(sizeof(struct uzel));
initializef(head->next);
}
return head;
}
void
gprint(struct uzel *head)
{
printf("(");
while (head->next != NULL) {
printf("%d ", head->n);
head = head->next;
}
printf(")\n");
}
struct uzel *
delete(struct uzel *head, int val)
{
if (head->n == val) {
struct uzel *newhead = head;
newhead = head->next;
struct uzel *del = head;
// free(del);//HERE!!!!!!!!!
return newhead;
}
struct uzel *right = head;
struct uzel *left = right;
while (left->next) {
right = left->next;
if (right->next != NULL) {
if (right->n == val) {
struct uzel *del = right;
left->next = right->next;
free(del);
}
} // but here is ok!!!
left = left->next;
}
return head;
}
struct uzel *
delete_all(struct uzel *head, int val)
{
struct uzel *newhead = head;
while (newhead->n == val) {
newhead = head->next;
// struct uzel* del=head;//here!!!!!!!!!!!!!!
// free(del);
if ((newhead->n) == NULL) {
printf("No more elements to work with,ERROR");
return 0;
}
}
struct uzel *right = newhead;
struct uzel *left = newhead;
while (left->next) {
right = right->next;
if (right->n == val) {
struct uzel *tmp = right;
left->next = right->next;
free(tmp);
}
else {
left = left->next;
}
}
return newhead;
}
void
addAfter(int info, int after_what, struct uzel *head)
{
struct uzel *left = head;
struct uzel *right = head;
while ((left->n) != after_what) {
right = right->next;
left = left->next;
}
right = right->next;
struct uzel *newelem = malloc(sizeof(struct uzel));
newelem->n = info;
newelem->next = right;
left->next = newelem;
}
void
addAfterALL(int info, int after_what, struct uzel *head)
{
struct uzel *left = head;
struct uzel *right = head;
while ((right->next)) {
while (((left->n) != after_what)) {
right = right->next;
left = left->next;
}
right = right->next;
struct uzel *newelem = malloc(sizeof(struct uzel));
newelem->n = info;
newelem->next = right;
left->next = newelem;
}
}
int
main()
{
int a, b;
struct uzel head2;
struct uzel *mainhead1 = initializef(&head2);
gprint(mainhead1);
printf("Enter a number to delete: ");
scanf("%d", &a);
printf("Delete all %d ? If yes enter 2 if no enter 1: ", a);
scanf("%d", &b);
if (b == 2) {
struct uzel *mainhead = delete_all(mainhead1, a);
gprint(mainhead);
}
else {
struct uzel *mainhead = delete(mainhead1, a);
gprint(mainhead);
}
printf("Enter after what number u want to insert smth: ");
int r;
scanf("%d", &r);
printf("Enter what number u want to insert: ");
int u;
scanf("%d", &u);
printf("After all numbers %d ? If yes enter 1 If no enter 2:", r);
int g;
scanf("%d", &g);
if (g == 2) {
addAfter(u, r, mainhead1);
gprint(mainhead1);
}
if (g == 1) {
addAfterALL(u, r, mainhead1);
gprint(mainhead1);
}
}
【问题讨论】:
-
这不是 C++ 所以不要使用 C++ 标签。
-
"当我们放入链表的“头”并同时放入下一个需要删除的元素值时,“delete_All”中的程序也无休止地工作“这甚至意味着什么?试着提出一个具体的问题。
-
你说删除不起作用,但甚至不告诉我们实际问题。
-
@lulle 已编辑