Assignment 5 - Stacks and Queues
-
Upload
theakholic -
Category
Documents
-
view
219 -
download
0
Transcript of Assignment 5 - Stacks and Queues
-
7/29/2019 Assignment 5 - Stacks and Queues
1/22
Assignment 3 Stacks and Queues 18 Aug 2013
Akshay Tiwary XII A
Assignment4
Stacks&Queues
Akshay Tiwary
XII A
03
-
7/29/2019 Assignment 5 - Stacks and Queues
2/22
Assignment 3 Stacks and Queues 18 Aug 2013
Akshay Tiwary XII A
AssignmentI. To reverse a string using stack.
II. 2. To check if a given string is palindrome or not.(*)III. 3. To convert a decimal number 135 to binary using stack.(*)IV. 4. To check if a given number is armstrong number or not.V. 5. To convert an infix to a post fix expression.
VI. 6. To evaluate a given postfix expressionVII. 7. To add and delete a passenger from a linear queue of passengers.(*)
VIII. 8. To add an element to a circleular queue.IX. 9. Create a static linear queue of characters. Write functions to add characters and delete
characters. Write function to traverse the queue, while doing so, count the number of
vowels. Take care of all conditions.
X. 10. Repeat above program with circleular queue.
-
7/29/2019 Assignment 5 - Stacks and Queues
3/22
Assignment 3 Stacks and Queues 18 Aug 2013
Akshay Tiwary XII A
Question 1:#include using namespace std;class Stack{
char word[50];int end;public:
Stack(){
end = -1;}void push(char element){
if(end != 49){
end++;word[end] = element;
}
else{
cout
-
7/29/2019 Assignment 5 - Stacks and Queues
4/22
Assignment 3 Stacks and Queues 18 Aug 2013
Akshay Tiwary XII A
Question 2:
#include
using namespace std;
class Stack {
char word[50];
int end;
public:
Stack() {
end = -1;
}
void push(char element) {
if(end != 49) {
end++;
word[end] = element;
}
else {
cout
-
7/29/2019 Assignment 5 - Stacks and Queues
5/22
Assignment 3 Stacks and Queues 18 Aug 2013
Akshay Tiwary XII A
int palindrome(char array1[],char array2[],int len1, int len2) {
if(len1 != len2)return 0;
for(int i = 0; i < len1; i++)
if(array1[i] != array2[i])
return 0;
return 1;
}
int main() {
int n;
cout n;
Stack characters;
char array_forward[50];
for(int i = 0; i < n;i++) {
char temp;
cin >> temp;
characters.push(temp);
array_forward[i] = temp;
}
char array_backward[50];
for(int i = 0; i < n; i++) {
array_backward[i] = characters.pop();
}
int check = palindrome(array_forward,array_backward,n,n);
if(check)
cout
-
7/29/2019 Assignment 5 - Stacks and Queues
6/22
Assignment 3 Stacks and Queues 18 Aug 2013
Akshay Tiwary XII A
Question 3:
#include
using namespace std;
class Stack {
int binary[50];
int end;
public:
Stack() {
end = -1;
}
void push(int element) {
if(end != 49) {
end++;
binary[end] = element;
}
else {
cout
-
7/29/2019 Assignment 5 - Stacks and Queues
7/22
Assignment 3 Stacks and Queues 18 Aug 2013
Akshay Tiwary XII A
int main() {
int number;
cin >> number;
Stack binary;
while(number != 0) {
int temp = number%2;
binary.push(temp);
number = number/2;
}
int flag = 1;
while(flag) {
int temp = binary.pop();
if(temp == -1) break;
else{
cout
-
7/29/2019 Assignment 5 - Stacks and Queues
8/22
Assignment 3 Stacks and Queues 18 Aug 2013
Akshay Tiwary XII A
Question 4:
#include
using namespace std;
int my_pow(int base,int exponent) {
int product = 1;
for(int i = 0; i < exponent; i++)
product = product*base;
return product;
}
class Stack {
int number[50];
int end;
public:
Stack() {
end = -1;
}
void push(int element) {
if(end != 49) {
end++;
number[end] = element;
}
else {
cout
-
7/29/2019 Assignment 5 - Stacks and Queues
9/22
Assignment 3 Stacks and Queues 18 Aug 2013
Akshay Tiwary XII A
return temp;
}
else {
return -1;
}
}};
int main() {
int number;
cin >> number;
Stack num;
int temp = number;
int numDigits = 0;
while(temp != 0) {
num.push(temp%10);
temp = temp/10;
numDigits++;
}
int sum = 0;
int flag = 1;
while(flag) {
int temp = num.pop();
if(temp == -1) break;
else {
sum += my_pow(temp,numDigits);
}
} if(number == sum)
cout
-
7/29/2019 Assignment 5 - Stacks and Queues
10/22
Assignment 3 Stacks and Queues 18 Aug 2013
Akshay Tiwary XII A
Question 5:#include
using namespace std;
int priority(char opera) {
switch(opera) {
case '%':
return 5;
break;
case '/':
return 4;
break;
case '*':
return 3;
break;
case '+':
return 2;
break;
case '-':
return 1;
break;
default:
return 0;
}
}
class Stack {
char expression[50];
int end;
public:
Stack() {
end = -1;
}
-
7/29/2019 Assignment 5 - Stacks and Queues
11/22
Assignment 3 Stacks and Queues 18 Aug 2013
Akshay Tiwary XII A
char current() {
return expression[end];
}
int length(){
return end;
}
void push(char element) {
if(end != 49) {
end++;
expression[end] = element;
}
else {
cout
-
7/29/2019 Assignment 5 - Stacks and Queues
12/22
Assignment 3 Stacks and Queues 18 Aug 2013
Akshay Tiwary XII A
cin >> expression[ctr];
if(expression[ctr] == ';')break;
ctr++; }
Stack stack;
for (int i = 0; expression[i] != '\0'; i++) {
if(isdigit(expression[i])) {
cout
-
7/29/2019 Assignment 5 - Stacks and Queues
13/22
Assignment 3 Stacks and Queues 18 Aug 2013
Akshay Tiwary XII A
Question 6:#include
#include
using namespace std;
class Stack {
int numbers[50];
int end;
public:
Stack() {
end = -1;
}
int empty() {
return (end == -1);
}
void push(int element) {
if(end != 49) {
end++;
numbers[end] = element;
}
else {
cout
-
7/29/2019 Assignment 5 - Stacks and Queues
14/22
Assignment 3 Stacks and Queues 18 Aug 2013
Akshay Tiwary XII A
int main() {
string input_expression;
getline(cin,input_expression);
int operand1,operand2, result;
Stack stack;
int i = 0;
while(i < input_expression.length()) {
if(isspace(input_expression[i])) {
}
else if(isdigit(input_expression[i])) {
stack.push((int)(input_expression[i] - '0'));
}
else{
operand2 = stack.pop();
operand1 = stack.pop();
switch(input_expression[i]) {
case '+':
result = operand1 + operand2;
stack.push(result);
break;
case '-':
result = operand1 - operand2;
stack.push(result);
break;
case '*':
result = operand1 * operand2;
stack.push(result);
break;
case '/':
result = operand1 / operand2;
-
7/29/2019 Assignment 5 - Stacks and Queues
15/22
Assignment 3 Stacks and Queues 18 Aug 2013
Akshay Tiwary XII A
stack.push(result);
break;
default:
break;
}
}
i++;
}
cout
-
7/29/2019 Assignment 5 - Stacks and Queues
16/22
Assignment 3 Stacks and Queues 18 Aug 2013
Akshay Tiwary XII A
Question 7:#include
using namespace std;
struct passenger {
char name[50];
int seat_num;
};
int ctr = 0;
class queue {
passenger p[50];
int rear,front;
public: void add(char name1[],int seat_num1) {
if(ctr = 0){
rear = -1;
front = -1; }
if(rear==49)
cout
-
7/29/2019 Assignment 5 - Stacks and Queues
17/22
Assignment 3 Stacks and Queues 18 Aug 2013
Akshay Tiwary XII A
else
return p[++front];
} };
int main() {
queue q;
int choice;
do {
cout choice;
switch(choice) {
case 1:
char name[50];
int seat_num;
cout > name >> seat_num;
q.add(name,snum);
break;
case 2:
passenger p=q.remove();
cout
-
7/29/2019 Assignment 5 - Stacks and Queues
18/22
Assignment 3 Stacks and Queues 18 Aug 2013
Akshay Tiwary XII A
Question 8:#include
using namespace std;
int main() {
int circle[10];
int rear,front;
rear = front=0;
for(int i=0;i
-
7/29/2019 Assignment 5 - Stacks and Queues
19/22
Assignment 3 Stacks and Queues 18 Aug 2013
Akshay Tiwary XII A
Question 9:#include
using namespace std;
void add(char character, int &rear, int &front, char ch[10]) {
if(rear==9)
cout
-
7/29/2019 Assignment 5 - Stacks and Queues
20/22
Assignment 3 Stacks and Queues 18 Aug 2013
Akshay Tiwary XII A
int main()
{
char ch[10];
int rear,front;
rear=front=-1;
char cha;
for(int i=0;i
-
7/29/2019 Assignment 5 - Stacks and Queues
21/22
Assignment 3 Stacks and Queues 18 Aug 2013
Akshay Tiwary XII A
Question 10:#include
using namespace std;
void add(int &rear, int &front, char ch[10]) {
if(((rear+1)%10)==front) {
cout
-
7/29/2019 Assignment 5 - Stacks and Queues
22/22
Assignment 3 Stacks and Queues 18 Aug 2013
return c;
}
int main()
{
char ch[10];
int rear, front;
rear=front=-1;
char cha;
for(int i=0;i