我编写了如下代码并遇到问题。我想在我的列表中添加任务名称,但是当我输入不同的名称并尝试显示它们时,它会显示我为列表中的每个元素输入的最新名称。我怎么才能纠正?我的代码是用土耳其语写的,但我相信你明白了。 (我也在评论栏中翻译)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct element // element
{
char *taskName; // task name
struct element *next; // next
};
typedef struct element Element;
typedef Element *Elementptr;
Elementptr new_element(char *taskName) // new element
{
Elementptr element = (Element *)malloc(sizeof(Element));
element->taskName = taskName;
element->next = NULL;
return element;
}
struct stack // stack
{
Elementptr top; // top
};
typedef struct stack stack;
typedef stack *stackptr;
stackptr new_stack() // new stack
{
stackptr stack = (stack *)malloc(sizeof(stack));
stack->top = NULL;
return stack;
}
Elementptr top(stackptr c) // top
{
return c->top;
}
int stack_empty(stackptr c) // empty stack
{
if (c->top == NULL)
{
return 1;
}
else
{
return 0;
}
}
void stack_add(stackptr c, Elementptr new) // add stack
{
new->next = c->top;
c->top = new;
}
Elementptr stack_delete(stackptr c) // delete stack
{
Elementptr e = c->top;
if (!stack_empty(c))
{
c->top = c->top->next;
}
return e;
}
void stack_write(stackptr c) // write stack
{
printf("\n\n*** Stack'te Bulunan Gorevler ***\n");
Elementptr p;
int sayac = 0;
while (!stack_empty(c))
{
p = stack_delete(c);
sayac++;
printf("\nStack d. gorevi: s\n", sayac, p->taskName);
}
}
int main()
{
int devam; // continue
char gorevIsmi[32]; // task name
bool dogruMu = true; // isTrue
stackptr test = new_stack();
printf("-- Gorev addmeyi sonlandirmak icin \"157\" giriniz --\n"); // to stop program enter 157
do
{
printf("\nenter task name: "); // enter task name
scanf("s", gorevIsmi);
Elementptr new = new_element(gorevIsmi);
stack_add(test, new);
printf("\nDevam durumu giriniz (Rakam): ");
scanf("d", &devam);
if (devam == 157)
{
dogruMu = false;
}
} while (dogruMu == true);
stack_write(test);
}