<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Venkat Ashish on Medium]]></title>
        <description><![CDATA[Stories by Venkat Ashish on Medium]]></description>
        <link>https://medium.com/@venkatashish?source=rss-ecaf8fce2f71------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*5xs-kIl3zMUCAR2n.</url>
            <title>Stories by Venkat Ashish on Medium</title>
            <link>https://medium.com/@venkatashish?source=rss-ecaf8fce2f71------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 04:34:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@venkatashish/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Data Structure in C, Java and Python]]></title>
            <link>https://medium.com/@venkatashish/data-structure-in-c-java-and-python-836fd75777e7?source=rss-ecaf8fce2f71------2</link>
            <guid isPermaLink="false">https://medium.com/p/836fd75777e7</guid>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[linked-lists]]></category>
            <category><![CDATA[stack-and-queues]]></category>
            <category><![CDATA[arrays]]></category>
            <category><![CDATA[abstract-data-type]]></category>
            <dc:creator><![CDATA[Venkat Ashish]]></dc:creator>
            <pubDate>Thu, 08 Nov 2018 15:07:56 GMT</pubDate>
            <atom:updated>2018-11-08T15:07:56.756Z</atom:updated>
            <content:encoded><![CDATA[<p>So before doing anything else first let us get clear about what “<em>Data Structure</em>” actually means, because I have seen many people working on data structure and yet have no proper understanding of it(<em>including myself before writing this blog</em>).</p><blockquote>A data structure is a specialized format for organizing and storing data. Any data structure is designed to organize data to suit a specific purpose so that it can be accessed and worked with in appropriate ways. In computer programming, a data structure may be selected or designed to store data for the purpose of working on it with various algorithms. -by <a href="https://www.techtarget.com/contributor/Margaret-Rouse"><strong>Margaret Rouse</strong></a></blockquote><p>So a data structure is just a specific format to store the data which will be helpful to perform a specific task. For example, <em>Queues </em>can be used to model the online movie ticket booking. One may or may not choose to use data structures in their program, but using data structures will help to visualize the problem and will also be helpful in creating <a href="https://www.geeksforgeeks.org/abstract-data-types/"><em>Abstract Data Type</em></a> which is the best way of writing a program.</p><p>Basically there are three types of data structures which are <strong>Linear</strong>, <strong>Trees </strong>and <strong>Graphs</strong>. In this article I will be discussing only about Linear Data structures and the other two will be in other blogs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/333/1*Q30pMknOHye_9s0P7HnQGg.jpeg" /><figcaption>Hierarchy of Data Structures</figcaption></figure><h3>Linear Data Structures</h3><p>As the name suggest this is the linear format of storing the data. Some of the popular linear data structures are array, linked list, stack and queues.</p><h4>Array</h4><p>An array is a collection of items stored at contiguous memory locations. It is used to store homogeneous elements together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, which is the memory location of the first element of the array. Array is available both in C and Java. In python instead of array we have lists which is advancement of array(<em>it is heterogeneous</em>).</p><p>A small demo of array implementation is shown below:-</p><p>C implementation</p><pre>int main() <br>{ <br>int arr[5]={10,20,30,40,50}; <br>for (int i=0; i&lt;5; i++) <br> printf(&quot;Element at index %d : %d \n&quot;, i, arr[i]); <br>return 0; <br>}</pre><p>Java implementation</p><pre>class display<br>{ <br> public static void main (String[] args) <br> {</pre><pre>int[] arr; <br> arr = new int[5]; <br> arr[0] = 10; <br> arr[1] = 20; <br> arr[2] = 30; <br> arr[3] = 40; <br> arr[4] = 50; <br> for (int i = 0; i &lt; arr.length; i++) <br>  System.out.println(&quot;Element at index &quot; + i + &quot; : &quot;+ arr[i]);   <br> } <br>}</pre><h4>Linked list</h4><p>A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers. Each element famously called as <strong>node</strong> of a list is comprising of two items the data and a reference to the next node. One disadvantage of using arrays to store data is that arrays are static structures and therefore cannot be easily extended or reduced to fit the data set. Arrays are also expensive to maintain new insertions and deletions.</p><p>A small demo of inserting a node in the front is shown below:-</p><p>C implementation</p><pre>#include &lt;stdio.h&gt; <br>#include&lt;stdlib.h&gt;</pre><pre>struct node<br>{<br>    int data;<br>    struct node *next;<br>};</pre><pre>void add(struct Node** head_ref, int new_data) <br>{ <br> struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); <br> new_node-&gt;data = new_data; <br> new_node-&gt;next = (*head_ref); <br> (*head_ref) = new_node; <br>}</pre><p>Java implementation</p><pre>import java.util.LinkedList;  //class which provides ADT for linked                         <br>class LinkedListExample {<br><br>  public static void main(String[] args) {<br><br>     // create a LinkedList<br>     LinkedList&lt;String&gt; list = new LinkedList&lt;String&gt;();<br><br>     // Add elements<br>     list.add(&quot;AA&quot;);<br>     list.add(&quot;BB&quot;);<br>     list.add(&quot;CC&quot;);<br>     list.add(&quot;DD&quot;);<br> <br>     // Display List element<br>     System.out.println(&quot;LinkedList Elements:&quot;+list);<br><br>     // Adding element to front of LinkedList<br>     list.offerFirst(&quot;NEW Element&quot;);<br><br>     // Displaying List after adding element<br>     System.out.println(&quot;LinkedList After Addition:&quot;+list);<br>  }<br>}</pre><p>Python implementation</p><pre>class Node:<br><br>   def __init__(self,data,nextNode=None):<br>       self.data = data<br>       self.nextNode = nextNode</pre><pre>class LinkedList:<br><br>   def __init__(self,head = None):<br>       self.head = head<br>       self.size = 0</pre><pre>   def addNode(self,data):<br>       newNode = Node(data,self.head)<br>       self.head = newNode<br>       self.size+=1<br>       return True</pre><pre>myList = LinkedList()print(myList.addNode(5))<br>print(myList.addNode(15))<br>print(myList.addNode(25))</pre><h4>Stack</h4><p>A stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle. There are two major operations: <strong>push</strong> the item into the stack, and <strong>pop</strong> the item out of the stack. A stack is a limited access data structure elements can be added and removed from the stack only at the top. <strong>push</strong> adds an item to the top of the stack, <strong>pop</strong> removes the item from the top. Stack can be implemented either using array or linked list.</p><p>Stack can be used in compilers to check the scope of the variable by counting the brackets, to reverse a string by pushing the string into the stack and displaying it while poping the string will print in reverse order. It can also be used to evaluate prefix, postfix and infix expressions. Can be used for backtracking Suppose we are finding a path for solving maze problem. We choose a path and after following it we realize that it is wrong. Now we need to go back to the beginning of the path to start with new path. This can be done with the help of stack. And most importantly used for function call, it is used to keep information about the active functions in the program.</p><p>C implementation of stack</p><pre>#include &lt;stdio.h&gt;<br>#include &lt;stdlib.h&gt;<br>#include &lt;limits.h&gt;</pre><pre>struct StackNode<br>{<br>int data;<br>struct StackNode* next;<br>};</pre><pre>struct StackNode* newNode(int data)<br>{<br>struct StackNode* stackNode =(struct StackNode*) malloc(sizeof(struct StackNode));<br>stackNode-&gt;data = data;<br>stackNode-&gt;next = NULL;<br>return stackNode;<br>}<br></pre><pre>void push(struct StackNode** root, int data)<br>{<br>struct StackNode* stackNode = newNode(data);<br>stackNode-&gt;next = *root;<br>*root = stackNode;<br>printf (&quot;%d pushed to stack\n&quot;, data);<br>}<br></pre><pre>int pop(struct StackNode** root)<br>{<br>if (isEmpty(*root))<br>return INT_MIN;<br>struct StackNode* temp = *root;<br>*root = (*root)-&gt;next;<br>int popped = temp-&gt;data;<br>free(temp);<br>return popped;<br>}</pre><pre><strong><br></strong>int main()<br>{<br>struct StackNode* root = NULL;<br>push(&amp;root, 10);<br>push(&amp;root, 20);<br>push(&amp;root, 30);<br>printf(&quot;%d popped from stack\n&quot;, pop(&amp;root));<br>printf(&quot;Top element is %d\n&quot;, peek(root));<br>return 0;<br>}</pre><p>Java implementation of stack</p><pre>import java.io.*;<br>import java.util.*;</pre><pre>class Test<br>{<br>   // Pushing element on the top of the stack<br>   static void stack_push(Stack&lt;Integer&gt; stack)<br>   {<br>       for(int i = 0; i &lt; 5; i++)<br>        {<br>           stack.push(i);<br>        }<br>   }</pre><pre>// Popping element from the top of the stack<br>   static void stack_pop(Stack&lt;Integer&gt; stack)<br>   {<br>     System.out.println(&quot;Pop :&quot;);<br>     for(int i = 0; i &lt; 5; i++)<br>      {<br>         Integer y = (Integer) stack.pop();<br>         System.out.println(y);<br>      }<br>   }</pre><pre>public static void main (String[] args)<br>  {<br>   Stack&lt;Integer&gt; stack = new Stack&lt;Integer&gt;();<br>   stack_push(stack);<br>   stack_pop(stack);<br>   stack_push(stack);<br>  }<br>}</pre><p>Python implementation of stack</p><pre>class StackNode:<br># Constructor to initialize a node<br>    def __init__(self, data):<br>       self.data <strong>=</strong> data<br>       self.next <strong>=</strong> None</pre><pre><strong><br></strong>class Stack:<br># Constructor to initialize the root of linked list<br>     def __init__(self):<br>       self.root <strong>=</strong> None<br>  <br>     def isEmpty(self):<br>        return True if self.root is None else False</pre><pre><strong>     </strong>def push(self, data):<br>        newNode <strong>=</strong> StackNode(data)<br>        newNode.next <strong>=</strong> self.root<br>        self.root <strong>=</strong> newNode<br>        print &quot;%d pushed to stack&quot; <strong>%</strong>(data)</pre><pre><strong>     </strong>def pop(self):<br>        if  (self.isEmpty()):<br>           return float(&quot;-inf&quot;)<br>        temp <strong>=</strong> self.root<br>        self.root <strong>=</strong> self.root.next<br>        popped <strong>=</strong> temp.data<br>        return popped</pre><pre># Driver program to test above class<br>stack <strong>=</strong> Stack()<br>stack.push(10)<br>stack.push(20)<br>stack.push(30)print &quot;%d popped from stack&quot; <strong>%</strong>(stack.pop())</pre><h4>Queue</h4><p>A queue is a container of objects that are inserted and removed according to the first-in first-out (FIFO) principle. New additions to a line made to the back of the queue, while removal/serving happens in the front. In the queue only two operations are allowed enqueue and dequeue. Enqueue means to insert an item into the back of the queue, dequeue means removing the front item. Just like stack even queue can be implemented using both array and linked list.</p><p>Queue is useful in CPU/Disk Scheduling. When multiple processes require CPU at the same time, various CPU scheduling algorithms are used which are implemented using Queue data structure. In print spooling, documents are loaded into a buffer and then the printer pulls them off the buffer at its own rate. Spooling also lets you place a number of print jobs on a queue instead of waiting for each one to finish before specifying the next one. Breadth First search in a Graph .It is an algorithm for traversing or searching graph data structures. It starts at some arbitrary node of a graph and explores the neighbor nodes first, before moving to the next level neighbors.</p><p>C implementation of queue</p><pre>#include &lt;stdio.h&gt; <br>#include &lt;stdlib.h&gt; <br>#include &lt;limits.h&gt; <br>  <br>// A structure to represent a queue <br>struct Queue <br>{ <br>    int front, rear, size; <br>    unsigned capacity; <br>    int* array; <br>}; <br>  <br>// function to create a queue of given capacity.  <br>// It initializes size of queue as 0 <br>struct Queue* createQueue(unsigned capacity) <br>{ <br>    struct Queue* queue = (struct Queue*) malloc(sizeof(struct Queue)); <br>    queue-&gt;capacity = capacity; <br>    queue-&gt;front = queue-&gt;size = 0;  <br>    queue-&gt;rear = capacity - 1;  <br>    queue-&gt;array = (int*) malloc(queue-&gt;capacity * sizeof(int)); <br>    return queue; <br>} <br>  <br>// Queue is full when size becomes equal to the capacity  <br>int isFull(struct Queue* queue) <br>{  return (queue-&gt;size == queue-&gt;capacity);  } <br>  <br>// Queue is empty when size is 0 <br>int isEmpty(struct Queue* queue) <br>{  return (queue-&gt;size == 0); } <br>  <br>// Function to add an item to the queue.   <br>// It changes rear and size <br>void enqueue(struct Queue* queue, int item) <br>{ <br>    if (isFull(queue)) <br>        return; <br>    queue-&gt;rear = (queue-&gt;rear + 1)%queue-&gt;capacity; <br>    queue-&gt;array[queue-&gt;rear] = item; <br>    queue-&gt;size = queue-&gt;size + 1; <br>    printf(&quot;%d enqueued to queue\n&quot;, item); <br>} <br>  <br>// Function to remove an item from queue.  <br>// It changes front and size <br>int dequeue(struct Queue* queue) <br>{ <br>    if (isEmpty(queue)) <br>        return INT_MIN; <br>    int item = queue-&gt;array[queue-&gt;front]; <br>    queue-&gt;front = (queue-&gt;front + 1)%queue-&gt;capacity; <br>    queue-&gt;size = queue-&gt;size - 1; <br>    return item; <br>}</pre><pre>int main() <br>{ <br>    struct Queue* queue = createQueue(1000); <br>    enqueue(queue, 10); <br>    enqueue(queue, 20); <br>    enqueue(queue, 30); <br>    enqueue(queue, 40);  <br>    return 0; <br>}</pre><p>Java implementation of queue</p><pre>class Queue <br>{ <br>    int front, rear, size; <br>    int  capacity; <br>    int array[]; <br>       <br>    public Queue(int capacity) { <br>         this.capacity = capacity; <br>         front = this.size = 0;  <br>         rear = capacity - 1; <br>         array = new int[this.capacity]; <br>     } <br>       <br>    // Queue is full when size becomes equal to  <br>    // the capacity  <br>    boolean isFull(Queue queue) <br>    {  return (queue.size == queue.capacity); <br>    } <br>       <br>    // Queue is empty when size is 0 <br>    boolean isEmpty(Queue queue) <br>    {  return (queue.size == 0); } <br>       <br>    // Method to add an item to the queue.  <br>    // It changes rear and size <br>    void enqueue( int item) <br>    { <br>        if (isFull(this)) <br>            return; <br>        this.rear = (this.rear + 1)%this.capacity; <br>        this.array[this.rear] = item; <br>        this.size = this.size + 1; <br>        System.out.println(item+ &quot; enqueued to queue&quot;); <br>    } <br>       <br>    // Method to remove an item from queue.   <br>    // It changes front and size <br>    int dequeue() <br>    { <br>        if (isEmpty(this)) <br>            return Integer.MIN_VALUE; <br>           <br>        int item = this.array[this.front]; <br>        this.front = (this.front + 1)%this.capacity; <br>        this.size = this.size - 1; <br>        return item; <br>    } <br>}</pre><pre>public class Test <br>{ <br>    public static void main(String[] args)  <br>    { <br>        Queue queue = new Queue(1000); <br>        queue.enqueue(10); <br>        queue.enqueue(20); <br>        queue.enqueue(30); <br>        queue.enqueue(40); <br>    } <br>}</pre><p>Python implementation of queue</p><pre># Class Queue to represent a queue <br>class Queue: <br>  <br>    # __init__ function <br>    def __init__(self, capacity): <br>        self.front = self.size = 0<br>        self.rear = capacity -1<br>        self.Q = [None]*capacity <br>        self.capacity = capacity <br>      <br>    # Queue is full when size becomes <br>    # equal to the capacity  <br>    def isFull(self): <br>        return self.size == self.capacity <br>      <br>    # Queue is empty when size is 0 <br>    def isEmpty(self): <br>        return self.size == 0<br>  <br>    # Function to add an item to the queue.  <br>    # It changes rear and size <br>    def EnQueue(self, item): <br>        if self.isFull(): <br>            print(&quot;Full&quot;) <br>            return<br>        self.rear = (self.rear + 1) % (self.capacity) <br>        self.Q[self.rear] = item <br>        self.size = self.size + 1<br>        print(&quot;%s enqueued to queue&quot;  %str(item)) <br>  <br>    # Function to remove an item from queue.  <br>    # It changes front and size <br>    def DeQueue(self): <br>        if self.isEmpty(): <br>            print(&quot;Empty&quot;) <br>            return<br>          <br>        print(&quot;%s dequeued from queue&quot; %str(self.Q[self.front])) <br>        self.front = (self.front + 1) % (self.capacity) <br>        self.size = self.size -1<br> <br>  <br> <br>if __name__ == &#39;__main__&#39;: <br>  <br>    queue = Queue(30) <br>    queue.EnQueue(10) <br>    queue.EnQueue(20) <br>    queue.EnQueue(30) <br>    queue.EnQueue(40) <br>    queue.DeQueue() <br>    queue.DeQueue()</pre><p>Uhhh! Ok, so we saw linear data structure and their implementation in C, Java and Python. In the next post I will try to cover Tree data structures in the similar fashion.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=836fd75777e7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>