0% found this document useful (0 votes)
3 views4 pages

DS Ass5

The document outlines an experiment to implement a stack using a linked list in C for converting infix expressions to postfix expressions. It includes an algorithm detailing the steps for conversion, source code for the implementation, and a sample output indicating successful completion. The program reads an infix expression, processes it, and outputs the corresponding postfix expression.

Uploaded by

mesaran27s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

DS Ass5

The document outlines an experiment to implement a stack using a linked list in C for converting infix expressions to postfix expressions. It includes an algorithm detailing the steps for conversion, source code for the implementation, and a sample output indicating successful completion. The program reads an infix expression, processes it, and outputs the corresponding postfix expression.

Uploaded by

mesaran27s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

EXPERIMENT NO:5

DATE:07 / 08 / 2024

IMPLEMENTATION OF STACK USING LINKED LIST(CONVERSION OF INFIX


TO POSTFIX)

AIM:

To Create C program to implement the stack and perform the conversion of infix
expression ro postfix expression

ALGORITHM:

1. Start
2. Initialize an empty stack `top`.
3. Read the infix expression as a string `a`.
4. For each character `ch` in the string `a`:
- If `ch` is an operand (i.e., a letter or a digit):
- Print `ch`.
- Else if `ch` is an open parenthesis `(`:
- Push `ch` onto the stack.
- Else if `ch` is a close parenthesis `)`:
- While the stack is not empty and the top of the stack is not an open parenthesis
`(`:
- Pop from the stack and print the popped operator.
- Pop the open parenthesis `(` from the stack.
- Else (it is an operator):
- While the stack is not empty and the precedence of the operator on the top of
the stack is greater than or equal to the precedence of `ch`:
- Pop from the stack and print the popped operator.
- Push `ch` onto the stack.
5. While the stack is not empty:
- Pop from the stack and print the popped operator.
6. End

SOURCE CODE :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node {
char ope;
struct node *next;
} *top = NULL;
void push(char op) {
struct node* nn = (struct node*)malloc(sizeof(struct node));
nn->ope = op;
nn->next = top;
top = nn;
}

char peek() {
return top ? top->ope : '\0';
}

char pop() {
if (top == NULL) {
printf("Stack is empty!\n");
return '\0';
}
char op = top->ope;
struct node* temp = top;
top = top->next;
free(temp);
return op;
}

int prior(char op) {


if (op == '+' || op == '-')
return 1;
else if (op == '*' || op == '/')
return 2;
else if (op == '^')
return 3;
return 0;
}

int main() {
char a[20];
printf("Enter the infix expression: ");
scanf("%s", a);

int l = strlen(a);
int i;

for (i = 0; i < l; i++) {


if (a[i] >= '0' && a[i] <= '9' || a[i] >= 'a' && a[i] <= 'z' || a[i] >= 'A' && a[i] <= 'Z') {
printf("%c", a[i]);
}
else if (a[i] == '(') {
push(a[i]);
}
else if (a[i] == ')') {
while (top != NULL && peek() != '(') {
printf("%c", pop());
}
if (top != NULL) {
pop();
}
}
else {
while (top != NULL && prior(peek()) >= prior(a[i])) {
printf("%c", pop());
}
push(a[i]);
}
}

while (top != NULL) {


printf("%c", pop());
}

printf("\n");
return 0;

}
SAMPLE OUTPUT :

RESULT:

Thus the c program to Convert the Infixt to Postfix expression is Successfully


completed and verified.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy