0% found this document useful (0 votes)
3 views

Program With Comments[1]

The document contains multiple C programs demonstrating data structures such as stacks and queues, including their operations like insertion, deletion, and evaluation of postfix expressions. It features user details and prompts for input, showcasing how to convert infix expressions to postfix as well. Each program includes comments explaining the functionality and structure.

Uploaded by

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

Program With Comments[1]

The document contains multiple C programs demonstrating data structures such as stacks and queues, including their operations like insertion, deletion, and evaluation of postfix expressions. It features user details and prompts for input, showcasing how to convert infix expressions to postfix as well. Each program includes comments explaining the functionality and structure.

Uploaded by

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

 PROGRAM WITH COMMENTS:

#include<stdio.h>
#define size 10
char stack[size];
int top = -1;,nknkl.m
void puch(int);
int pop();
int eva_Post(char*);
int main()
{
char postExp[size];
printf("Name: Harsh Singh\n Roll No.:11500323031");
printf("\nEnter the postfix expression: ");
scanf("%s",postExp);
printf("\nPostfix Evaluation Result: %d\n",eva_Post(postExp));
return 0;
}
void push(int value)
{
if(top != size - 1)
{
top++;
stack[top] = value;
}
else
printf("\n OVERFLOW!");
}
int pop()
{size
if(top != -1)
{
int value = stack[top];
top--;
return value;
}
else
{
printf("\n UNDERFLOW!");
return -1;
}
}
int eva_Post(char* exp)
{
int i, oper2, oper1, val;
for(i=0;exp[i]!='\0';i++)
{
if(exp[i]>='0' && exp[i]<='9')
push(exp[i] - '0');
else
{
oper2 = pop();
oper1 = pop();
switch(exp[i])
{
case '+':val = oper1 + oper2;
push(val);
break;
case '-':val = oper1 - oper2;
push(val);
break;
case '*':val = oper1 * oper2;
push(val);
break;
case '/':val = oper1 / oper2;
push(val);
break;
default:printf("\nINVALID OPERATOR: %c\n!!!",exp[i]);
break;
}
}
}return pop();
}

 OUTPUT OF THE PROGRAM:


 PROGRAM WITH COMMENTS:
#include <stdio.h>

#define MAX 5

int queue[MAX];
int front = -1, rear = -1;

void insert(int data) {


if (rear == MAX - 1) {
printf("Queue Overflow\n");
return;
}

if (front == -1) front = 0;


rear++;
queue[rear] = data;
printf("Inserted: %d\n", data);
}

void delete() {
if (front == -1 || front > rear) {
printf("Queue Underflow\n");
return;
}
printf("Deleted: %d\n", queue[front]);
front++;
if (front > rear) front = rear = -1;
}
void display() {
if (front == -1) {
printf("Queue is empty\n");
return;
}
printf("Queue elements: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}

int main() {
printf("Name: HARSH SINGH\n");
printf("Roll No: 11500323031\n\n");

int choice, data;

while (1) {
printf("\nQueue Operations:\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
insert(data);
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice! Please try again.\n");
}
}
}
 OUTPUT OF THE PROGRAM:
 PROGRAM WITH COMMENTS:
#include <stdio.h>
#define MAX 5

int queue[MAX];
int front = -1, rear = -1;

void enqueue(int element) {


if ((front == 0 && rear == MAX - 1) || (rear == (front - 1) % (MAX - 1))) {
printf("Queue Overflow\n");
} else {
if (front == -1) { // Queue is initially empty
front = rear = 0;
} else if (rear == MAX - 1 && front != 0) { // Wrap around
rear = 0;
} else {
rear++;
}
queue[rear] = element;
printf("Inserted element: %d\n", element);
}
}

void dequeue() {
if (front == -1) {
printf("Queue Underflow\n");
} else {
printf("Deleted element: %d\n", queue[front]);
if (front == rear) { // Queue becomes empty after deletion
front = rear = -1;
} else if (front == MAX - 1) { // Wrap around
front = 0;
} else {
front++;
}
}
}

void display() {
if (front == -1) {
printf("Queue is empty\n");
} else {
printf("Front index: %d\n", front);
printf("Queue elements: ");
int i = front;
while (1) {
printf("%d ", queue[i]);
if (i == rear) break;
i = (i + 1) % MAX;
}
printf("\nRear index: %d\n", rear);
}
}

int main() {
// Display user details
printf("Name: HARSH SINGH\n");
printf("Roll No: 11500323031\n");
int choice, value;
while (1) {
printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to be inserted: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
}
 OUTPUT OF THE PROGRAM:
 PROGRAM WITH COMMENTS:
#include <stdio.h>
#include <string.h>

#define MAX 100

// Stack structure
char stack[MAX];
int top = -1;

// Function to push an element onto the stack


void push(char elem) {
stack[++top] = elem;
}

// Function to pop an element from the stack


char pop() {
return stack[top--];
}

// Function to check if a character is an alphabet or digit


int isAlnum(char ch) {
return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9');
}
// Function to get precedence of operators
int precedence(char elem) {
switch (elem) {
case '+':
case '-': return 1;
case '*':
case '/': return 2;
case '^': return 3;
default: return 0;
}
}

// Function to convert infix expression to postfix


void infixToPostfix(char* infix, char* postfix) {
int i = 0, k = 0;
char ch;
while ((ch = infix[i++]) != '\0') {
if (ch == '(') {
push(ch);
} else if (isAlnum(ch)) {
postfix[k++] = ch;
} else if (ch == ')') {
while (stack[top] != '(') {
postfix[k++] = pop();
}
pop(); // Remove '(' from stack
} else {
while (top != -1 && precedence(stack[top]) >= precedence(ch)) {
postfix[k++] = pop();
}
push(ch);
}
}
// Pop all remaining operators from stack
while (top != -1) {
postfix[k++] = pop();
}
postfix[k] = '\0'; // Null-terminate the postfix expression
}

int main() {
char infix[MAX], postfix[MAX];
printf("Name: HARSH SINGH\n");
printf("Roll No: 11500323031\n\n");
printf("Enter infix expression: ");
scanf("%s", infix);
infixToPostfix(infix, postfix);
printf("Postfix expression: %s\n", postfix);
return 0;
}

OUTPUT OF THE PROGRAM:

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