## Knowledge in data strcutures

Python Career Path

An comprehensive guide for career options with Python Programming Language. Learn Python Programming now and boost up your career ! For FREE Video Course visit :- https://www.edyoda.com/course/98

Double Ended Queue

# include&lt;stdio.h&gt; #include&lt;process.h&gt; #define MAX 30 typedef struct dequeue { int data[MAX]; int rear,front; }dequeue; void initialize(dequeue *p); int empty(dequeue *p); int full(dequeue *p); void enqueueR(dequeue *p,int x); void enqueueF(dequeue *p,int x); int dequeueF(dequeue *p); int dequeueR(dequeue *p); void print(dequeue *p); void main() { int i,x,op,n; dequeue q; initialize(&amp;q); do { printf(&quot;\n1.Create\n2.Insert(rear)\n3.Insert(front)\n4.Delete(rear)\n5.Delete(front)&quot;); printf(&quot;\n6.Print\n7.Exit\n\nEnter your choice:&quot;); scanf(&quot;%d&quot;,&amp;op); switch(op) { case 1: printf(&quot;\nEnter number of elements:&quot;); scanf(&quot;%d&quot;,&amp;n); initialize(&amp;q); printf(&quot;\nEnter the data:&quot;); for(i=0;i&lt;n;i++) { scanf(&quot;%d&quot;,&amp;x); if(full(&amp;q)) { printf(&quot;\nQueue is full!!&quot;); exit(0); } enqueueR(&amp;q,x); } break; case 2: printf(&quot;\nEnter element to be inserted:&quot;); scanf(&quot;%d&quot;,&amp;x); if(full(&amp;q)) { printf(&quot;\nQueue is full!!&quot;); exit(0); } enqueueR(&amp;q,x); break; case 3: printf(&quot;\nEnter the element to be inserted:&quot;); scanf(&quot;%d&quot;,&amp;x); if(full(&amp;q)) { printf(&quot;\nQueue is full!!&quot;); exit(0); } enqueueF(&amp;q,x); break; case 4: if(empty(&amp;q)) { printf(&quot;\nQueue is empty!!&quot;); exit(0); } x=dequeueR(&amp;q); printf(&quot;\nElement deleted is %d\n&quot;,x); break; case 5: if(empty(&amp;q)) { printf(&quot;\nQueue is empty!!&quot;); exit(0); } x=dequeueF(&amp;q); printf(&quot;\nElement deleted is %d\n&quot;,x); break; case 6: print(&amp;q); break; default: break; } }while(op!=7); } void initialize(dequeue *P) { P-&gt;rear=-1; P-&gt;front=-1; } int empty(dequeue *P) { if(P-&gt;rear==-1) return(1); return(0); } int full(dequeue *P) { if((P-&gt;rear+1)%MAX==P-&gt;front) return(1); return(0); } void enqueueR(dequeue *P,int x) { if(empty(P)) { P-&gt;rear=0; P-&gt;front=0; P-&gt;data[0]=x; } else { P-&gt;rear=(P-&gt;rear+1)%MAX; P-&gt;data[P-&gt;rear]=x; } } void enqueueF(dequeue *P,int x) { if(empty(P)) { P-&gt;rear=0; P-&gt;front=0; P-&gt;data[0]=x; } else { P-&gt;front=(P-&gt;front-1+MAX)%MAX; P-&gt;data[P-&gt;front]=x; } } int dequeueF(dequeue *P) { int x; x=P-&gt;data[P-&gt;front]; if(P-&gt;rear==P-&gt;front) //delete the last element initialize(P); else P-&gt;front=(P-&gt;front+1)%MAX; return(x); } int dequeueR(dequeue *P) { int x; x=P-&gt;data[P-&gt;rear]; if(P-&gt;rear==P-&gt;front) initialize(P); else P-&gt;rear=(P-&gt;rear-1+MAX)%MAX; return(x); } void print(dequeue *P) { if(empty(P)) { printf(&quot;\nQueue is empty!!&quot;); exit(0); } int i; i=P-&gt;front; while(i!=P-&gt;rear) { printf(&quot;\n%d&quot;,P-&gt;data[i]); i=(i+1)%MAX; } printf(&quot;\n%d\n&quot;,P-&gt;data[P-&gt;rear]); }

Learn C programming

Hey, in case you are looking for a good C programming tutorial, then you are at correct place.&nbsp; I am going to start C programming tutorial for all who want to learn. Starting from very basic to advanced topics in C will be covered in this tutorial. So, we will start from very basic topics like a variable, data type, and all these things then we will move towards array, functions, strings etc. After that, we will move to data-structures. So, it&#39;s going to be very excited and the teaching technique is going to be unique. Hope, you will enjoy this course. It&#39;s completely free of cost. So, don&#39;t miss this opportunity. See you soon.

BFS tree

FIFO using JAVA

import java.util.LinkedList; import java.util.Queue; &nbsp;&nbsp; public class QueueExample { &nbsp;&nbsp;&nbsp;&nbsp;public static void main(String[] args) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Queue&lt;Integer&gt; q = new LinkedList&lt;&gt;(); &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Adds elements {0, 1, 2, 3, 4} to queue &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int i = 0; i &lt; 5; i++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q.add(i); &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Display contents of the queue. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;Elements of queue-&quot; + q); &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// To remove the head of queue. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// In this the oldest element &#39;0&#39; will be removed &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int removedele = q.remove(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;removed element-&quot; + removedele); &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(q); &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// To view the head of queue &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int head = q.peek(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;head of queue-&quot; + head); &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Rest all methods of collection interface, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Like size and contains can be used with this &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// implementation. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int size = q.size(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;Size of queue-&quot; + size); &nbsp;&nbsp;&nbsp;&nbsp;} }

Sorting a Queue without using Extra Space

#include &lt;bits/stdc++.h&gt; using namespace std; &nbsp;&nbsp; // Queue elements after sortedIndex are&nbsp; // already sorted. This function returns // index of minimum element from front to // sortedIndex int minIndex(queue&lt;int&gt; &amp;q, int sortedIndex) { &nbsp;&nbsp;&nbsp;&nbsp;int min_index = -1; &nbsp;&nbsp;&nbsp;&nbsp;int min_val = INT_MAX; &nbsp;&nbsp;&nbsp;&nbsp;int n = q.size(); &nbsp;&nbsp;&nbsp;&nbsp;for (int i=0; i&lt;n; i++) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int curr = q.front(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q.pop();&nbsp; // This is dequeue() in C++ STL &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// we add the condition i &lt;= sortedIndex &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// because we don&#39;t want to traverse &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// on the sorted part of the queue, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// which is the right part. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (curr &lt;= min_val &amp;&amp; i &lt;= sortedIndex) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;min_index = i; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;min_val = curr; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q.push(curr);&nbsp; // This is enqueue() in&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// C++ STL &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;return min_index; } &nbsp;&nbsp; // Moves given minimum element to rear of&nbsp; // queue void insertMinToRear(queue&lt;int&gt; &amp;q, int min_index) { &nbsp;&nbsp;&nbsp;&nbsp;int min_val; &nbsp;&nbsp;&nbsp;&nbsp;int n = q.size(); &nbsp;&nbsp;&nbsp;&nbsp;for (int i = 0; i &lt; n; i++) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int curr = q.front(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q.pop(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (i != min_index) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q.push(curr); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;min_val = curr; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;q.push(min_val); } &nbsp;&nbsp; void sortQueue(queue&lt;int&gt; &amp;q) { &nbsp;&nbsp;&nbsp;&nbsp;for (int i = 1; i &lt;= q.size(); i++) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int min_index = minIndex(q, q.size() - i); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insertMinToRear(q, min_index); &nbsp;&nbsp;&nbsp;&nbsp;} } &nbsp;&nbsp; // driver code int main() { &nbsp;&nbsp;queue&lt;int&gt; q; &nbsp;&nbsp;q.push(30); &nbsp;&nbsp;q.push(11); &nbsp;&nbsp;q.push(15); &nbsp;&nbsp;q.push(4); &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;// Sort queue &nbsp;&nbsp;sortQueue(q); &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;// Print sorted queue &nbsp;&nbsp;while (q.empty() == false) &nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; q.front() &lt;&lt; &quot; &quot;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q.pop(); &nbsp;&nbsp;} &nbsp;&nbsp;cout &lt;&lt; endl; &nbsp;&nbsp;return 0; }

Subset Backtracking

Subset Backtracking in C

Implement a stack using single Queue

#include&lt;bits/stdc++.h&gt; using namespace std; &nbsp;&nbsp; // User defined stack that uses a queue class Stack { &nbsp;&nbsp;&nbsp;&nbsp;queue&lt;int&gt;q; public: &nbsp;&nbsp;&nbsp;&nbsp;void push(int val); &nbsp;&nbsp;&nbsp;&nbsp;void pop(); &nbsp;&nbsp;&nbsp;&nbsp;int top(); &nbsp;&nbsp;&nbsp;&nbsp;bool empty(); }; &nbsp;&nbsp; // Push operation void Stack::push(int val) { &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp; Get previous size of queue &nbsp;&nbsp;&nbsp;&nbsp;int s = q.size(); &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;// Push current element &nbsp;&nbsp;&nbsp;&nbsp;q.push(val); &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;// Pop (or Dequeue) all previous &nbsp;&nbsp;&nbsp;&nbsp;// elements and put them after current &nbsp;&nbsp;&nbsp;&nbsp;// element &nbsp;&nbsp;&nbsp;&nbsp;for (int i=0; i&lt;s; i++) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// this will add front element into &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// rear of queue &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q.push(q.front()); &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// this will delete front element &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q.pop(); &nbsp;&nbsp;&nbsp;&nbsp;} } &nbsp;&nbsp; // Removes the top element void Stack::pop() { &nbsp;&nbsp;&nbsp;&nbsp;if (q.empty()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; &quot;No elements\n&quot;; &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q.pop(); } &nbsp;&nbsp; // Returns top of stack int&nbsp; Stack::top() { &nbsp;&nbsp;&nbsp;&nbsp;return (q.empty())? -1 : q.front(); } &nbsp;&nbsp; // Returns true if Stack is empty else false bool Stack::empty() { &nbsp;&nbsp;&nbsp;&nbsp;return (q.empty()); } &nbsp;&nbsp; // Driver code int main() { &nbsp;&nbsp;&nbsp;&nbsp;Stack s; &nbsp;&nbsp;&nbsp;&nbsp;s.push(10); &nbsp;&nbsp;&nbsp;&nbsp;s.push(20); &nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; s.top() &lt;&lt; endl; &nbsp;&nbsp;&nbsp;&nbsp;s.pop(); &nbsp;&nbsp;&nbsp;&nbsp;s.push(30); &nbsp;&nbsp;&nbsp;&nbsp;s.pop(); &nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; s.top() &lt;&lt; endl; &nbsp;&nbsp;&nbsp;&nbsp;return 0; }

Question papers of DS

Previous year question papers of Datastructure

Stack in Data structures

stact in data structures i.e convert infix expression to postfix using a stack

Data structure using Java.

These are the complete notes of data strucures using java . Chapter wise explanation of all the topics with simple and understandable examples and thier respective java codes.