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

Stack

programs on stacks

Uploaded by

sbquantum2005
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Stack

programs on stacks

Uploaded by

sbquantum2005
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Program 1: Design and develop a program to perform array operations.

a. Creating an array
b. Display of array Elements
c. Inserting an Element at a given Position
d. Deleting an Element at a given Position

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

int i,a[50],n=0,POS,ELEM;
void create();
void display();
int insert();
int del();

int main()
{
int choice;
//clrscr();
do
{
printf("\n\n---MENU---");
printf("\n 1.Create");
printf("\n 2.Display");
printf("\n 3.Insert");
printf("\n 4.delete");
printf("\n 5.Exit");
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1: create();
break;
case 2: display();
break;
case 3: if (n==0)
{
printf ("\tArray is empty ");
}
else
{
insert();
}
break;
case 4: if (n==0)
{
printf ("\tArray is empty ");
}
else
{
del();
}
break;
case 5: exit(0);
break;
default : printf("\t Invalid Chioce");
}
}while(choice!=5);
return 0;
}

void create()
{
printf("Enter the number of elements:\t");
scanf("%d",&n);
printf("\nEnter the array elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}

void display()
{
if(n==0)
{
printf("\tArray is empty ");
}
else
{
printf("Array elements are \n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}
}

int insert()
{
printf("Enter the location where you wish to insert an element:\t");
scanf("%d",&POS);

if(POS==0 || POS>n+1)
{
printf("Insertion is not possible 'POS>N'");
}
else
{
printf("\nEnter the value to Insert: \t");
scanf("%d", &ELEM);

for(i=n-1;i>=POS-1;i--)
{
a[i+1]=a[i];
}
a[POS-1]=ELEM;
n++;
}
return 0;
}

int del()
{
printf("Enter the location where you wish to delete element:\t");
scanf("%d",&POS);
if(POS==0 || POS>=n+1)
{
printf("\nDeletion is not possible 'POS>N'");
}
else
{
printf("\nDeleted Item is = %d\n",a[POS-1]);
for(i=POS-1;i<n;i++)
{
a[i]=a[i+1];
}
n--;
}
return 0;
}

Design and develop a program working of stack using array


Push
Pop
Display
Demonstrate overflow and underflow

#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 4
int stk[MAX_SIZE], top=-1;
void push();
void pop();
void display();

void main()
{
int choice;

while(1)
{
printf("\n\n---MENU---");
printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.Display");
printf("\n4.exit");
printf("\nEnter your choice:\t");
scanf("%d",&choice); //Read Choice
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
break;
default :printf("\tInvalid Chioce");
}
}
}

void push()
{
int p;
if(top==MAX_SIZE-1)
{
printf("Stack Overflow");
}
else
{
printf("Enter the Element to Push\n");
scanf("%d",&p);
top++;
stk[top]=p;
}
}

void pop()
{
if(top==-1)
{
printf("Stack Underflow");
}
else
{
printf("Poped element is %d",stk[top]);
top--;
}
}

void display()
{
int i;
if(top== -1)
{
printf("Stack is Empty");
}
else
{
printf("Elements in STACK are \n");
for(i=0;i<=top;i++)
{
printf("%d\t",stk[i]);
}
}
}

Program 4: Design and develop a Program for converting an Infix Expression to Postfix
Expression. The expression should consist operators: +, -, *, /, %(Remainder), ^ (Power) and
alphanumeric operands.

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

int F(char symbol)


{
switch(symbol)
{
case '+':
case '-': return 2;
case '*':
case '/':
case '%': return 4;
case '^':
case '$': return 5;
case '(': return 0;
case '#': return -1;
default: return 8;
}
}

int G(char symbol)


{
switch(symbol)
{
case '+':
case '-': return 1;
case '*':
case '/':
case '%': return 3;
case '^':
case '$': return 6;
case '(': return 9;
case ')': return 0;
default: return 7;
}
}
void infix_postfix(char infix[],char postfix[])
{
int top, i, j;

char symbol, s[30];


top--;
s[++top]= '#';
j=0;
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
while(F(s[top])>G(symbol))
{
postfix[j++]=s[top--];
}
if(F(s[top])!=G(symbol))
s[++top]=symbol;
else
top--;

}
while(s[top]!= '#')
{
postfix[j++]=s[top--];
}
postfix[j]='\0';
}

void main()
{
char infix[20],postfix[20];

printf("Enter the Infix Expression :\n");


scanf("%s",infix);
infix_postfix(infix,postfix);
printf("The Post Expression is:\n");
printf("%s\n",postfix);

You might also like