Sei sulla pagina 1di 22

Assignment 3

Stacks and Queues

18 Aug 2013

Assignment 4
Stacks & Queues
Akshay Tiwary XII A 03

Akshay Tiwary XII A

Assignment 3

Stacks and Queues

18 Aug 2013

Assignment
I. II. III. IV. V. VI. VII. VIII. IX. To reverse a string using stack. 2. To check if a given string is palindrome or not.(*) 3. To convert a decimal number 135 to binary using stack.(*) 4. To check if a given number is armstrong number or not. 5. To convert an infix to a post fix expression. 6. To evaluate a given postfix expression 7. To add and delete a passenger from a linear queue of passengers.(*) 8. To add an element to a circleular queue. 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. 10. Repeat above program with circleular queue.

X.

Akshay Tiwary XII A

Assignment 3

Stacks and Queues

18 Aug 2013

Question 1:
#include <iostream> 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 << "Stack Overflow. " << endl; } } char pop() { if(end != -1) { char temp = word[end]; word[end] = '\0'; end--; return temp; } else { return '\0'; } } };

Akshay Tiwary XII A

Assignment 3

Stacks and Queues

18 Aug 2013

Question 2:
#include <iostream> 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 << "Stack Overflow. " << endl; } } char pop() { if(end != -1) { char temp = word[end]; word[end] = '\0'; end--; return temp; } else { return '\0'; } } }; {

Akshay Tiwary XII A

Assignment 3

Stacks and Queues

18 Aug 2013

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 << "How many character's are you going to enter? " << endl; cin >> 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 << "Palindrome" << endl; else cout << "Not a Palindrome" << endl;

system("pause"); return 0; }

Akshay Tiwary XII A

Assignment 3

Stacks and Queues

18 Aug 2013

Question 3:
#include<iostream> 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 << "Stack Overflow. " << endl; } } int pop() { if(end != -1) { int temp = binary[end]; binary[end] = -1; end--; return temp; } Else { return -1; } } };

Akshay Tiwary XII A

Assignment 3
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) {

Stacks and Queues

18 Aug 2013

int temp = binary.pop(); if(temp == -1) break; else{ cout << temp; } } cout << endl; system("pause"); return 0; }

Akshay Tiwary XII A

Assignment 3

Stacks and Queues

18 Aug 2013

Question 4:
#include <iostream> 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 << "Stack Overflow. " << endl; } } int pop() { if(end != -1) { int temp = number[end]; number[end] = -1; end--;

Akshay Tiwary XII A

Assignment 3
return temp; } else { return -1; } }}; int main() { int number; cin >> number; Stack num; int temp = number; int numDigits = 0; while(temp != 0) {

Stacks and Queues

18 Aug 2013

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 << "Armstrong Number" << endl; else cout << "Not an Armstrong number" << endl;

system("pause"); return 0; }

Akshay Tiwary XII A

Assignment 3

Stacks and Queues

18 Aug 2013

Question 5:
#include <iostream> 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; } {

Akshay Tiwary XII A

Assignment 3
char current() { return expression[end]; } int length(){ return end; } void push(char element) { if(end != 49) { end++;

Stacks and Queues

18 Aug 2013

expression[end] = element; } else { cout << "Stack Overflow. " << endl; } } char pop() { if(end != -1) { char temp = expression[end]; expression[end] = -1; end--; return temp; } else { return '\0'; } } }; int main() { char expression[50], trash[10]; int ctr = 0; while(true) {

Akshay Tiwary XII A

Assignment 3
cin >> expression[ctr];

Stacks and Queues

18 Aug 2013

if(expression[ctr] == ';')break; ctr++; Stack stack; for (int i = 0; expression[i] != '\0'; i++) if(isdigit(expression[i])) { cout << expression[i]; } { }

else if (expression[i] == '(') { stack.push(expression[i]); else if (expression[i] == ')') do { char temporary = stack.pop(); if (temporary != '(') cout << temporary; } while (stack.length() >= 0); } else if (expression[i] == ';') { while(true){ char temp = stack.pop(); if(temp == '\0') break; else } }else { if(priority(expression[i]) < priority(stack.current())) cout << stack.pop(); stack.push(expression[i]); } } cout << endl; system("pause"); return 0; } cout << temp; { }

Akshay Tiwary XII A

Assignment 3

Stacks and Queues

18 Aug 2013

Question 6:
#include <iostream> #include <string> 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 << "Stack Overflow. " << endl; } } int pop() { if(end != -1){ int temp = numbers[end]; numbers[end] = -1; end--; return temp; } else {return '\0'; }}};

Akshay Tiwary XII A

Assignment 3
int main() { string input_expression; getline(cin,input_expression); int operand1,operand2, result; Stack stack; int i = 0;

Stacks and Queues

18 Aug 2013

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;

Akshay Tiwary XII A

Assignment 3

Stacks and Queues


stack.push(result); break; default: break; } } i++;

18 Aug 2013

} cout << result << endl; system("pause"); return 0; }

Akshay Tiwary XII A

Assignment 3

Stacks and Queues

18 Aug 2013

Question 7:
#include<iostream> 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<<"Queue full"; else { rear++; strcpy(p[rear].name,name1); seat_num1=p[rear].seat_num; } ctr ++; } passenger remove() { if(front==rear) cout<<"Queue empty"; else if(front==49) front=-1; rear=-1; } { }

Akshay Tiwary XII A

Assignment 3
else

Stacks and Queues

18 Aug 2013

return p[++front]; } int main() { queue q; int choice; do { cout << "1.Insert 2.Delete 3.Exit" << endl; cin >> choice; switch(choice) { case 1: char name[50]; int seat_num; cout << "Enter name and seat number"; cin >> name >> seat_num; q.add(name,snum); break; case 2: passenger p=q.remove(); cout << p.name; cout << p.seat_num; break; case 3: break; default: cout<<"Wrong Input" << endl; } }while(choice != 3); system("pause"); return 0; } };

Akshay Tiwary XII A

Assignment 3

Stacks and Queues

18 Aug 2013

Question 8:
#include<iostream> using namespace std; int main() { int circle[10]; int rear,front; rear = front=0; for(int i=0;i<10;i++) {

if(((rear+1)%10)==front) { cout<<"Queue full"; break; } else { int val; cout<<"Enter value\n"; cin>>val; rear=(rear+1)%10; circle[rear]=val; } } system("pause"); }

Akshay Tiwary XII A

Assignment 3

Stacks and Queues

18 Aug 2013

Question 9:
#include<iostream> using namespace std; void add(char character, int &rear, int &front, char ch[10]) { if(rear==9) cout<<"Queue full"; else { rear++; ch[rear]=cha; } } char del(int &rear,int &front,char ch[10]) { if(front==rear) cout<<"Queue empty"; else if(front==9) { front=-1; rear=-1; } else return ch[++front]; } int vowels(int &rear,int &front,char ch[10]) { char chak; for(int i=0;i<10;i++) { chak=del(rear,front,ch); if(chak=='a' || chak=='e' || chak=='i' || chak=='o' || chak=='u') { c++; } } return c; } int c=0;

Akshay Tiwary XII A

Assignment 3
int main() { char ch[10]; int rear,front; rear=front=-1; char cha; for(int i=0;i<10;i++) {

Stacks and Queues

18 Aug 2013

cout<<"enter character"; cin>>cha; add(cha, rear, front, ch); } cout<<vowels(rear, front, ch); system("pause"); return 0; }

Akshay Tiwary XII A

Assignment 3

Stacks and Queues

18 Aug 2013

Question 10:
#include<iostream> using namespace std; void add(int &rear, int &front, char ch[10]) { if(((rear+1)%10)==front) cout<<"Queue full"; } else { char val; cout<<"Enter value\n"; cin>>val; rear=(rear+1)%10; ch[rear]=val; } } char del(int &rear,int &front,char ch[10]) { if(front==rear) cout<<"Queue empty"; else front=(front+1)%10; return ch[front]; } int vowels(int &rear,int &front,char ch[10]) { int c=0; char chak; for(int i=0;i<10;i++) { chak=del(rear,front,ch); if(chak=='a' || chak=='e' || chak=='i' || chak=='o' || chak=='u') { c++; } } {

Akshay Tiwary XII A

Assignment 3
return c; } int main() { char ch[10]; int rear, front; rear=front=-1; char cha; for(int i=0;i<10;i++){ add(rear, front, ch); }

Stacks and Queues

18 Aug 2013

cout<<vowels(rear, front, ch); system("pause"); return 0; }

Akshay Tiwary XII A

Potrebbero piacerti anche