The document describes a program to simulate the functioning of back and forward buttons in a web browser using two stacks. It implements two stacks using an array to store the links of web pages visited. The back stack stores the history of pages as the user navigates backward, while the forward stack stores the path for navigation in the forward direction. Functions are defined to push and pop from the stacks to simulate button clicks and change the current page. Printing options display the contents of both stacks. The program provides a menu to test different navigation options and stack operations.
Call Girls Btm Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Towers of Hanoi Recursive Program
1. 1
VELLORE INSTITUTE OF TECHNOLOGY
WINTER SEMESTER 2021-2022
⦁ NAME: Srinath.A
⦁ REGISTER NO:21MIS0153
⦁ FACULTY:Nancy victor
⦁ SUBJECT: SWE2001
1:
Students of a Programming class arrive to submit assignments.
Their register numbers are stored in a LIFO list in the order in
which the assignments are submitted. Write a program using
array to display the register number of the ten students who
submitted first.
Register number of the ten students who submitted first will be
at the bottom of the LIFO list. Hence pop out the required
number of elements from the top so as to retrieve and display
the first 10 students.
Low Level: Display the register number of the last submitted
record
Middle Level: Display the register number of the ten students
who submitted first
CODE:--
#include <stdio.h>
#define max 100
int top = -1; char
2. 2
a[max][100]; int
isempty(); int
isfull();
void push(char x[100]);
void pop();
void display();
int main()
{ char x[100];
int ch = 1, n;
while (ch != 4)
{
printf("1.PUSHn");
printf("2.POPn");
printf("3.DISPLAYn");
printf("4.EXITn");
printf("Enter your choicen");
scanf("%d", &ch); switch
(ch)
{
case 1:
printf("Enter the registration no. of student:-n");
scanf("%s", x); push(x); break; case
2: pop(); break; case 3:
display(); break;
}
}
printf("last student of record submission:%s", a[top]);
printf("nEnter the first n number of required recordn");
scanf("%d", &n); while (top >= n)
{
pop();
}
printf("The first %d students aren", n);
display();
return 0;
}
int isempty()
{ if (top == -1)
return 1; else
3. 3
return 0; } int
isfull() { if (top
== max - 1)
return 1; else
return 0;
}
void push(char x[100])
{ int i; if (isfull())
printf("stack is fulln");
else
{
top++; for (i =
0; i < 100; i++)
{
a[top][i] = x[i];
}
}
}
void pop()
{ if
(isempty())
{
printf("stack is emptyn");
}
else
{
printf("deleted element is %sn", a[top]);
top--;
}
}
void display()
{ int i; if (isempty())
printf("stack is emptyn");
else
{
for (i = 0; i <= top; i++)
printf("%sn", a[i]);
}
4. 4
}
_____________________________________________________________
_ _
2:
Most of the bugs in scientific and engineering applications are
due to improper usage of precedence order in arithmetic
expressions. Thus it is necessary to use an appropriate notation
that would evaluate the expression without taking into account
the precedence order and parenthesis.
a) Write a program to convert the given arithmetic expression
into
i) Reverse Polish
notation (postfix) ii)
Polish notation (prefix)
b) Evaluate the above notations with necessary input.
5. 5
High Level: Develop a menu driven program to implement all
conversions and evaluations in a single program with different
inputs
CODE:--
#include <string.h>
#include <stdio.h>
#include
<stdlib.h>
#include
<ctype.h> #define
size 50 int top = -
1; char s[size]; int
isemty()
{ if (top ==
-1) return
1; else
return 0;
}
int isfull()
{ if (top >= size -
1) return 1;
else return 0;
}
void push(char x)
{ s[++top] =
x;
}
char pop()
{ return (s[top--]); }
int priority(char x) {
if (x == '(') return
0; if (x == '+' || x ==
'-') return 1; if
(x == '*' || x == '/')
6. 6
return 2; if (x == '^')
return 3;
}
void main()
{
int choice;
while (1)
{
printf("n1.Infix to Postfix....");
printf("n2.Infix to Prefix....");
printf("n3.prefix evaluation....");
printf("n4.Postfix evaluation....");
printf("n5.exit....");
printf("nEnter your choice::");
scanf("%d", &choice); switch
(choice)
{
case 1:
{
char in[50], pr[50], ch, x, *e; int i
= 0, k = 0; printf("nnEnter the Infix
Expression :: "); scanf("%s", in); e
= in; while (*e != '0')
{
if (isalnum(*e))
printf("%c", *e);
else if (*e == '(')
push(*e); else if
(*e == ')')
{
while ((x = pop()) != '(')
printf("%c", x);
}
else
{
7. 7
while (priority(s[top]) >=
priority(*e)) printf("%c", pop());
push(*e);
}
e++;
}
while (top != -1)
{
printf("%c", pop());
}
break;
}
case 2:
{
char in[50], pr[50], ch, x, *e;
int i = 0, k = 0;
printf("nnEnter the Infix
Expression::"); scanf("%s", in);
strrev(in);
while ((ch = in[i++]) != '0')
{ if (ch ==
')') push(ch);
else if (isalnum(ch))
pr[k++] = ch; else
if (ch == '(')
{ while
(s[top] != ')')
pr[k++] = pop();
x = pop(); }
else
{ if
(s[top] == -1)
push(ch);
while (priority(s[top]) >=
priority(ch)) pr[k++] = pop();
push(ch);
}
}
while (top != -1)
8. 8
{
pr[k++] = pop();
}
strrev(pr); strrev(in);
printf("nnPrefix Expression:: %sn", pr);
break;
}
case 3:
{
float stack[50];
int i = 0, top = -1;
int val; char
a[30], ch1;
printf("enter the prefix
expresion : ");
scanf("%s", a);
strrev(a);
printf("%sn", a);
while ((ch1 = a[i++]) !=
'0')
{
printf("%c", ch1);
if (isalpha(ch1))
{
printf(" enter the value : ");
scanf("%d", &val);
stack[++top] = val;
}
else if (ch1 == '+' || ch1 == '-' || ch1 == '*' ||
ch1 == '/')
{
if (ch1 == '+')
{
stack[top - 1] = stack[top]
+ stack[top - 1];
top--; } if (ch1
== '-')
{
9. 9
stack[top - 1] = stack[top]
stack[top - 1]; top--;
} if (ch1 == '*')
{
stack[top - 1] = stack[top]
* stack[top - 1];
top--; } if (ch1
== '/')
{
stack[top - 1] = stack[top] /
stack[top -
1]; top--;
}
}
}
printf("n%f", stack[top]);
break;
}
case 4:
{
float stack[50]; int i = 0, top = -
1; int val; char a[30], ch;
printf("enter the postfi x expression : ");
scanf("%s", a); while ((ch = a[i++]) !=
'0')
{
if (isalpha(ch))
{
printf("%c", ch);
printf(" enter the value : ");
scanf("%d", &val);
stack[++top] = val;
}
else if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
{
if (ch == '+')
{ printf("in =");
stack[top - 1] = stack[top - 1] +
12. 12
_____________________________________________________________
3:
Design a program to employ a stack for balancing symbols such as
parentheses, flower braces and square brackets, in the code
snippet given below.
for(i=0;i<n;i++)
{
if(i<
5)
{ z[i]=x[i]+y[i];
p=(((a+b)*c)+(d/(e+f
)*g); }
Ensure that your program works for any arbitrary expression
Implement balancing of all symbols only for some specified
expressions only
Challenging: Implement balancing of all symbols for a program
CODE:--
#include <stdio.h>
int main( )
{ char Arr[500],
exp[500]; int top = -1, i;
printf("nntt**************CODE TO CHECK BALANCED
EXPRESSION**************"); printf("ntt Enter an infix expression(press @ to end
input):n "); scanf("%[^@]s", exp); for (i = 0; exp[i] != '0'; i++)
{
14. 14
printf("ttt********* Expression is Not Balanced *********");
printf("nttt No. of Mismatch::%d", top + 1);
}
}
CODE OUTPUT:-
_____________________________________________________________
_
4:
Some priests are given three poles and a stack of n gold disks,
each disk a little smaller than the one beneath it. Their
assignment is to transfer all n disks from one of the 3 pole to
another with 2 important constraints. They can move only one
disk at a time, and they can never place a larger disk on top of a
smaller one. Design a recursive program for the above Towers of
Hanoi puzzle using stack.
15. 15
1: Implement the problem using recursion
2: Implement the problem using recursion and also trace the flow
of execution
CODE:--
#include <stdio.h> #define max 4 int n, A[max], B[max], C[max], i,
top_1 = -1, top_2 = -1, top_3 = -1, k = 1; void transfer(char x, char z);
void display(); void TOH(int n, char x, char y, char z); void main()
{ char p, q, x = 'A', y = 'B', z =
'C'; for (i = max - 1; i >= 0; i--
)
{
A[i] = 4 - i;
}
top_1 = max - 1; for (i
= max - 1; i >= 0; i--)
{
B[i] = 0;
C[i] = 0;
}
n = 4;
printf("n ****************** TOWER OF HANOI
PROBLEM*********************");
printf("ntt********USING
RECURSION********"); display();
TOH(n,'A','B','C');
printf("n *************** AFTER COMPLETE
TRANSFORMATION***************"); display();
} void TOH(int n, char x, char y,
char z)
{
if (n > 0)
{
TOH(n - 1, x, z,
y); transfer(x, z);
k = k + 1;
19. 19
To facilitate a thorough net surfing, any web browser has back
and forward buttons that allow the user to move backward and
forward through a series of web pages. To allow the user to move
both forward and backward two stacks are employed. When the
user presses the back button, the link to the current web page is
stored on a separate stack for the forward button. As the user
moves backward through a series of previous pages, the link to
each page is moved in turn from the back to the forward stack.
When the user presses the forward button, the action is the
reverse of the back button. Now the item from the forward
stack is popped, and becomes the current web page. The previous
web page is pushed on the back stack. Simulate the functioning of
these buttons using array implementation of
Stack. Also provide options for displaying the contents of both
the stacks whenever required. Implement web browser navigation
using both the stacks
CODE:--
#include<stdio.h>
#include<string.h> char
array[10][50]; int top1 = -1; int
top2 = 10; void pushforward
(char data[50])
{ if (top1 < top2 - 1) {
top1++;
strcpy(array[top1],data); }
else
{
printf ("Stack Overloadn");
}
}
void pushback (char data[50])
21. 21
}
void print_forw ()
{ int i; for (i = top1; i >=
0; --i)
{
printf ("%sn", array[i]);
}
printf ("n");
}
void print_back ()
{ int i; for (i = top2; i <
10; ++i)
{ printf ("%sn",
array[i]);
}
printf ("n");
}
void main()
{ int in,cont=0; char n[50]; printf("Browser History Program:nEnter 1 to Open
link.nEnter 2 to Press Back."); printf("nEnter 3 to Press Forward.nEnter 4 to Print
Both Stacks.nEnter 5 to Exitn"); while(cont==0)
{
printf("Enter Choice - ");
scanf("%d",&in);
switch(in)
{
case 1:printf("Enter Data - ");scanf("%s",&n);pushback(n);break; case
2:pop_back();break; case 3:pop_forw();break; case
4:printf("Forward:n");print_forw();printf("Backward:n");print_back();break; case
5:cont++;break;
}
}
}