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

Stack Programs

The document provides C code implementations for stack operations using both arrays and linked lists, including functions for push, pop, peek, and display. It also includes algorithms for converting infix expressions to postfix and evaluating postfix expressions. The code demonstrates basic stack functionality and expression handling in C programming.

Uploaded by

rowthu padma
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 views14 pages

Stack Programs

The document provides C code implementations for stack operations using both arrays and linked lists, including functions for push, pop, peek, and display. It also includes algorithms for converting infix expressions to postfix and evaluating postfix expressions. The code demonstrates basic stack functionality and expression handling in C programming.

Uploaded by

rowthu padma
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

Stack using array

#include<stdio.h>
#include<stdlib.h>
void push();
void pop();
void peek();
void display();
# define size 5
int arr[size];
int top = -1;
int main()
{
int n;
printf("Stack Menu:-\n\n1.Push\n2.Pop\n3.Peek\
n4.Display\n5.Exit\n\n");
while(1)
{
printf("Enter your choice\t");
scanf("%d",&n);
switch(n)
{
case 1: push(); break;
case 2: pop(); break;
case 3: peek(); break;
case 4: display(); break;
case 5: exit(1);
default: printf("Please enter a valid
choice\n");
};
}
return 0;
}
void push()
{
if(top == size-1)
printf("Overflow Occured\n");
else
{
int n;
printf("Enter value\t");
scanf("%d",&n);
top++;
arr[top] = n;
}
}
void pop()
{
if(top == -1)
printf("Underflow Occured\n");
else
{
printf("Element %d is deleted\n",arr[top]);
top--;
}
}
void peek()
{
if(top == -1)
printf("Stack is Empty\n");
else
printf("Top element is %d\n",arr[top]);
}
void display()
{
if(top == -1)
printf("Empty stack - No elements to display\
n");
else
{
int i;
for(i = top;i>=0;i--)
printf("%d ",arr[i]);
printf("\n");
}
}
Stack using Linked List
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
}*top = NULL;
void push();
void pop();
void peek();
void display();
int main()
{
int n;
printf("Stack Menu:-\n\n1.Push\n2.Pop\n3.Peek\
n4.Display\n5.Exit\n\n");
while(1)
{
printf("Enter your choice\t");
scanf("%d",&n);
switch(n)
{
case 1: push(); break;
case 2: pop(); break;
case 3: peek(); break;
case 4: display(); break;
case 5: exit(1);
default: printf("Please enter a valid choice\n");
};
}
return 0;
}
void push()
{
int n;
struct node *temp,*p;
temp = (struct node *)malloc(sizeof(struct node));
printf("Enter value\t");
scanf("%d",&n);
temp->data = n;
temp->link = NULL;
if(top == NULL)
top = temp;
else
{
temp->link = top;
top = temp;
}
}
void pop()
{
if(top == NULL)
printf("Underflow - No elements to delete\n");
else
{
printf("Top element %d is deleted\n",top->data);
top = top->link;
}
}
void peek()
{
if(top == NULL)
printf("Empty Stack\n");
else
printf("Top element = %d\n",top->data);
}
void display()
{
if(top == NULL)
printf("No elements to display\n");
else
{
struct node *p=top;
for (p = top ; p!= NULL ; p = p->link)
printf("%d ",p->data);
printf("\n");
}
}
INFIX TO POSTFIX CONVERSION
#include<stdio.h>

#include<ctype.h>

char stack[20];

int top = -1;

void push(char);

char pop();

int precedence(char);

int main()

char infix[20],postfix[20];

int i,j=0;

printf("Enter an Infix expression:-\t");

gets(infix);

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

if(isalnum(infix[i]))

postfix[j] = infix[i];

j++;

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

push(infix[i]);

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

while(stack[top] != '(')
{

postfix[j] = pop();

j++;

pop();

else

while((top != -1 && stack[top] != '(') && (precedence(stack[top]) >=


precedence(infix[i])))

postfix[j] = pop();

j++;

push(infix[i]);

while(top != -1)

postfix[j] = pop();

j++;

postfix[j] = '\0';

printf("PostFix Expression = %s\n",postfix);

return 0;

void push(char c)

{
top++;

stack[top] = c;

char pop()

char c;

c = stack[top];

top--;

return c;

int precedence(char c)

switch(c)

case '+':

case '-': return 1;

case '*':

case '/': return 2;

Evalution of Postfix Expression


#include<stdio.h>

#include<stdlib.h>

#include<ctype.h>

int a[20];
int top = -1;

void push(int);

int pop();

int main()

char stack[20];

int i,a,b;

printf("Enter Postfix Expression\t");

gets(stack);

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

if(isdigit(stack[i]))

push(stack[i]-'0');

else

a = pop();

b = pop();

switch(stack[i])

{
case '+': push(b+a); break;

case '-': push(b-a); break;

case '*': push(b*a); break;

case '/': push(b/a); break;

case '%': push(b%a); break;

default : printf("Invalid expression\n");

exit(1);

int res = pop();

if(top == -1)

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

else

printf("Invalid expression\n");

return 0;

void push(int n)

top++;

a[top] = n;
}

int pop()

int n;

n = a[top];

top--;

return n;

You might also like