[go: up one dir, main page]

0% found this document useful (0 votes)
37 views12 pages

Stack Programs

The document contains multiple C programs demonstrating stack implementations using arrays and linked lists, including operations like push, pop, peep, and display. It also includes programs for checking balanced parentheses, evaluating postfix expressions, converting infix to postfix, and checking if a string is a palindrome. Each program is structured with a main function and relevant stack operations, showcasing different applications of stack data structures.

Uploaded by

medidhag54
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)
37 views12 pages

Stack Programs

The document contains multiple C programs demonstrating stack implementations using arrays and linked lists, including operations like push, pop, peep, and display. It also includes programs for checking balanced parentheses, evaluating postfix expressions, converting infix to postfix, and checking if a string is a palindrome. Each program is structured with a main function and relevant stack operations, showcasing different applications of stack data structures.

Uploaded by

medidhag54
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/ 12

Program: Stack implementation using arrays.

#include <stdio.h>
#define SIZE 10
int top = -1, arr[SIZE];

void push();
void pop();
void peep();
void display();

int main()
{
int choice;
printf("Operations of Stack:\n");
printf("1.Push\n2.Pop\n3.Peep\n4.Display\n5.Exit\n");
while(1)
{
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch(choice)
{
case 1: push(); break;
case 2: pop(); break;
case 3: peep(); break;
case 4: display(); break;
case 5: return 0;
default: printf("\nInvalid choice, try again.\n");
}
}
}

void push()
{
int n;
if(top == SIZE - 1)
{
printf("\nStack overflow\n");
}
else
{
printf("Enter the element to be added onto the stack: ");
scanf("%d", &n);
top = top + 1;
arr[top] = n;
}
}

void pop()
{
if(top == -1) //w.s.enmpty
{
printf("\nStack underflow\n");
}
else
{
printf("Popped element: %d\n", arr[top]);
top = top - 1;
}
}

void peep()
{
if(top == -1)
{
printf("\nStack is empty, nothing to peep.\n");
}
else
{
printf("Top element of the stack: %d\n", arr[top]);
}
}

void display()
{
int i;
if(top == -1)
{
printf("\nStack is empty (underflow)\n");
}
else
{
printf("\nElements present in the stack:\n");
for(i = top; i >= 0; i--)
{
printf("%d\t", arr[i]);
}
}
}
Program: Stack implementation using linked list.

#include <stdio.h>
#include <stdlib.h>

struct Node
{
int data;
struct Node* next;
};
struct Node* top = NULL;

void push(int item);


void pop();
void peep();
void display();

int main()
{
int choice, item;

printf("Operations of Stack:\n");
printf("1. Push\n2. Pop\n3. Display\n4. Peep\n5. Exit\n");

while (1)
{
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch(choice)
{
case 1: printf("Enter the item to push: ");
scanf("%d", &item);
push(item); break;
case 2: pop(); break;
case 3: peep(); break;
case 4: display(); break;
case 5: return 0;
default: printf("Invalid choice.\n");
}
}
}

void push(int item)


{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

if (newNode == NULL) // Check if memory allocation fails


{
printf("\nMemory overflow\n");
return;
}
newNode->data = item;
newNode->next = top; // Point the new node's next to the current top
top = newNode; // Make the new node the new top
printf("Item %d pushed to stack.\n", item);
}

void pop()
{
if (top == NULL) // Check if the stack is empty
{
printf("\nStack underflow\n");
}
else
{
struct Node* temp = top;
printf("Popped item: %d\n", temp->data);
top = top->next; // Move the top pointer to the next node
free(temp); // Free the memory of the popped node
}
}

void peep()
{
if (top == NULL)
{
printf("\nStack is empty, nothing to peep.\n");
}
else
{
printf("Top item: %d\n", top->data);
}
}

void display()
{
if (top == NULL) // Check if the stack is empty
{
printf("\nStack is empty (underflow)\n");
}
else
{
struct Node* temp = top;
printf("\nElements present in the stack:\n");
while (temp != NULL)
{
printf("%d\t", temp->data);
temp = temp->next;
}
printf("\n");
}
}
Program: Implement a program to check for balanced parenthesis using stack.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 30

int top=-1;
int stack[MAX];

void push(char item);


int pop();
int match(char a, char b);
int check(char exp[]);

int main()
{
char exp[MAX];
int valid;
printf("Enter an algebraic expression : ");
gets(exp);
valid=check(exp);
if(valid == 1)
{
printf("\nValid expression\n");
}
else
{
printf("\nInvalid expression\n");
}
return 0;
}

void push(char item)


{

if(top==MAX-1)
{
printf("overflow");
return;
}
stack[++top]=item;
}

int pop()
{
if(top==-1)
{
printf("underflow");
exit(1);
}
return (stack[top--]);
}

int match(char a,char b)


{
if((a=='{' && b=='}') || (a=='[' && b==']') || (a=='(' && b==')'))
{
return 1;
}
else
{
return 0;
}
}

int check(char exp[])


{
int i;
char temp;
for(i=0;exp[i]!='\0';i++)
{
if(exp[i]=='{' || exp[i]=='[' || exp[i]=='(')
{
push(exp[i]);
}
if(exp[i]=='}' || exp[i]==']' || exp[i]==')')
{
if(top==-1)
{
printf("*right parenthesis are more than the left parenthesis*");
return 0;
}
else
{
temp=pop();
if(!match(temp,exp[i]))
{
printf("mismatch parenthesis");
printf("%c and %c",temp,exp[i]);
return 0;
}
}
}
}
if(top==-1)
{
return 1;
}
else
{
printf("left parenthesis are more than the right parenthesis");
return 0;
}
}

Program : Postfix evaluation using stack

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define MAX 100

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

void push(int value);


int pop();
int evaluatePostfix(char* expression) ;

int main()
{
char expression[MAX];
printf("Enter a postfix expression: ");
gets(expression);
int result = evaluatePostfix(expression);
printf("The result of the postfix expression is: %d\n", result);
return 0;
}
// Function to push an item onto the stack
void push(int value)
{
stack[++top] = value;
}

// Function to pop an item from the stack


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

// Function to evaluate a postfix expression


int evaluatePostfix(char* expression)
{
for (int i = 0; expression[i] != '\0'; i++)
{
// If the current character is a digit
if (isdigit(expression[i]))
{
int num = 0;// Extract the full number (to handle multi-digit numbers)
while (isdigit(expression[i]))
{
num = num * 10 + (expression[i] - '0');
i++;
}
i--; // Adjust for the increment after the loop
push(num);
}
// If the current character is an operator
else if (expression[i] == ' ' || expression[i] == '\t')
{
continue; // Skip spaces
}
else
{
int operand2 = pop();
int operand1 = pop();
int result;

// Perform the operation based on the operator


if (expression[i] == '+')
{
result = operand1 + operand2;
}
else if (expression[i] == '-')
{
result = operand1 - operand2;
}
else if (expression[i] == '*')
{
result = operand1 * operand2;
}
else if (expression[i] == '/')
{
result = operand1 / operand2;
}
push(result); // Push the result back onto the stack
}
}
return pop(); // The final result is the only element left in the stack
}
Program: Write a program to convert infix to postfix expression using stack
#include<stdio.h>
#include<ctype.h>
#define MAX 100

char stack[MAX];
int top = -1;

void push(char x);


char pop();
int priority(char x);

int main()
{
char exp[MAX], x;
int i;
printf("Enter an infix expression:");
scanf("%s",exp);

printf("\nPostfic expression:");
for( i=0; exp[i]!='\0'; i++)
{
x = exp[i];
if(isalnum(x))
{
printf("%c",x);
}
else if(x == '(')
{
push(x);
}
else if(x == ')')
{
while( (x = pop()) != '(')
{
printf("%c",x);
}
}
else
{
while(top != -1 && priority(stack[top]) >= priority(x))
{
printf("%c", pop());
}
push(x);
}
}
while(top != -1)
{
printf("%c",pop());
}
return 0;
}
void push(char x)
{
stack[++top] = x;
}

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

int priority(char x)
{
if(x == '(')
return 0;
else if(x == '+' || x =='-')
return 1;
else if(x == '*' || x == '/' || x == '%')
return 2;
else if(x == '^')
return 3;
else
return -1;
}

Program: Implement a program to check palindrome or not

#include<stdio.h>
#include<string.h>

#define max 100

int top = -1;


char stack[max];

void push(char x);


int pop();
int ispalindrome(char exp[]);

int main()
{
char exp[max];
printf("Enter a string:");
scanf("%s",exp);

if(ispalindrome(exp))
{
printf("entered string is palindrome");
}
else
{
printf("netered string is not a palindrome:");
}
}

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

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

int ispalindrome(char exp[])


{
char x;
int i;
for(i=0; exp[i]!='\0'; i++)
{
x = exp[i];
push(x);
}

for(i=0; exp[i]!='\0'; i++)


{
if(exp[i] != pop())
{
return 0;
}

}
return 1;
}

You might also like