You are on page 1of 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

class Stack { char word[50]. " << endl. return temp. public: Stack() { end = -1. } else { cout << "Stack Overflow. } } char pop() { if(end != -1) { char temp = word[end]. int end. end--. word[end] = '\0'.Assignment 3 Stacks and Queues 18 Aug 2013 Question 1: #include <iostream> using namespace std. Akshay Tiwary XII A . } void push(char element) { if(end != 49) { end++. word[end] = element. } } }. } else { return '\0'.

class Stack { char word[50]. return temp. } else { return '\0'. } } }. word[end] = element. public: Stack() { end = -1.Assignment 3 Stacks and Queues 18 Aug 2013 Question 2: #include <iostream> using namespace std. { Akshay Tiwary XII A . } } char pop() { if(end != -1) { char temp = word[end]. word[end] = '\0'. } else { cout << "Stack Overflow. } void push(char element) if(end != 49) { end++. int end. " << endl. end--.

array_forward[i] = temp. i < len1. for(int i = 0. return 1. int len2) { if(len1 != len2)return 0.i++) { char temp.n).n.char array2[]. for(int i = 0. } int main() { int n. } Akshay Tiwary XII A . i++) { array_backward[i] = characters. i < n. cin >> temp. } int check = palindrome(array_forward. else cout << "Not a Palindrome" << endl.push(temp). system("pause"). characters.array_backward. for(int i = 0. return 0.Assignment 3 Stacks and Queues 18 Aug 2013 int palindrome(char array1[]. i++) if(array1[i] != array2[i]) return 0. cin >> n.int len1. } char array_backward[50]. Stack characters. if(check) cout << "Palindrome" << endl.pop(). cout << "How many character's are you going to enter? " << endl. i < n. char array_forward[50].

class Stack { int binary[50]. } } }. binary[end] = element. } void push(int element) { if(end != 49) { end++. } else { cout << "Stack Overflow. binary[end] = -1. int end. } Else { return -1. } } int pop() { if(end != -1) { int temp = binary[end].Assignment 3 Stacks and Queues 18 Aug 2013 Question 3: #include<iostream> using namespace std. return temp. Akshay Tiwary XII A . public: Stack() { end = -1. end--. " << endl.

return 0.push(temp). } } cout << endl. while(flag) { Stacks and Queues 18 Aug 2013 int temp = binary. cin >> number. else{ cout << temp. if(temp == -1) break. while(number != 0) { int temp = number%2. Stack binary. } int flag = 1. } Akshay Tiwary XII A . number = number/2. binary.Assignment 3 int main() { int number.pop(). system("pause").

" << endl. } } int pop() { if(end != -1) { int temp = number[end]. for(int i = 0. int end. return product. int my_pow(int base. number[end] = element. i < exponent. } else { cout << "Stack Overflow. public: Stack() { end = -1.int exponent) { int product = 1. } class Stack { int number[50]. i++) product = product*base. } void push(int element) { if(end != 49) { end++. end--.Assignment 3 Stacks and Queues 18 Aug 2013 Question 4: #include <iostream> using namespace std. number[end] = -1. Akshay Tiwary XII A .

} }}. if(temp == -1) break. system("pause"). } else { return -1. while(temp != 0) { Stacks and Queues 18 Aug 2013 num. else cout << "Not an Armstrong number" << endl. int temp = number.Assignment 3 return temp. return 0. } } if(number == sum) cout << "Armstrong Number" << endl. numDigits++. Stack num. temp = temp/10. int flag = 1.pop(). } int sum = 0.push(temp%10). } Akshay Tiwary XII A . int numDigits = 0. int main() { int number.numDigits). else { sum += my_pow(temp. while(flag) { int temp = num. cin >> number.

break. break. case '+': return 2. } { Akshay Tiwary XII A . break.Assignment 3 Stacks and Queues 18 Aug 2013 Question 5: #include <iostream> using namespace std. break. int priority(char opera) { switch(opera) case '%': return 5. case '/': return 4. } } class Stack { char expression[50]. int end. case '-': return 1. public: Stack() { end = -1. case '*': return 3. break. default: return 0.

} void push(char element) { if(end != 49) { end++. end--. expression[end] = -1. return temp. } else { return '\0'. trash[10]. } } char pop() { if(end != -1) { char temp = expression[end]. } } }. int main() { char expression[50]. } else { cout << "Stack Overflow. int ctr = 0.Assignment 3 char current() { return expression[end]. while(true) { Akshay Tiwary XII A . Stacks and Queues 18 Aug 2013 expression[end] = element. " << endl. } int length(){ return end.

current())) cout << stack.length() >= 0). } } cout << endl. if (temporary != '(') cout << temporary.') { while(true){ char temp = stack. } while (stack. for (int i = 0. } { } else if (expression[i] == '(') { stack. Stacks and Queues 18 Aug 2013 if(expression[ctr] == '. else } }else { if(priority(expression[i]) < priority(stack. Stack stack. return 0. ctr++.push(expression[i]).Assignment 3 cin >> expression[ctr].push(expression[i]).')break. i++) if(isdigit(expression[i])) { cout << expression[i].pop(). system("pause"). } cout << temp.pop(). else if (expression[i] == ')') do { char temporary = stack. stack. if(temp == '\0') break.pop(). } else if (expression[i] == '. { } Akshay Tiwary XII A . expression[i] != '\0'.

} else { cout << "Stack Overflow. }}}. numbers[end] = element. return temp. } int empty() { return (end == -1). end--. public: Stack() { end = -1. " << endl. } else {return '\0'. } } int pop() { if(end != -1){ int temp = numbers[end]. } void push(int element) { if(end != 49) { end++. numbers[end] = -1. int end.Assignment 3 Stacks and Queues 18 Aug 2013 Question 6: #include <iostream> #include <string> using namespace std. class Stack { int numbers[50]. Akshay Tiwary XII A .

case '-': result = operand1 . case '/': result = operand1 / operand2. Akshay Tiwary XII A . getline(cin.operand2.pop(). int operand1.push(result). break.push(result). stack.input_expression). stack.push(result).length()) { if(isspace(input_expression[i])) { } else if(isdigit(input_expression[i])) { stack. } else{ operand2 = stack. case '*': result = operand1 * operand2.Assignment 3 int main() { string input_expression. int i = 0. Stack stack. stack. result. Stacks and Queues 18 Aug 2013 while(i < input_expression.pop(). break.operand2.'0')). break.push((int)(input_expression[i] . switch(input_expression[i]) { case '+': result = operand1 + operand2. operand1 = stack.

Assignment 3 Stacks and Queues stack. 18 Aug 2013 } cout << result << endl. break. default: break. } } i++. return 0.push(result). } Akshay Tiwary XII A . system("pause").

seat_num1=p[rear]. } { } Akshay Tiwary XII A .Assignment 3 Stacks and Queues 18 Aug 2013 Question 7: #include<iostream> using namespace std. front = -1.int seat_num1) { if(ctr = 0){ rear = -1. struct passenger { char name[50]. else if(front==49) front=-1.front. } ctr ++. rear=-1.name. public: void add(char name1[]. } passenger remove() { if(front==rear) cout<<"Queue empty".name1). if(rear==49) cout<<"Queue full". strcpy(p[rear]. int rear. else { rear++. }. class queue { passenger p[50].seat_num. int ctr = 0. int seat_num.

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

circle[rear]=val. break. } else { int val.i++) { if(((rear+1)%10)==front) { cout<<"Queue full".front. rear = front=0. } Akshay Tiwary XII A . cin>>val. for(int i=0. int main() { int circle[10].Assignment 3 Stacks and Queues 18 Aug 2013 Question 8: #include<iostream> using namespace std. cout<<"Enter value\n". int rear. rear=(rear+1)%10.i<10. } } system("pause").

Assignment 3 Stacks and Queues 18 Aug 2013 Question 9: #include<iostream> using namespace std. } } return c.front.char ch[10]) { if(front==rear) cout<<"Queue empty". } int c=0. else if(front==9) { front=-1.i<10.int &front. else { rear++. rear=-1. Akshay Tiwary XII A .ch). char ch[10]) { if(rear==9) cout<<"Queue full". for(int i=0.i++) { chak=del(rear. void add(char character. if(chak=='a' || chak=='e' || chak=='i' || chak=='o' || chak=='u') { c++. } } char del(int &rear. } else return ch[++front]. int &rear. int &front. } int vowels(int &rear.int &front. ch[rear]=cha.char ch[10]) { char chak.

} Akshay Tiwary XII A . rear=front=-1. } cout<<vowels(rear. char cha.front.Assignment 3 int main() { char ch[10]. int rear. front. front. add(cha. rear. system("pause").i++) { Stacks and Queues 18 Aug 2013 cout<<"enter character". for(int i=0. cin>>cha. ch). return 0.i<10. ch).

i++) { chak=del(rear. rear=(rear+1)%10.i<10. char chak.Assignment 3 Stacks and Queues 18 Aug 2013 Question 10: #include<iostream> using namespace std.int &front.front. void add(int &rear. return ch[front].char ch[10]) { int c=0. cout<<"Enter value\n".char ch[10]) { if(front==rear) cout<<"Queue empty". cin>>val. else front=(front+1)%10. int &front. } } { Akshay Tiwary XII A . ch[rear]=val. } } char del(int &rear. } int vowels(int &rear.ch).int &front. char ch[10]) { if(((rear+1)%10)==front) cout<<"Queue full". if(chak=='a' || chak=='e' || chak=='i' || chak=='o' || chak=='u') { c++. } else { char val. for(int i=0.

} int main() { char ch[10]. front. front. rear=front=-1. front.Assignment 3 return c. ch). int rear. } Akshay Tiwary XII A . ch).i++){ add(rear. system("pause"). char cha. return 0.i<10. for(int i=0. } Stacks and Queues 18 Aug 2013 cout<<vowels(rear.