【发布时间】:2017-11-16 01:54:12
【问题描述】:
当我在堆栈中插入一个字符串时,它会添加它,但也会更改之前的所有元素。
void push(linkedList *ll,char *item) {
node *newNode = malloc(sizeof(node));
if (ll->size == 0) {
newNode->value = item;
newNode->next = NULL;
ll->head = newNode;
ll->tail = newNode;
ll->size++;
}
else {
newNode->value = item;
newNode->next = ll->head;
ll->head = newNode;
ll->size++;
}
}
int main() {
linkedList* stack = init();
int scan = 1;
char * p = malloc(sizeof(char) * 5);
while (scan = 1) {
scanf("%1s", p);
if (*p >= '0' && *p <= '9') {
push(stack, p);
print(stack);
}
}
WINPAUSE;
}
输出看起来像这样:
插入 5
5
插入 7
7 --> 7
插入 8
8 --> 8 --> 8
【问题讨论】:
标签: c pointers linked-list stack