[go: up one dir, main page]

0% found this document useful (0 votes)
23 views14 pages

Exercise 5

The document contains multiple C programs demonstrating stack operations using both arrays and linked lists, including push, pop, and display functionalities. It also includes programs for reversing a list using a stack, evaluating postfix expressions, and checking for balanced parentheses. Each program is structured with user input and error handling for stack overflow and underflow conditions.

Uploaded by

anusha
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)
23 views14 pages

Exercise 5

The document contains multiple C programs demonstrating stack operations using both arrays and linked lists, including push, pop, and display functionalities. It also includes programs for reversing a list using a stack, evaluating postfix expressions, and checking for balanced parentheses. Each program is structured with user input and error handling for stack overflow and underflow conditions.

Uploaded by

anusha
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/ 14

1. Write a C program to implement stack using array?

#include <stdio.h>

int stack[100],i,j,choice=0,n,top=-1;

void push();

void pop();

void show();

void main ()

printf("Enter the number of elements in the stack ");

scanf("%d",&n);

printf("*********Stack operations using array*********");

printf("\n----------------------------------------------\n");

while(choice != 4)

printf("Chose one from the below options...\n");

printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");

printf("\n Enter your choice \n");

scanf("%d",&choice);

switch(choice)

case 1:

push();

break;

case 2:

{
pop();

break;

case 3:

show();

break;

case 4:

printf("Exiting....");

break;

default:

printf("Please Enter valid choice ");

void push ()

int val;

if (top == n )

printf("\n Overflow");

else

{
printf("Enter the value?");

scanf("%d",&val);

top = top +1;

stack[top] = val;

void pop ()

if(top == -1)

printf("Underflow");

else

top = top -1;

void show()

for (i=top;i>=0;i--)

printf("%d\n",stack[i]);

if(top == -1)

printf("Stack is empty");

}
2. C Program to Implement Stack using Linked List

#include <stdio.h>

#include <stdlib.h>

void push();

void pop();

void display();

struct node

int val;

struct node *next;

};

struct node *head;

void main ()

int choice=0;

printf("\n*********Stack operations using linked list*********\n");

printf("\n----------------------------------------------\n");

while(choice != 4)

printf("\n\nChose one from the below options...\n");

printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");

printf("\n Enter your choice \n");

scanf("%d",&choice);

switch(choice)

case 1:
{

push();

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

printf("Exiting....");

break;

default:

printf("Please Enter valid choice ");

};

void push ()
{

int val;

struct node *ptr = (struct node*)malloc(sizeof(struct node));

if(ptr == NULL)

printf("not able to push the element");

else

printf("Enter the value");

scanf("%d",&val);

if(head==NULL)

ptr->val = val;

ptr -> next = NULL;

head=ptr;

else

ptr->val = val;

ptr->next = head;

head=ptr;

printf("Item pushed");

}
}

void pop()

int item;

struct node *ptr;

if (head == NULL)

printf("Underflow");

else

item = head->val;

ptr = head;

head = head->next;

free(ptr);

printf("Item popped");

void display()

int i;

struct node *ptr;

ptr=head;

if(ptr == NULL)

printf("Stack is empty\n");
}

else

printf("Printing Stack elements \n");

while(ptr!=NULL)

printf("%d\n",ptr->val);

ptr = ptr->next;

3. C Program to reversing the list application by using stack?

#include <stdio.h>

#define size 10

int stack[size],top=-1;

void push(int);

int pop();

int isempty();

void main()

int a[size],i;

printf("enter the array elements\n");

for(i=0;i<size;i++)

scanf("%d",&a[i]);

for(i=0;i<size;i++)

push(a[i]);

printf("list in reverse order\n");


for(i=0;!isempty();i++)

int ele;

ele=pop();

printf("%d\t",ele);

void push(int ele)

if(top==size-1)

printf("stack is overflow");

else

top=top+1;

stack[top]=ele;

int pop()

int ele;

if(top==-1)

printf("stack is underflow");

else

ele=stack[top];

top=top-1;

}
return ele;

int isempty()

if(top==-1)

return 1;

else

return 0;

4. Write a program to evaluate the postfix expression using a stack?

#include <stdio.h>

#include <stdlib.h>

#define MAX_SIZE 100

// Stack implementation

int stack[MAX_SIZE];

int top = -1;

void push(int item)

if (top >= MAX_SIZE - 1)

printf("Stack Overflow\n");

return;

top++;

stack[top] = item;

int pop()
{

if (top < 0)

printf("Stack Underflow\n");

return -1;

int item = stack[top];

top--;

return item;

int is_operator(char symbol)

if (symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/')

return 1;

return 0;

int evaluate(char* expression)

int i = 0;

char symbol = expression[i];

int operand1, operand2, result;

while (symbol != '\0')

if (symbol >= '0' && symbol <= '9')


{

int num = symbol - '0';

push(num);

else if (is_operator(symbol))

operand2 = pop();

operand1 = pop();

switch(symbol)

case '+': result = operand1 + operand2; break;

case '-': result = operand1 - operand2; break;

case '*': result = operand1 * operand2; break;

case '/': result = operand1 / operand2; break;

push(result);

i++;

symbol = expression[i];

result = pop();

return result;

int main()

char expression[] = "5 6 7 + * 8 -";

int result = evaluate(expression);


printf("Result= %d\n", result);

return 0;

4. Implement a program to check for balanced parenthesis using a stack?

#include<stdio.h>

int main()

char expression[50];

int x=0, i=0;

printf("\nEnter an expression");

scanf("%s", expression);

while(expression[i]!= '\0')

if(expression[i]=='(')

x++;

else if(expression[i]==')')

x--;

if(x<0)

break;

i++;

}
if(x==0)

printf("Expression is balanced");

else

printf("Expression is unbalanced");

return 0;

Output

Enter an expression(A+B+(C*D)

Expression is unbalanced

You might also like