c

Error in linked list implementation of stack


Problem: I’m implementing a stack with a linked list, but it’s not working correctly. I’ll share my code along with the current and expected outputs. I also need help writing an if condition that checks whether the input n is alphabetic (e.g. “no. 456”)—if it is, run one branch; otherwise, run the other. Please let me know what’s causing the problem.

Code:

#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");}
    }
}

Output:

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

Expectation:

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

Solution

  • #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>           //added this library to use 'isalpha' a 
                                 // function to check wheteher the symbol is 
                                 //alphabet or not.
    
    
    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;             //removed malloc since we only need a 
                                   //pointer and not a memory block
    temp=head;
    head=head->next;
    free(temp);
    printf("pop is successful\n");
    return ;
    }
    
    void print(){
        struct node *temp;           //removed malloc since we only need 
                                     //a pointer and not a memory block
        temp=head;
        while(temp != NULL){
            printf("%c  ",temp->data);
            temp=temp->next;
        }
        printf("\nall items printed\n");
        return ;
    }
    int main() {                //void main() isn't very portable. Most 
                                //implementations expect main to return int.
        char n;
        printf("enter alphabet\n");
        while (0!=1){
        scanf(" %c",&n);       //the program prints twice the message 
                               //"additon successful" because it reads the 
                               //new line character '\n'. Change this call 
                               //scanf("%c",&n); to scanf(" %c",&n); Pay 
                               //attention to the leading space in the 
                               //format string. It allows to skip white 
                               //space characters.    
        if(isalpha(n)) {                   
        push(n);}
        else if(n == '1') {      //'1' instead of 1
        pop();}
        else if(n == '`') {
        print();}
        else if(n == '4') {      //used' 4' instead of 4
        printf("\nexiting program");
        return 0;}
        else {
        printf("enter valid argument\n");}
        }
    }
    

    final solution