Python coding conceptual questions -

Following is the queue implementation using stacks, and in python, we can use lists as the stack using the append and pop operation for push and pop respectively.
Following is the code for the same in python.
# Python3 program to implement Queue using
# Two Stack implementation for making a queue
# Here we can use list as the stack in python
# using the append function as the push operation
# using the pop operation from poping out from the last
class Queue:
def __init__(self):
self.s1 = []
self.s2 = []
def enQueue(self, x):
# if first stack is empty
# Add to top of self.s1
self.s1.append(x)
# Dequeue an item from the queue
def deQueue(self):
if len(self.s1) == 0:
print('Q is Empty')
while len(self.s1) != 0:
self.s2.append(self.s1[-1])
self.s1.pop()
# Push item into self.s1
x = self.s2[-1]
self.s2.pop()
# Push everything back to s1
while len(self.s2) != 0:
self.s1.append(self.s2[-1])
self.s2.pop()
return x
# Driver code
if __name__ == '__main__':
q = Queue()
q.enQueue(55)
q.enQueue(44)
q.enQueue(33)
print(q.deQueue())
print(q.deQueue())
print(q.deQueue())
2. Worst case for the dequeue operation in this implementation
can be O(n)
As we need to remove all the elements from the one stack and then
push them back again to other stack, which could take n operation
hence the complexity would be O(n).
3. dequeue for k items would be 2*k
Thus for n enqueue and n dequeue operation.
Enqueue operation: does not require any pop operation.
Dequeue operation: at any time with k entries, we require 2*k pop for one dequeue.
So for n operation: 2*n + 2*(n-1) + 2*(n-2) + 2 *(n-3) + . . . .
. + 2*(1)
= 2 *(n + n-1 + n-2 + n-3 + . . . . 1)
= 2* (n * (n-1)) /2
= n*(n-1)
Thanks,
Conceptual Questions Suppose you are given a (strange) computer that can only perform the followi...
HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD
COMMENTS:
stackARRAY:
#include<iostream>
#define SIZE 100
#define NO_ELEMENT -999999
using namespace std;
class Stack {
int arr[SIZE]; // array to store Stack elements
int top;
public:
Stack() {
top = -1;
}
void push(int); // push an element into Stack
int pop(); // pop the top element from Stack
int topElement(); // get the top element
void display(); // display Stack elements from top to bottom
};
void Stack...
help finish Queue, don't think
I have the right thing.
# 1. After studying the Stack class and testStack() functions in stack.py # complete the Queue class below (and test it with the testQueue function) # # 2. Afer studying and testing the Circle class in circle.py, # complete the Rectangle class below (and test it with the testRectangle function) # # # 3. SUBMIT THIS ONE FILE, with your updates, TO ICON. # # # NOTE: you may certainly...
In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions. You could use either the array or linked list implementation for stacks and queues. Source for stack array: --------------------------------------------------- #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the...
PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same *************************************************************************************************************** /** * Stack implementation using array in C/procedural language. */ #include <iostream> #include <cstdio> #include <cstdlib> //#include <climits> // For INT_MIN #define SIZE 100 using namespace std; /// Create a stack with capacity of 100 elements int stack[SIZE]; /// Initially stack is...
My CSC 220 teacher has given this as a homework assignment and starting it has not made much sense and am generally lost on this assignment help would be much appreciated. I put everything from the assignment power point slides she gave us in here so the expert has everything im working with as well. Dequeue Please implement your own Dequeue class which has following methods boolean add(E e)= void addLast(E e) void addFirst(E e) E getFirst( ) = E...
I RE: Singly Linked List, Stack, and Queue Implementation Suppose, you have the following Node clas. public class Node ! int id; Node next: public Node (int id) ( this.id id: Write program codes for the traditional: 1) append int id), prepend(int id), removeFirstNodeO. displayAlINodesO, and findById(int id) operations for a singly linked list 2) pushint id), pop), peek0, displayAllNodes0 operations for a stack 3) enQueue(int id), deQueuel), displayAINodes() operations for a gueue Please make sure that you declare separate...
Goals This assignment is an individual assignment and you will work on it on your own. The goal of this assignment is to be able to use stacks and queues, and to master and have an in-depth understanding of such primitive ADTs. In this assignment you will build a more complex ADT using the primitive stack and queue ADTs. Moreover, an important goal of this assignment is to be able to analyze an ADT that you will build yourself using...
I need help fixing my code. My output should be the following. Hello, world! : false A dog, a panic in a pagoda : true A dog, a plan, a canal, pagoda : true Aman, a plan, a canal--Panama! : true civic : true If I had a hi-fi : true Do geese see God? : true Madam, I’m Adam. : true Madam, in Eden, I’m Adam. : true Neil, a trap! Sid is part alien! : true Never odd...
how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 } class Stack { Node head; int size; Stack() //default constructor { this.head=null; this.size=0; } //Input = data //Output = void (just adds value to list) // method pushes elements on stack // time: O(1) // space: O(1) public void push(int data) { Node node=new Node(data); node.next=head; head=node; size++; } //Input = none //Output = top of stack // method pops value from top of...
I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts { public static void main(String[] args) { Scanner input = new Scanner(System.in); String sentence; String again; do { System.out .println("Enter a sentence, I will tell you if it is a palindrome: ");...