Program to push and pop student information using stack
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 5 // Maximum number of students in the stack
typedef struct Student {
int roll_no;
char name[50];
float marks;
} node;
// Define a pointer type for stack
typedef struct stack {
node std[MAX];
int top;
} *st;
int isFull(st s) {
return s->top == MAX - 1;
}
int isEmpty(st s) {
return s->top == -1;
}
void push(st s, node temp) {
if (isFull(s)) {
printf("Stack Overflow: Cannot add more students.\n");
return;
}
s->top++;
s->std[s->top] = temp;
printf("Student pushed successfully.\n");
}
// Function to pop student from stack
void pop(st s) {
if (isEmpty(s)) {
printf("Stack Underflow: No student to pop.\n");
return;
}
printf("Popped student: %s (Roll No: %d)\n", s->std[s->top].name, s->std[s->top].roll_no);
s->top--;
}
// Function to display all students in the stack
void display(st s) {
if (isEmpty(s)) {
printf("Stack is empty.\n");
return;
}
printf("\nStudents in the stack:\n");
int i;
for ( i = s->top; i >= 0; i--)
{
printf("Roll No: %d, Name: %s, Marks: %.2f\n",
s->std[i].roll_no,
s->std[i].name,
s->std[i].marks);
}
}
// Main function with menu
int main() {
st s = (st)malloc(sizeof(struct stack)); // Allocate memory for the stack
if (!s)
{
printf("Memory allocation failed!\n");
return 1;
}
s->top = -1; // Initialize the top index to -1
node temp;
int choice;
while(1)
{
printf("\n--- Stack Operations ---\n");
printf("1. Push Student\n");
printf("2. Pop Student\n");
printf("3. Display Stack\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
if (isFull(s)) {
printf("Stack is full. Cannot add more students.\n");
break;
}
printf("Enter Roll Number: ");
scanf("%d", &temp.roll_no);
printf("Enter Name: ");
scanf(" %s", temp.name); // to read string with spaces
printf("Enter Marks: ");
scanf("%f", &temp.marks);
push(s, temp);
break;
case 2:
pop(s);
break;
case 3:
display(s);
break;
case 4:
printf("Exiting program.\n");
free(s); // Free the allocated memory for stack
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
Program - string palindrom
#include <stdio.h>
#include <string.h>
#define MAX 15
typedef struct Stack
{
char items[MAX];
int top;
} *St;
void push(St s, char value)
{
if (s->top < MAX - 1)
{
s->items[++(s->top)] = value;
}
}
// Pop from stack
char pop(St s)
{
if (s->top >= 0)
{
return s->items[(s->top)--];
}
return '\0'; // Return null character if stack is empty
}
int isPalindrome(St s,char arr[])
{ int length = strlen(arr);
int i;
for ( i = 0; i < length; i++)
{
push(s, arr[i]);
}
for ( i = 0; i < length; i++)
{
if (arr[i] != pop(s))
{
return 0; // Not a palindrome
}
}
return 1; // Palindrome
}
int main()
{
St s = (St)malloc(sizeof(struct Stack));
s->top=-1;
char arr[MAX]; // Array to store the input string
printf("Enter a string: ");
scanf("%s", arr);
if (isPalindrome(s,arr))
{
printf("The string is a palindrome.\n");
} else {
printf("The string is NOT a palindrome.\n");
}
return 0;
}
Rules for Manual Infix to Postfix Conversion
1. Operands (like variables or numbers):
o Directly copy operands to the output (postfix expression) in the order they appear.
o
2. Operators (+, −, ×, ÷, ^):
o Use a stack to hold operators.
o Pop from the stack to the output if the top of the stack has higher or equal
precedence (for left-associative operators).
o Push the current operator on the stack.
3. Precedence of Operators (from highest to lowest):
o ^ (Exponentiation)
o *, / (Multiplication, Division)
o +, - (Addition, Subtraction)
4. Associativity:
o ^ is right-associative
o All other operators (+, -, *, /) are left-associative
5. Parentheses:
o Left parenthesis (: Always push to the stack.
o Right parenthesis): Pop from the stack to the output until a left parenthesis ( is
encountered. Discard both parentheses.
6. At the End:
o Pop all remaining operators from the stack to the output.