我想做堆栈的链表实现,但是我的代码没有按预期工作。 我无法发现导致问题的原因,因此我将发布我的代码、当前输出和理想输出。 我不知道如何添加 n 是字母的 if 条件,然后执行 if 语句作为输入,如 no。 456 或执行else语句。 如果您解决了问题,请帮忙。
代码:
#include <stdio.h>
#include <stdlib.h>
struct node{
char data;
struct node *next;
};
struct node*head;
void push(char x){
if (head == NULL) {
head= (struct node*) malloc(sizeof(struct node));
head->data=x;
head->next=NULL;
printf("additon successful\n");
return ;
}
struct node *temp = (struct node*) malloc(sizeof(struct node));
temp->next=head;
temp->data=x;
head=temp;
printf("additon successful\n");
return ;
}
void pop(){
if(head == NULL){
printf("list is empty\n");
return ;
}
struct node *temp = (struct node*) malloc(sizeof(struct node));
temp=head;
head=head->next;
free(temp);
printf("pop is successful\n");
return ;
}
void print(){
struct node *temp = (struct node*) malloc(sizeof(struct node));
temp=head;
while(temp != NULL){
printf("%c ",temp->data);
temp=temp->next;
}
printf("\nall items printed\n");
return ;
}
void main() {
char n;
printf("enter alphabet\n");
while (0!=1){
scanf("%c",&n);
if(n != 1 && n!= '`' && n!= 4 ) {
push(n);}
else if(n == 1) {
pop();}
else if(n == '`') {
print();}
else if(n == 4) {
printf("\nexiting program");
return ;}
else {
printf("enter valid argument\n");}
}
}
输出:
enter alphabet
r
additon successful // why is this printing twice?
additon successful
a
additon successful
additon successful
n
additon successful
additon successful
d
additon successful
additon successful
1 // for 1 code should perform pop operation
additon successful
additon successful
4 // for 4 function should end
additon successful
additon successful
456 // i could not figure how to add in if condition that n is
aplphabet then execute if statement for such no. like 456
execute else
additon successful
additon successful
additon successful //why so many executions?
additon successful
理想输出:
1
list is empty
r
additon successful
e
additon successful
a
additon successful
d
additon successful
1
pop is successful
1
pop is successful
`
a d
all items printed
456
enter valid argument
4
exiting program
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
struct node{
char data;
struct node *next;
};
struct node*head;
void push(char x){
if (head == NULL) {
head= (struct node*) malloc(sizeof(struct node));
head->data=x;
head->next=NULL;
printf("additon successful\n");
return ;
}
struct node *temp = (struct node*) malloc(sizeof(struct node));
temp->next=head;
temp->data=x;
head=temp;
printf("additon successful\n");
return ;
}
void pop(){
if(head == NULL){
printf("list is empty\n");
return ;
}
struct node *temp;
temp=head;
head=head->next;
free(temp);
printf("pop is successful\n");
return ;
}
void print(){
struct node *temp;
temp=head;
while(temp != NULL){
printf("%c ",temp->data);
temp=temp->next;
}
printf("\nall items printed\n");
return ;
}
int main() {
char n;
printf("enter alphabet\n");
while (0!=1){
scanf(" %c",&n);
if(isalpha(n)) {
push(n);}
else if(n == '1') {
pop();}
else if(n == '`') {
print();}
else if(n == '4') {
printf("\nexiting program");
return 0;}
else {
printf("enter valid argument\n");}
}
}
最终解决方案