Skip to content

Commit 9d04bb8

Browse files
Add files via upload
1 parent 4766987 commit 9d04bb8

File tree

5 files changed

+312
-0
lines changed

5 files changed

+312
-0
lines changed

18 - Queues and Stacks/Soln_Queue.c

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <stdio.h>
2+
#define MAX 50
3+
4+
void insert();
5+
void delete();
6+
void display();
7+
8+
int queue_array[MAX];
9+
int rear = - 1;
10+
int front = - 1;
11+
//Aditya Seth
12+
int main(){
13+
int choice;
14+
while (1){
15+
printf("1.Insert element to queue. \n");
16+
printf("2.Delete element from queue. \n");
17+
printf("3.Display all elements of queue. \n");
18+
printf("4.Quit. \n");
19+
printf("Enter your choice : ");
20+
scanf("%d", &choice);
21+
22+
switch (choice){
23+
24+
case 1:
25+
insert();
26+
break;
27+
28+
case 2:
29+
delete();
30+
break;
31+
32+
case 3:
33+
display();
34+
break;
35+
36+
case 4:
37+
exit(1);
38+
39+
default:
40+
printf("Wrong choice. \n");
41+
42+
}
43+
}
44+
return 0;
45+
}
46+
47+
void insert(){
48+
int add_item;
49+
if (rear == MAX - 1)
50+
printf("Queue Overflow. \n");
51+
52+
else{
53+
54+
if (front == - 1)
55+
front = 0;
56+
printf("Inset the element in queue : ");
57+
scanf("%d", &add_item);
58+
rear = rear + 1;
59+
queue_array[rear] = add_item;
60+
}
61+
}
62+
63+
void delete(){
64+
if (front == - 1 || front > rear){
65+
printf("Queue Underflow. \n");
66+
return ;
67+
}
68+
else{
69+
printf("Element deleted from queue is : %d\n", queue_array[front]);
70+
front = front + 1;
71+
}
72+
}
73+
74+
void display(){
75+
int i;
76+
if (front == - 1)
77+
printf("Queue is empty. \n");
78+
else
79+
{
80+
printf("Queue is : \n");
81+
for (i = front; i <= rear; i++)
82+
printf("%d ", queue_array[i]);
83+
printf("\n");
84+
}
85+
}

18 - Queues and Stacks/Soln_Stacks.c

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <stdio.h>
2+
#include <conio.h>
3+
int ch, n, x, top = -1, i, stack[100];
4+
void push();
5+
void pop();
6+
void display();
7+
//Aditya Seth
8+
int main()
9+
{
10+
printf("Enter Number:\n");
11+
scanf("%d", &n);
12+
do
13+
{
14+
printf("1. Push\n 2. Pop\n 3. Display\n 4. Exit\n");
15+
scanf("%d", &ch);
16+
switch (ch)
17+
{
18+
case 1:
19+
push();
20+
break;
21+
case 2:
22+
pop();
23+
case 3:
24+
display();
25+
break;
26+
case 4:
27+
printf("bye:\n");
28+
break;
29+
default:
30+
printf("wrong choice. \n");
31+
}
32+
} while (ch != 4);
33+
return 0;
34+
}
35+
36+
void push()
37+
{
38+
if (top == n - 1)
39+
{
40+
printf("Full\n");
41+
}
42+
else
43+
{
44+
printf("Enter elements: \n");
45+
scanf("%d", &x);
46+
top++;
47+
stack[top] = x;
48+
}
49+
}
50+
51+
void pop()
52+
{
53+
if (top == -1)
54+
{
55+
top--;
56+
x = stack[top];
57+
}
58+
else
59+
{
60+
printf("Empty\n");
61+
}
62+
}
63+
void display()
64+
{
65+
if (top > 0)
66+
{
67+
for (i = 0; i < top; i++)
68+
{
69+
printf("%d", stack[i]);
70+
}
71+
}
72+
}

18 - Queues and Stacks/Solution.cpp

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <iostream>
2+
#include <queue>
3+
#include <stack>
4+
5+
using namespace std;
6+
7+
class Solution {
8+
private:
9+
queue<char> q;
10+
stack<char> s;
11+
12+
public:
13+
char popCharacter() {
14+
char tmp = s.top();
15+
s.pop();
16+
return tmp;
17+
}
18+
19+
void pushCharacter(char ch) {
20+
s.push(ch);
21+
}
22+
23+
char dequeueCharacter() {
24+
char tmp = q.front();
25+
q.pop();
26+
return tmp;
27+
}
28+
29+
void enqueueCharacter(char ch) {
30+
q.push(ch);
31+
}
32+
};
33+
34+
int main() {
35+
36+
string s;
37+
getline(cin, s);
38+
//Aditya Seth
39+
Solution obj;
40+
41+
for (int i = 0; i < s.length(); i++) {
42+
obj.pushCharacter(s[i]);
43+
obj.enqueueCharacter(s[i]);
44+
}
45+
46+
bool isPalindrome = true;
47+
48+
for (int i = 0; i < s.length() / 2; i++) {
49+
if (obj.popCharacter() != obj.dequeueCharacter()) {
50+
isPalindrome = false;
51+
52+
break;
53+
}
54+
}
55+
56+
if (isPalindrome) {
57+
cout << "The word, " << s << ", is a palindrome.";
58+
} else {
59+
cout << "The word, " << s << ", is not a palindrome.";
60+
}
61+
62+
return 0;
63+
}

18 - Queues and Stacks/Solution.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Solution:
2+
def __init__(self):
3+
self.stack = []
4+
self.queue = []
5+
6+
def popCharacter(self):
7+
return self.stack.pop()
8+
9+
def pushCharacter(self, char):
10+
self.stack.append(char)
11+
12+
def dequeueCharacter(self):
13+
char = self.queue[0]
14+
self.queue = self.queue[1:]
15+
return char
16+
17+
def enqueueCharacter(self, char):
18+
self.queue.append(char)
19+
20+
#Aditya Seth
21+
22+
s = input()
23+
24+
obj = Solution()
25+
26+
l = len(s)
27+
28+
for i in range(l):
29+
obj.pushCharacter(s[i])
30+
obj.enqueueCharacter(s[i])
31+
32+
isPalindrome = True
33+
'''
34+
pop the top character from stack
35+
dequeue the first character from queue
36+
compare both the characters
37+
'''
38+
for i in range(l // 2):
39+
if obj.popCharacter() != obj.dequeueCharacter():
40+
isPalindrome = False
41+
break
42+
43+
if isPalindrome:
44+
print("The word, " + s + ", is a palindrome.")
45+
else:
46+
print("The word, " + s + ", is not a palindrome.")

18 - Queues and Stacks/Solution.rb

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Solution
2+
def initialize
3+
@queue = Queue.new
4+
@stack = []
5+
end
6+
7+
def push_character(char)
8+
@stack << char
9+
end
10+
def pop_character
11+
@stack.pop
12+
end
13+
def enqueue_character(char)
14+
@queue << char
15+
end
16+
def dequeue_character
17+
@queue.pop
18+
end
19+
end
20+
21+
solution = Solution.new
22+
23+
input = gets
24+
25+
input.split('').each do |c|
26+
27+
solution.push_character c
28+
29+
solution.enqueue_character c
30+
end
31+
32+
is_palindrome = true
33+
34+
(input.length / 2).times do
35+
if solution.pop_character != solution.dequeue_character
36+
is_palindrome = false
37+
break
38+
end
39+
end
40+
41+
42+
if is_palindrome
43+
puts "The word, #{input}, is a palindrome."
44+
else
45+
puts "The word, #{input}, is not a palindrome."
46+
end

0 commit comments

Comments
 (0)