C语言 栈 输入字母可以输出,但输入文字输出不了,代码没有问题的,在命令提示符可以,在vscode不行。
#include <stdio.h>
#include <stdlib.h>
struct NODE
{
char name[30];
float score;
struct NODE * next;
};
struct STACK
{
struct NODE * top;
struct NODE *bottom;
};
struct STACK * CreateStack(void)
{
struct STACK *S =(struct STACK *)malloc(sizeof*S);
S->top=S->bottom=(struct NODE *)malloc(sizeof*S->top);
if (NULL==S->top)
{
printf("内存不足,分配失败\n");
exit(-1);
}
S->top->next=NULL;
return S;
}
int StackEmpty(struct STACK *S)
{
if (S->bottom==S->top)
{
return 1;
}
else
{
return 0;
}
}
struct NODE * Push(struct NODE * top)
{
struct NODE * node=(struct NODE *)malloc(sizeof*node);
if (NULL==node)
{
printf("内存不足,分配失败\n");
exit(-1);
}
node->score=-123456;
while (1)
{
printf("请输入学生姓名,成绩:");
scanf("%s %f",node->name,&node->score);
while (getchar()!='\n');
if (node->score==-123456)
{
printf("输入的成绩不符合规范,请重新输入\n");
}
else
{
break;
}
}
node->next=top;
top=node;
return top;
}
void OutputStack(struct STACK *S)
{
struct NODE * move=S->top;
while (S->bottom!=move)
{
printf("[姓名:%s,成绩:%.1f]->",move->name,move->score);
move=move->next;
}
printf("[^]\n");
}
void GetTop(struct NODE * top)
{
printf("姓名:%s,成绩%.1f\n",top->name,top->score);
}
struct NODE * Pop(struct NODE * top)
{
struct NODE * buf;
buf=top;
top=top->next;
free(buf);
buf=NULL;
return top;
}
void DestroyStack(struct STACK *S)
{
while (!(StackEmpty(S)))
{
S->top=Pop(S->top);
}
}
int main(void)
{
int num;
int ret;
struct STACK *S;
printf("是否创建栈(Y/N):");
while (1)
{
char ch;
scanf("%c",&ch);
while (getchar()!='\n');
if ('Y'==ch || 'y'==ch)
{
S=CreateStack();
break;
}
else if ('N'==ch || 'n'==ch)
{
return 0;
}
else
{
printf("请重新输入(Y/N):");
}
}
printf("1、判栈空\n2、压栈\n3、输出整个栈\n4、取栈顶结点\n5、出栈\n6、销毁栈\n7、退出\n");
while (1)
{
num=-1;
printf("请输入操作号:");
scanf("%d",&num);
while (getchar()!='\n');
switch (num)
{
case 1:
{
ret=StackEmpty(S);
if (1==ret)
{
puts("栈为空");
}
else if (0==ret)
{
puts("栈为非空");
}
break;
}
case 2:
{
S->top=Push(S->top);
break;
}
case 3:
{
OutputStack(S);
break;
}
case 4:
{
GetTop(S->top);
break;
}
case 5:
{
if (!(StackEmpty(S)))
{
S->top=Pop(S->top);
printf("出栈成功\n");
}
else
{
puts("栈空,出栈失败");
}
break;
}
case 6:
{
DestroyStack(S);
puts("栈已销毁");
break;
}
case 7:return 0;break;
default:
{
puts("输入错误,请重新输入!");
}
}
}
return 0;
}
这可以的呀
使用for循环将一个字符串复制到另一个字符串数中,在控制台输出字符串,出现了一堆中文乱码,如下图:
根据给出的参考资料,我们可以看到你在使用栈数据结构输出字符时成功,但当输入文字时却无法正确输出。这可能是因为栈的数据类型为int,在读取文字时无法正确处理。为了解决这个问题,我们需要修改栈的数据类型为字符型。
以下是解决方案:
首先,将栈的数据类型修改为字符型。修改Sqstack结构体的base和top的数据类型为char*。
typedef struct Stack
{
char* base;
char* top;
int stacksize;
}Sqstack;
然后,在push和pop函数中,修改指针的数据类型为char*,并将int类型的数据改为char类型。
void push(Sqstack* S, char e)
{
// 省略部分代码
*S->top = e;
S->top++;
}
void pop(Sqstack* S)
{
// 省略部分代码
char e;
if (!StackEmpty(*S))
{
S->top--;
e = *S->top;
printf("%c ", e);
}
else
{
printf("Stack is empty!\n");
}
}
最后,在reverse_list函数中,将push的参数改为字符类型。
void reverse_list(Node* head, Sqstack* S)
{
Node* p = head->next;
while (p)
{
push(S, p->data);
p = p->next;
}
while (S->top != S->base)
pop(S);
printf("\n");
}
通过以上的修改,栈数据结构就可以正确处理字符类型的输入,从而解决了输出文字时无法正确显示的问题。
完整代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct Node
{
char data;
struct Node* next;
} Node;
#define true 1
#define false 0
#define STACK_MAXSIZE 100
#define STACK_INCREMENT 10
typedef struct Stack
{
char* base;
char* top;
int stacksize;
} Sqstack;
void InitStack(Sqstack* S)
{
S->base = (char*)malloc(STACK_MAXSIZE * sizeof(char));
if (!S->base)
{
printf("ERROR!\n");
}
S->top = S->base;
S->stacksize = STACK_MAXSIZE;
}
int StackEmpty(Sqstack S)
{
if (S.top == S.base)
{
return true;
}
else
{
return false;
}
}
void push(Sqstack* S, char e)
{
if (S->top - S->base >= S->stacksize)
{
S->base = (char*)realloc(S->base,
(S->stacksize + STACK_INCREMENT) * sizeof(char));
assert(S->base);
S->stacksize += STACK_INCREMENT;
S->top = S->base + S->stacksize;
}
*S->top = e;
S->top++;
}
void pop(Sqstack* S)
{
char e;
if (!StackEmpty(*S))
{
S->top--;
e = *S->top;
printf("%c ", e);
}
else
{
printf("Stack is empty!\n");
}
}
void print_list(Node* head);
Node* Init_List()
{
Node* head = (Node*)malloc(sizeof(Node));
assert(head);
int size;
printf("please enter list sizes: ");
scanf("%d", &size);
head->data = '0';
head->next = NULL;
Node* tail = head;
for (int i = 0; i < size; i++)
{
Node* new = (Node*)malloc(sizeof(Node));
assert(new);
char first;
scanf(" %c", &first);
new->data = first;
tail->next = new;
new->next = NULL;
tail = tail->next;
}
return head;
}
void reverse_list(Node* head, Sqstack* S)
{
Node* p = head->next;
while (p)
{
push(S, p->data);
p = p->next;
}
while (S->top != S->base)
pop(S);
printf("\n");
}
void print_list(Node* head)
{
Node* p;
p = head->next;
while (p != NULL)
{
printf("%c ", p->data);
p = p->next;
}
printf("\n");
}
int main(int argc, char const* argv[])
{
Node* list = Init_List();
Sqstack S;
InitStack(&S);
reverse_list(list, &S);
return 0;
}
这样,你就可以正确输出文字了。希望对你有帮助!
供参考:https://blog.csdn.net/qq_43570634/article/details/130034645