Using STACK and infix and postfix

  • Thread starter Mikeyjoe512
  • Start date
In summary, the code should try to parse the string as infix and then postfix notation, and the result should be the value of the arithmetic operator in postfix notation.
  • #1
Mikeyjoe512
1
0
Mod note: please use [noparse]
Code:
 and
[/noparse] tags around your code, to preserve your indentation and improve readability. I have done this to your code.


I'm using DEV C++ for my class and I have to use infix and postfix in my STACK. The sign is that has to be in infix and post fix is : //
Code:
#include <stdio.h>
#include <stdlib.h>

//STACK

struct node
{
       char *info;
       node *next;
};

node *top;

void push(node *n)
{
     if(top == NULL)
     {
            top = n;
            return;
     }
     n->next = top;
     top = n;
}

node *pop()
{
     if(top == NULL) return NULL;
     node *temp;
     temp = top;
     top = top->next;
     return temp;
}

bool isEmpty()
{
     return (top == NULL);
}

int main()
{
    //top = NULL;
    
    //push(new node("Michael"));
    //push(new node("Justin"));
    
    //printf("%s\n", pop()->info);
    
    //push(new node("Saundra"));
    //push(new node("Jerry"));
    
    //while(!isEmpty())
    //{
                    // printf("%s\n", pop()->info);
    //}
    
    system("pause");
    return 0;
}
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
I don't understand what you're asking -
Mikeyjoe512 said:
I have to use infix and postfix in my STACK. The sign is that has to be in infix and post fix is : //

Can you give an example of what you're trying to do?
 
  • #3
Mikeyjoe512 said:
infix and postfix
infix and postfix are two types of notations for mathematical expressions (another type is prefix). Wiki article for infix notation, which is the notation most people normally use:

http://en.wikipedia.org/wiki/Infix_notation

postfix notation is also called reverse polish notation, wiki article:

http://en.wikipedia.org/wiki/Reverse_Polish_notation

My guess is that you're suppose to parse an string converting infix notation to postfix notation or vice versa, using a stack for the conversion process, or that you're supposed to write two programs to evaluate expressions, one program handles infix notation, the other postfix notation.
 
  • #4
In addition to what rcgldr said, the commented-out code in what you posted is working with the names of people. Instead it should probably be working with strings that contain numbers and arithmetic operators, with the goal being to evaluate a string such as "5 + 8" (infix) and "5 8 +" (postfix). The result from each of these strings should be 13.
 
  • #5


I would say that the use of STACK and infix and postfix notations is a common and useful approach in computer science. It allows for efficient storage and retrieval of data, and can be applied to various algorithms and data structures. In this case, the code provided is a simple implementation of a STACK using the C++ language. The use of infix and postfix notations allows for a clear and concise representation of mathematical expressions, which can be easily evaluated and manipulated by the computer. Overall, the use of STACK and infix and postfix notations is a valuable tool in computer science and can be applied in many different applications.
 

FAQ: Using STACK and infix and postfix

What is a stack and how does it differ from infix and postfix?

A stack is a data structure that follows the "last in, first out" (LIFO) principle, meaning that the last item added to the stack will be the first one removed. Infix and postfix are ways of writing mathematical expressions, with infix being the traditional notation where operators are placed between operands, and postfix being a notation where operators are placed after their operands. Stacks are commonly used to evaluate infix and postfix expressions.

How does using a stack make evaluating infix and postfix expressions more efficient?

Using a stack allows for efficient evaluation of infix and postfix expressions because it eliminates the need for parentheses and operator precedence rules. In infix notation, parentheses are used to specify the order of operations, while in postfix notation, the order is determined by the position of the operators. This eliminates the need for extra steps in evaluating the expression, making the process more efficient.

Can any expression be written in both infix and postfix notation?

Yes, any expression can be written in both infix and postfix notation. Infix and postfix are just different ways of representing the same mathematical expression. However, the resulting postfix expression may not be unique, as there can be multiple postfix expressions that evaluate to the same value.

How does the conversion from infix to postfix notation work?

The conversion from infix to postfix notation involves using a stack to keep track of operators and parentheses. Starting from the leftmost character in the infix expression, each character is scanned and added to the resulting postfix expression according to certain rules. When an operator is encountered, it is compared to the top operator on the stack, and if the top operator has higher precedence, it is popped off the stack and added to the postfix expression. This process continues until the top operator on the stack has lower precedence, or until the stack is empty. When a closing parenthesis is encountered, all operators on the stack are popped off and added to the postfix expression until the matching opening parenthesis is found.

What are some real-world applications of using a stack to evaluate infix and postfix expressions?

Stacks are commonly used in computer programming for tasks such as evaluating mathematical expressions, parsing, and implementing algorithms. They are also used in web browsers to keep track of visited pages and in undo/redo functionality in text editors. Additionally, stacks are used in memory management and function call mechanisms in programming languages.

Similar threads

Replies
5
Views
4K
Replies
1
Views
9K
Replies
3
Views
1K
Replies
11
Views
4K
Replies
20
Views
4K
Replies
1
Views
2K
Back
Top