Given a deque dq containing integer elements, the task is to traverse the dq and print its elements of it.
Examples:
Input: dq = [1, 2, 3, 4, 5]
Output: 1 2 3 4 5
Explanation: We print elements of deque from from front to backInput: dq = [1]
Output: 1
Try It Yourself
Deque Traversal refers to the process of accessing each element of the deque, typically to read or process the values stored within.
#include <deque>
#include <iostream>
using namespace std;
void print(deque<int>& dq) {
for (auto x : dq) cout << x << " ";
cout << endl;
}
int main() {
deque<int> dq = {1, 2, 3, 4, 5};
print(dq);
return 0;
}
import java.util.ArrayDeque;
import java.util.Deque;
public class GfG {
public static void print(Deque<Integer> dq) {
for (int x : dq) {
System.out.print(x + " ");
}
System.out.println();
}
public static void main(String[] args) {
Deque<Integer> dq = new ArrayDeque<>();
dq.add(1);
dq.add(2);
dq.add(3);
dq.add(4);
dq.add(5);
print(dq);
}
}
from collections import deque
def print_deque(dq):
for x in dq:
print(x, end=" ")
print()
def main():
dq = deque([1, 2, 3, 4, 5])
print_deque(dq)
if __name__ == '__main__':
main()
using System;
using System.Collections.Generic;
class GfG {
static void Print(Queue<int> dq) {
foreach (var x in dq) Console.Write(x + " ");
Console.WriteLine();
}
static void Main() {
Queue<int> dq = new Queue<int>(new int[] {1, 2, 3, 4, 5});
Print(dq);
}
}
function print(dq) {
dq.forEach(x => console.log(x));
}
const dq = [1, 2, 3, 4, 5];
print(dq);
Output
1 2 3 4 5
Time Complexity: O(n)
Auxiliary Space: O(1)