SlideShare une entreprise Scribd logo
1  sur  22
Télécharger pour lire hors ligne
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
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
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
}
_____________________________________________________________
_ _
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
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
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
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
{
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
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] +
10
stack[top]; top--;
} if (ch == '-')
{
stack[top - 1] = stack[top - 1]
stack[top]; top--;
} if (ch == '*')
{
stack[top - 1] = stack[top - 1]
* stack[top];
top--; } if (ch == '/')
{
stack[top - 1] = stack[top - 1]
/ stack[top];
top--;
}
}
}
printf("%f", stack[top]);
break;
}
case 5:
exit(0);
}
}
}
11
CODE OUTPUT:-
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++)
{
13
if (exp[i] == '(' || exp[i] == '[' || exp[i] == '{')
{
top++;
Arr[top] = exp[i];
}
else if (exp[i] == ')')
{
if (Arr[top] ==
'(') top--;
else
{
printf("ttt Unbalanced Expression");
break;
}
}
else if (exp[i] == ']')
{ if
(Arr[top] == '[')
top--; else
{
printf("ttt Unbalanced Expression");
break;
}
}
else if (exp[i] == '}')
{
if (Arr[top] ==
'{') top--;
else
{
printf("ttt Unbalanced Expression");
break;
}
}
}
if (top == -1)
{
printf("ttt*********** Expression is Balanced *************");
}
else
{
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
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;
16
TOH(n - 1, y, x, z);
}
}
void transfer(char x, char z)
{ if (x == 'A' && z
== 'B')
{
B[++top_2] = A[top_1];
A[top_1--] = 0;
printf("n step(%d) (A --->> B)",
k); display();
}
else if (x == 'C' && z == 'B')
{
B[++top_2] = C[top_3];
C[top_3--] = 0; printf("n
step(%d) (C --->> B)", k);
display();
}
else if (x == 'B' && z == 'C')
{
C[++top_3] = B[top_2];
B[top_2--] = 0; printf("n
step(%d) (B --->> C)", k);
display();
}
else if (x == 'A' && z == 'C')
{
C[++top_3] = A[top_1];
A[top_1--] = 0; printf("n
step(%d) (A --->> C)", k);
display();
}
else if (x == 'C' && z == 'A')
{
A[++top_1] = C[top_3];
C[top_3--] = 0; printf("n
step(%d) (C --->> A)", k);
display();
17
}
else if (x == 'B' && z == 'A')
{
A[++top_1] = B[top_2];
B[top_2--] = 0; printf("n
step(%d) (B --->> A)", k);
display();
}
} void
display()
{
printf("nt A__|__B__|__C");
for (i = max - 1; i >= 0; i--)
{
printf("nt %d | %d | %d", A[i], B[i], C[i]);
}
}
18
_____________________________________________________________
_ __
5:
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])
20
{ if (top1 < top2 -
1)
{
top2--;
strcpy(array[top2],data);
}
else
{
printf ("Stack Overloadn");
}
}
void pop_forw ()
{ if (top1 >=
0)
{
char popval[50]; top1--;
strcpy(popval,array[top1--]);
pushback(popval);
printf ("Current page - %sn", array[top2]);
}
else
{
printf ("Stack Emptyn");
}
}
void pop_back ()
{ if (top2 <
10)
{
char popval[50];
strcpy(popval,array[top2++]);
pushforward(popval); printf ("Current page
- %sn", array[top2]);
}
else
{
printf ("Stack Empty! Cannot Popn");
}
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;
}
}
}
22
CODE OUTPUT:-
*************************************************************

Contenu connexe

Tendances

Tendances (20)

Linked list
Linked listLinked list
Linked list
 
Pointer arithmetic in c
Pointer arithmetic in c Pointer arithmetic in c
Pointer arithmetic in c
 
Types of Statements in Python Programming Language
Types of Statements in Python Programming LanguageTypes of Statements in Python Programming Language
Types of Statements in Python Programming Language
 
Overview of TCP IP
Overview of TCP IPOverview of TCP IP
Overview of TCP IP
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
Data Structures Chapter-2
Data Structures Chapter-2Data Structures Chapter-2
Data Structures Chapter-2
 
Data Link Control
Data Link ControlData Link Control
Data Link Control
 
Application of Stacks
Application of StacksApplication of Stacks
Application of Stacks
 
Multi-Dimensional Lists
Multi-Dimensional ListsMulti-Dimensional Lists
Multi-Dimensional Lists
 
Three way handshake
Three way handshakeThree way handshake
Three way handshake
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
Remote procedure call
Remote procedure callRemote procedure call
Remote procedure call
 
Chap10
Chap10Chap10
Chap10
 
Mca 2nd sem u-4 operator overloading
Mca 2nd  sem u-4 operator overloadingMca 2nd  sem u-4 operator overloading
Mca 2nd sem u-4 operator overloading
 
AS2 vs. SFTP
AS2 vs. SFTPAS2 vs. SFTP
AS2 vs. SFTP
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
 
IPv4 Addressing
 IPv4 Addressing   IPv4 Addressing
IPv4 Addressing
 
SSL & TLS Architecture short
SSL & TLS Architecture shortSSL & TLS Architecture short
SSL & TLS Architecture short
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
 
C material
C materialC material
C material
 

Similaire à Towers of Hanoi Recursive Program

Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020vrgokila
 
Write a program to convert a given INFIX into POSTFIX. Make sure .pdf
Write a program to convert a given INFIX into POSTFIX. Make sure .pdfWrite a program to convert a given INFIX into POSTFIX. Make sure .pdf
Write a program to convert a given INFIX into POSTFIX. Make sure .pdfFOREVERPRODUCTCHD
 
#includeiostream#includecctypeusing namespace std;cl.docx
#includeiostream#includecctypeusing namespace std;cl.docx#includeiostream#includecctypeusing namespace std;cl.docx
#includeiostream#includecctypeusing namespace std;cl.docxkatherncarlyle
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 SolutionHazrat Bilal
 
Datastructures asignment
Datastructures asignmentDatastructures asignment
Datastructures asignmentsreekanth3dce
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxhappycocoman
 
Cd practical file (1) start se
Cd practical file (1) start seCd practical file (1) start se
Cd practical file (1) start sedalipkumar64
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresLakshmi Sarvani Videla
 
Data Structure in C Programming Language
Data Structure in C Programming LanguageData Structure in C Programming Language
Data Structure in C Programming LanguageArkadeep Dey
 
Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manualSANTOSH RATH
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkarsandeep kumbhkar
 

Similaire à Towers of Hanoi Recursive Program (20)

VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
Struct examples
Struct examplesStruct examples
Struct examples
 
stack.pptx
stack.pptxstack.pptx
stack.pptx
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
Write a program to convert a given INFIX into POSTFIX. Make sure .pdf
Write a program to convert a given INFIX into POSTFIX. Make sure .pdfWrite a program to convert a given INFIX into POSTFIX. Make sure .pdf
Write a program to convert a given INFIX into POSTFIX. Make sure .pdf
 
#includeiostream#includecctypeusing namespace std;cl.docx
#includeiostream#includecctypeusing namespace std;cl.docx#includeiostream#includecctypeusing namespace std;cl.docx
#includeiostream#includecctypeusing namespace std;cl.docx
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
 
week-18x
week-18xweek-18x
week-18x
 
week-17x
week-17xweek-17x
week-17x
 
Datastructures asignment
Datastructures asignmentDatastructures asignment
Datastructures asignment
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
Cd practical file (1) start se
Cd practical file (1) start seCd practical file (1) start se
Cd practical file (1) start se
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structures
 
Pnno
PnnoPnno
Pnno
 
Data Structure in C Programming Language
Data Structure in C Programming LanguageData Structure in C Programming Language
Data Structure in C Programming Language
 
Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manual
 
C- Programming Assignment 3
C- Programming Assignment 3C- Programming Assignment 3
C- Programming Assignment 3
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
Cpds lab
Cpds labCpds lab
Cpds lab
 

Dernier

Production Day 1.pptxjvjbvbcbcb bj bvcbj
Production Day 1.pptxjvjbvbcbcb bj bvcbjProduction Day 1.pptxjvjbvbcbcb bj bvcbj
Production Day 1.pptxjvjbvbcbcb bj bvcbjLewisJB
 
Delhi Call Girls South Delhi 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls South Delhi 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls South Delhi 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls South Delhi 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
CFO_SB_Career History_Multi Sector Experience
CFO_SB_Career History_Multi Sector ExperienceCFO_SB_Career History_Multi Sector Experience
CFO_SB_Career History_Multi Sector ExperienceSanjay Bokadia
 
WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)Delhi Call girls
 
Call Girls Devanahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Devanahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Devanahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Devanahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service B...amitlee9823
 
Nandini Layout Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Nandini Layout Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...Nandini Layout Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Nandini Layout Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...amitlee9823
 
Call Girls Hoodi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hoodi Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hoodi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hoodi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangaloreamitlee9823
 
Book Paid Saswad Call Girls Pune 8250192130Low Budget Full Independent High P...
Book Paid Saswad Call Girls Pune 8250192130Low Budget Full Independent High P...Book Paid Saswad Call Girls Pune 8250192130Low Budget Full Independent High P...
Book Paid Saswad Call Girls Pune 8250192130Low Budget Full Independent High P...ranjana rawat
 
Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...
Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...
Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...poojakaurpk09
 
Personal Brand Exploration - Fernando Negron
Personal Brand Exploration - Fernando NegronPersonal Brand Exploration - Fernando Negron
Personal Brand Exploration - Fernando Negronnegronf24
 
Vip Modals Call Girls (Delhi) Rohini 9711199171✔️ Full night Service for one...
Vip  Modals Call Girls (Delhi) Rohini 9711199171✔️ Full night Service for one...Vip  Modals Call Girls (Delhi) Rohini 9711199171✔️ Full night Service for one...
Vip Modals Call Girls (Delhi) Rohini 9711199171✔️ Full night Service for one...shivangimorya083
 
Bur Dubai Call Girl Service #$# O56521286O Call Girls In Bur Dubai
Bur Dubai Call Girl Service #$# O56521286O Call Girls In Bur DubaiBur Dubai Call Girl Service #$# O56521286O Call Girls In Bur Dubai
Bur Dubai Call Girl Service #$# O56521286O Call Girls In Bur Dubaiparisharma5056
 
Call Girls Alandi Road Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Alandi Road Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Alandi Road Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Alandi Road Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
Presentation on Workplace Politics.ppt..
Presentation on Workplace Politics.ppt..Presentation on Workplace Politics.ppt..
Presentation on Workplace Politics.ppt..Masuk Ahmed
 
Call Girls Hosur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hosur Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hosur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hosur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangaloreamitlee9823
 
Top Rated Pune Call Girls Warje ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated  Pune Call Girls Warje ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...Top Rated  Pune Call Girls Warje ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated Pune Call Girls Warje ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...Call Girls in Nagpur High Profile
 
Dubai Call Girls Demons O525547819 Call Girls IN DUbai Natural Big Boody
Dubai Call Girls Demons O525547819 Call Girls IN DUbai Natural Big BoodyDubai Call Girls Demons O525547819 Call Girls IN DUbai Natural Big Boody
Dubai Call Girls Demons O525547819 Call Girls IN DUbai Natural Big Boodykojalkojal131
 
Dombivli Call Girls, 9892124323, Kharghar Call Girls, chembur Call Girls, Vas...
Dombivli Call Girls, 9892124323, Kharghar Call Girls, chembur Call Girls, Vas...Dombivli Call Girls, 9892124323, Kharghar Call Girls, chembur Call Girls, Vas...
Dombivli Call Girls, 9892124323, Kharghar Call Girls, chembur Call Girls, Vas...Pooja Nehwal
 
Call Girls Btm Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Btm Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Btm Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Btm Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...amitlee9823
 

Dernier (20)

Production Day 1.pptxjvjbvbcbcb bj bvcbj
Production Day 1.pptxjvjbvbcbcb bj bvcbjProduction Day 1.pptxjvjbvbcbcb bj bvcbj
Production Day 1.pptxjvjbvbcbcb bj bvcbj
 
Delhi Call Girls South Delhi 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls South Delhi 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls South Delhi 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls South Delhi 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Sensual Moments: +91 9999965857 Independent Call Girls Paharganj Delhi {{ Mon...
Sensual Moments: +91 9999965857 Independent Call Girls Paharganj Delhi {{ Mon...Sensual Moments: +91 9999965857 Independent Call Girls Paharganj Delhi {{ Mon...
Sensual Moments: +91 9999965857 Independent Call Girls Paharganj Delhi {{ Mon...
 
CFO_SB_Career History_Multi Sector Experience
CFO_SB_Career History_Multi Sector ExperienceCFO_SB_Career History_Multi Sector Experience
CFO_SB_Career History_Multi Sector Experience
 
WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)
 
Call Girls Devanahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Devanahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Devanahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Devanahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
Nandini Layout Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Nandini Layout Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...Nandini Layout Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Nandini Layout Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
 
Call Girls Hoodi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hoodi Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hoodi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hoodi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
Book Paid Saswad Call Girls Pune 8250192130Low Budget Full Independent High P...
Book Paid Saswad Call Girls Pune 8250192130Low Budget Full Independent High P...Book Paid Saswad Call Girls Pune 8250192130Low Budget Full Independent High P...
Book Paid Saswad Call Girls Pune 8250192130Low Budget Full Independent High P...
 
Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...
Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...
Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...
 
Personal Brand Exploration - Fernando Negron
Personal Brand Exploration - Fernando NegronPersonal Brand Exploration - Fernando Negron
Personal Brand Exploration - Fernando Negron
 
Vip Modals Call Girls (Delhi) Rohini 9711199171✔️ Full night Service for one...
Vip  Modals Call Girls (Delhi) Rohini 9711199171✔️ Full night Service for one...Vip  Modals Call Girls (Delhi) Rohini 9711199171✔️ Full night Service for one...
Vip Modals Call Girls (Delhi) Rohini 9711199171✔️ Full night Service for one...
 
Bur Dubai Call Girl Service #$# O56521286O Call Girls In Bur Dubai
Bur Dubai Call Girl Service #$# O56521286O Call Girls In Bur DubaiBur Dubai Call Girl Service #$# O56521286O Call Girls In Bur Dubai
Bur Dubai Call Girl Service #$# O56521286O Call Girls In Bur Dubai
 
Call Girls Alandi Road Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Alandi Road Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Alandi Road Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Alandi Road Call Me 7737669865 Budget Friendly No Advance Booking
 
Presentation on Workplace Politics.ppt..
Presentation on Workplace Politics.ppt..Presentation on Workplace Politics.ppt..
Presentation on Workplace Politics.ppt..
 
Call Girls Hosur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hosur Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hosur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hosur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
Top Rated Pune Call Girls Warje ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated  Pune Call Girls Warje ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...Top Rated  Pune Call Girls Warje ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated Pune Call Girls Warje ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
 
Dubai Call Girls Demons O525547819 Call Girls IN DUbai Natural Big Boody
Dubai Call Girls Demons O525547819 Call Girls IN DUbai Natural Big BoodyDubai Call Girls Demons O525547819 Call Girls IN DUbai Natural Big Boody
Dubai Call Girls Demons O525547819 Call Girls IN DUbai Natural Big Boody
 
Dombivli Call Girls, 9892124323, Kharghar Call Girls, chembur Call Girls, Vas...
Dombivli Call Girls, 9892124323, Kharghar Call Girls, chembur Call Girls, Vas...Dombivli Call Girls, 9892124323, Kharghar Call Girls, chembur Call Girls, Vas...
Dombivli Call Girls, 9892124323, Kharghar Call Girls, chembur Call Girls, Vas...
 
Call Girls Btm Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Btm Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Btm Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
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] +
  • 10. 10 stack[top]; top--; } if (ch == '-') { stack[top - 1] = stack[top - 1] stack[top]; top--; } if (ch == '*') { stack[top - 1] = stack[top - 1] * stack[top]; top--; } if (ch == '/') { stack[top - 1] = stack[top - 1] / stack[top]; top--; } } } printf("%f", stack[top]); break; } case 5: exit(0); } } }
  • 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++) {
  • 13. 13 if (exp[i] == '(' || exp[i] == '[' || exp[i] == '{') { top++; Arr[top] = exp[i]; } else if (exp[i] == ')') { if (Arr[top] == '(') top--; else { printf("ttt Unbalanced Expression"); break; } } else if (exp[i] == ']') { if (Arr[top] == '[') top--; else { printf("ttt Unbalanced Expression"); break; } } else if (exp[i] == '}') { if (Arr[top] == '{') top--; else { printf("ttt Unbalanced Expression"); break; } } } if (top == -1) { printf("ttt*********** Expression is Balanced *************"); } else {
  • 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;
  • 16. 16 TOH(n - 1, y, x, z); } } void transfer(char x, char z) { if (x == 'A' && z == 'B') { B[++top_2] = A[top_1]; A[top_1--] = 0; printf("n step(%d) (A --->> B)", k); display(); } else if (x == 'C' && z == 'B') { B[++top_2] = C[top_3]; C[top_3--] = 0; printf("n step(%d) (C --->> B)", k); display(); } else if (x == 'B' && z == 'C') { C[++top_3] = B[top_2]; B[top_2--] = 0; printf("n step(%d) (B --->> C)", k); display(); } else if (x == 'A' && z == 'C') { C[++top_3] = A[top_1]; A[top_1--] = 0; printf("n step(%d) (A --->> C)", k); display(); } else if (x == 'C' && z == 'A') { A[++top_1] = C[top_3]; C[top_3--] = 0; printf("n step(%d) (C --->> A)", k); display();
  • 17. 17 } else if (x == 'B' && z == 'A') { A[++top_1] = B[top_2]; B[top_2--] = 0; printf("n step(%d) (B --->> A)", k); display(); } } void display() { printf("nt A__|__B__|__C"); for (i = max - 1; i >= 0; i--) { printf("nt %d | %d | %d", A[i], B[i], C[i]); } }
  • 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])
  • 20. 20 { if (top1 < top2 - 1) { top2--; strcpy(array[top2],data); } else { printf ("Stack Overloadn"); } } void pop_forw () { if (top1 >= 0) { char popval[50]; top1--; strcpy(popval,array[top1--]); pushback(popval); printf ("Current page - %sn", array[top2]); } else { printf ("Stack Emptyn"); } } void pop_back () { if (top2 < 10) { char popval[50]; strcpy(popval,array[top2++]); pushforward(popval); printf ("Current page - %sn", array[top2]); } else { printf ("Stack Empty! Cannot Popn"); }
  • 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; } } }