Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8889405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:15:17+00:00 2026-06-14T22:15:17+00:00

My program is supposed to read a postfix expression and convert it to infix

  • 0

My program is supposed to read a postfix expression and convert it to infix and prefix using a tree implementation.
the pop() method always give the first element without errasing it and i can’t figure out why. Any help will be apreciate.

        //tree structur
        typedef struct asa {
          enum {  number_exp,sous_exp_op } type;
          union {
                  int                                    child;
                  struct {
                           struct asa*      left;
                           struct asa*      right;
                           char              oper; }       tree;
              } op;
        } asa;

        //stack
        typedef struct stack {
            int size;

                struct {
                  asa *  element;
                  struct stack* link;
                }e;

        } stack;

        struct stack *top;

        (...)

       asa * pop(){
        asa* e ;
        stack * temp;
        if(top->size == 0 ){
            printf("ERR0R : empty stack\n");
            exit (EXIT_FAILURE);
        }
        else if (top->size >= 1){
            temp = top->e.link;
            e= top->e.element;

            top = temp;
        }
        return e;
    }

void push(asa* node ){
    if(top->size == 0 ){
        top->e.element = node;
        top->e.link = NULL;
        top->size++;
    }
    else if (top->size > 0){
        pile * next = (pile*) malloc(sizeof(top));
        next = top;
        top->e.element = node;
        top->e.link = next;
        top->size++;
    }
}

Logs snapshot:

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:15:18+00:00Added an answer on June 14, 2026 at 10:15 pm

    Your immediate problems are that you are discarding next as soon as you allocate it when top->size > 0 and that you are allocating the size for a pointer rather than for the whole struct. To fix them, replace next = top with top = next at the end of the function and fix the sizeof invocation:

        else if (top->size > 0){
            pile * next = (pile*) malloc(sizeof(*top));
            next->e.element = node;
            next->e.link = top;
            next->size = top->size + 1;
            top = next;
        }
    

    Also, this implementation of stack feels needlessly complex and error-prone. If you need the stack size, you should maintain the size independently of the nodes of the linked list, not in every individual node. The standard linked list idiom is to represent the empty list (stack) as NULL, so neither push nor pop need any extra code to check for empty stack:

    typedef struct stack {
        asa *element;
        struct stack *next;
    } stack;
    
    void push(stack **head, asa *elem)
    {
      stack *new_head = malloc(sizeof(stack));
      new_head->next = head;
      new_head->elem = elem;
      *head = new_head;
    }
    
    asa *pop(stack **head)
    {
      stack *old_head = *head;
      asa *top_elem = old_head->elem;
      *head = old_head->next;
      free(old_head);
      return top_elem;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This program has 2 classes with a main method and is supposed to read
My program is supposed to read a text document with a bunch of numbers
I have simple serial port program that is supposed to read the serial port
My program is supposed to read an integer n from the user and then
I'm writing a program which is supposed to read two strings that can contain
My small program is supposed to read from a .txt file named map1.txt and
Working on a school project, the program is supposed to read from a text
I am supposed to write a program that should read from input numbers in
Brand new to C here. The program is supposed to read in a file
I having some trouble with an APCS assignment. The program is supposed to read

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.