Skip to content
This repository has been archived by the owner on Apr 1, 2021. It is now read-only.

Create Articles : Data structures #1044

Open
4 of 23 tasks
ds-249 opened this issue May 27, 2016 · 20 comments
Open
4 of 23 tasks

Create Articles : Data structures #1044

ds-249 opened this issue May 27, 2016 · 20 comments

Comments

@ds-249
Copy link
Contributor

ds-249 commented May 27, 2016

Here is a rough outline of simple data structure articles. They are language agnostic, but it's better to provide some example implementation of functionalities in Java or Python 3.

It should be made abundantly clear to a reader what is the point in structuring our data to control how we access it.

It is to be made clear to the reader with some applications of these data structures and how fundamental they are to the software and tool we use. It is also to be emphasized upon that while it's good to know their implementation, it's probably better to use libraries when using them in production.

The language libraries in Java and Python 3 need to be mentioned, which exposes these APIs to use these data structures.
 




Need Help? Read CONTRIBUTING Guidelines
or Chat with us in FreeCodeCamp/Wiki


@ds-249 ds-249 changed the title How about we write articles about different Data Structures as well? Create Articles : Data structures May 27, 2016
@alayek
Copy link
Member

alayek commented May 27, 2016

Notwithstanding the fact that this would be a massive undertaking in itself, we have to use some popular object oriented language for the code examples.

Our Wiki articles, at present, are mostly on JavaScript. I don't know of many material out there that would discuss even basic Stacks or Queues in JS.

We are, however, planning to add languages like Python or Java in our curriculum, and it is expected there would be some basic data structure problems in that. Which means we would also need corresponding articles in our Wiki on some basic data structures.

FreecodeCamp is about getting a candidate job ready, or improve on their current technical skills to move to a more technical role within their current day job. There are very few jobs that require complex Data Structure knowledge on a day-to-day basis. So it was never a priority for us.

However, interviews are a step to get a job, and the tech interviews tend to ask some tricky data structure problems - doubly linked queues, linked lists, trie etc.

It would massively help if you would like to take up this initiative and get the ball rolling. You can start writing in any standard language of your choice - Java or Python. Since we don't have any current plans for C or C++ in-browser challenges, we would rather not have articles in those.

@ds-249
Copy link
Contributor Author

ds-249 commented May 27, 2016

I code in python2, if that's okay, I can start right up. :)

@alayek
Copy link
Member

alayek commented May 27, 2016

@ds-249 that's cool! We are currently planning to support only Python 3 for now, so you raise the PRs in Python 2, and we would recommend changes and tweaks for Python 3.

You can discuss this more with us in our chatroom

@abhisekp
Copy link
Member

abhisekp commented May 27, 2016

This might be helpful for this initiative 👉 RosettaCode:Programming Tasks which shows a single algorithm in all major and minor languages. 😄

Might interest someone for learning Khan Academy — Algorithms

@alayek
Copy link
Member

alayek commented May 27, 2016

Udacity has recently launched a course titled "Technical Interviews" which covers some of these topics in Python.

@alayek alayek added this to the Create Article milestone May 27, 2016
@alayek alayek self-assigned this May 27, 2016
@varunu28
Copy link
Contributor

@alayek I code in Python 3 and would like to get started on this. We can also add up few algorithms which get frequently asked in interviews.

@alayek
Copy link
Member

alayek commented May 27, 2016

@varunu28 for algorithms, I am under the impression you are talking about the sorting ones, median finding, string matching etc. (I am not including the ones that would be associated with a DS). Is that the case?

We have some Algorithm articles, but those are more for our internal in-browser challenges, and less for what an Algo course in college would cover. Sure, we could absolutely do that!

I think we should keep that as a separate epic though.

@varunu28
Copy link
Contributor

@alayek Yeah I am talking about those and also like the ones for which you mentioned Udacity course.
Still we can keep this for future. I would get started on this.

@alayek
Copy link
Member

alayek commented May 27, 2016

@varunu28 please join our chatroom to discuss this in detail.

@varunu28
Copy link
Contributor

@alayek @ds-249
Can we have a basic mock-up design of what the articles need to cover and what should be the format for it?

@ds-249
Copy link
Contributor Author

ds-249 commented May 27, 2016

@varunu28 I am following something like this

Hash Tables and Hashing Functions

Introduction to hashing

Hashing is designed to solve the problem of needing to efficiently find or store an item in a collection.
For example, if we have a list of 10,000 words of English and we want to check if a given word is in the list, it would be inefficient to successively compare the word with all 10,000 items until we find a match.
Hashing is a technique to make things more efficient by effectively narrowing down the search at the outset.

What is hashing?

TODO

How hashing works

TODO

How to use hashing in your code

TODO

@varunu28
Copy link
Contributor

@ds-249 What exactly are we looking to put in "What is" and "How" part?
Will it be just the theory or also the implementation?

@ds-249
Copy link
Contributor Author

ds-249 commented May 27, 2016

@varunu28 I'll add another heading for Implementation in programs, where I'll add code snippets of python and java in it.

@varunu28
Copy link
Contributor

ok. Do we need to add in both in Python and Java?

@varunu28
Copy link
Contributor

I am starting with Arrays for this

@alayek
Copy link
Member

alayek commented May 27, 2016

@varunu28 ideally we would want a separate implementation section, and subsection with Python and another subsection with Java.

@Rafase282
Copy link
Member

Adding a label to categorize these will be helpful @alayek particularly for when we move all articles into sub directories for organization in the near future hopefully.

@jainaman224
Copy link
Contributor

jainaman224 commented May 27, 2016

Linked list

A linked list is visually more like a garland. We call every flower-y stuff on this particular garland to be a node. And each of the node points to the next node in this list as well as it has data (here it is type of flower).

Types:

  1. Singly Linked List

    Singly linked lists contain nodes which have a data field as well as a next field, which points to the next node in the sequence. Operations that can be performed on singly linked lists are insertion, deletion and traversal.

     Singly Link List
    --------------
    
        head
         |
         |
       +-----+--+      +-----+--+      +-----+------+
       |  1  |o----->  |  2  |o----->  |  3  | NULL |
       +-----+--+      +-----+--+      +-----+------+
    
    
  2. Doubly Linked List

    Doubly linked lists contain node which have data field, next field and another link field prev pointing to the previous node in the sequence.

    Doubly Linked List
    ----------------
    
               head
                |
                |
       +------+-----+--+    +--+-----+--+       +-----+------+
       |      |     |o------>  |     |o------>  |     |      |
       | NULL |  1  |          |  2  |          |  3  | NULL |
       |      |     |  <------o|     |  <------o|     |      |
       +------+-----+--+    +--+-----+--+       +-----+------+
    
    
  3. Circular Linked List

    Circular linked lists is a singly linked list in which last node, next field points to first node in the sequence.

    Circular Linked List
     ------------------
    
          head
           |
           |
         +-----+--+      +-----+--+      +-----+--+
    -->  |  1  |o----->  |  2  |o----->  |  3  |o----
    |    +-----+--+      +-----+--+      +-----+--+  |
    |                                                |
    ------------------------------------------------
    

Basic Operations

  1. Insertion

    To add a new element to the list.

    Insertion at the beginning
    ------------------------
    
    * Create a new node with given data.
    * Point new node's `next` to old `head`.
    * Point `head` to this new node.
    
    Insertion in the middle/end
    --------------------------
    Insertion after node X.
    
    * Create a new node with given data.
    * Point new node's `next` to old X's `next`.
    * Point X's `next` to this new node.
    
  2. Deletion

    To delete existing element from the list.

    Deletion at the beginning
    -----------------------
    
    * Get the node pointed by `head` as Temp.
    * Point `head` to Temp's `next`.
    * Free memory used by Temp node.
    
    Deletion in the middle/end
    -------------------------
    Deletion after node X.
    
    * Get the node pointed by `X` as Temp.
    * Point X's `next` to Temp's `next`.
    * Free memory used by Temp node.
    
  3. Traversing

    To travel acroos the list.

    Traversal
    --------
    
    * Get the node pointed by `head` as Current.
    * Check if Current is not null and display it.
    * Point Current to Current's `next` and move to above step.
    

Implementation:

  • Python Implementation of Singly Linked List
class Node(object):
    # Constructor
    def __init__(self, data=None, next=None):
        self.data = data
        self.next = next

    # Function to get data
    def get_data(self):
        return self.data

    # Function to get next node
    def get_next(self):
        return self.next

    # Function to set next field
    def set_next(self, new_next):
        self.next = new_next


class LinkedList(object):
    def __init__(self, head=None):
        self.head = head

    # Function to insert data
    def insert(self, data):
        # new_node is a object of class Node
        new_node = Node(data)
        new_node.set_next(self.head)
        self.head = new_node
        print("Node with data " + str(data) + " is created succesfully")

    # Function to get size
    def size(self):
        current = self.head
        count = 0
        while current:
            count += 1
            current = current.get_next()
        print("Size of link list is " + str(count))

    # Function to search a data
    def search(self, data):
        current = self.head
        found = False
        while current and found is False:
            if current.get_data() == data:
                found = True
            else:
                current = current.get_next()
        if current is None:
            print("Node with data " + str(data) + " is not present")
        else:
            print("Node with data " + str(data) + " is found")


    # Function to delete a node with data
    def delete(self, data):
        current = self.head
        previous = None
        found = False
        while current and found is False:
            if current.get_data() == data:
                found = True
            else:
                previous = current
                current = current.get_next()
        if current is None:
            print("Node with data " + str(data) + " is not in list")
        elif previous is None:
            self.head = current.get_next()
            print("Node with data " + str(data) + " is deleted successfully")
        else:
            previous.set_next(current.get_next())
            print("Node with data " + str(data) + " is deleted successfully")

SLL = LinkedList() # Creates an object of class LinkedList
SLL.size() # prints 'Size of link list is 0'
data_elements = [5, 10, 2, 6, 8, 20]
for each in data_elements:
    SLL.insert(each)
SLL.size() # prints 'Size of link list is 6'
SLL.search(4) # prints 'Node with data 4 is not present'
SLL.search(5) # prints 'Node with data 5 is found'
SLL.delete(4) # prints 'Node with data 4 is not in list'
SLL.delete(5) # prints 'Node with data 5 is deleted successfully'

🚀 Run Code

  • C implementation of singly linked list
    // Header files
    #include <stdio.h>
    #include <stdlib.h>

    struct node
    {
        int data;
        struct node *next;
    };

    // Head pointer always points to first element of the linked list
    struct node *head = NULL;

    // Display the list
    void printList()
    {
        struct node *ptr = head;

        // Start from the beginning
        while(ptr != NULL)
        {
            printf("%d ",ptr->data);
            ptr = ptr->next;
        }

        printf("\n");
    }

    // Insert link at the beginning
    void insertFirst(int data)
    {
        // Create a new node
        struct node *new_node = (struct node*)malloc(sizeof(struct node));

        new_node->data = data;

        // Point it to old head
        new_node->next = head;

        // Point head to new node
        head = new_node;

        printf("Inserted successfully\n");
    }

    // Delete first item
    void deleteFirst()
    {
        // Save reference to head
        struct node *temp = head;

        // Point head to head's next
        head = head->next;

        // Free memory used by temp
        free(temp);

        printf("Deleted successfully\n");
    }

    // Find no. of nodes in link list
    void size()
    {
        int length = 0;
        struct node *current;

        for(current = head; current != NULL; current = current->next)
        {
            length++;
        }

        printf("Size of Linked List is %d\n", length);
    }

    // Find node with given data
    void find(int data){

        // Start from the head
        struct node* current = head;

        // If list is empty
        if(head == NULL)
        {
            printf("List is empty\n");
            return;
        }

        // Traverse through list
        while(current->data != data){

            // If it is last node
            if(current->next == NULL){
                printf("Not Found\n");
                return;
            }
            else{
                // Go to next node
                current = current->next;
            }
        }

        // If data found
        printf("Found\n");
    }

    // Delete a node with given data
    void delete(int data){

        // Start from the first node
        struct node* current = head;
        struct node* previous = NULL;

        // If list is empty
        if(head == NULL){
            printf("List is empty\n");
            return ;
        }

        // Navigate through list
        while(current->data != data){

            // If it is last node
            if(current->next == NULL){
                printf("Element not found\n");
                return ;
            }
            else {
                // Store reference to current node
                previous = current;
                // Move to next node
                current = current->next;
            }

        }

        // Found a match, update the node
        if(current == head) {
            // Change head to point to next node
            head = head->next;
        }
        else {
            // Skip the current node
            previous->next = current->next;
        }

        // Free space used by deleted node
        free(current);
        printf("Deleted succesfully\n");
    }

    int main() {
        insertFirst(10); // prints Inserted successfully
        insertFirst(20); // prints Inserted successfully
        insertFirst(30); // prints Inserted successfully
        insertFirst(1); // prints Inserted successfully
        insertFirst(40); // prints Inserted successfully
        insertFirst(56); // prints Inserted successfully

        // print list
        printList(); // prints 56 40 1 30 20 10

        deleteFirst(); // prints Deleted successfully

        printList(); // prints 40 1 30 20 10

        find(4); // prints Not Found
        find(1); // prints Found

        delete(4); // prints Element not found
        delete(1); // prints Deleted succesfully

        printList(); // prints 40 30 20 10

        return 0;
    }

🚀 Run Code

Advantages

  1. Linked lists are a dynamic data structure, which can grow and shrink, allocating and deallocating memory while the program is running.
  2. Insertion and deletion of node are easily implemented in a linked list at any position.

Disadvantages

  1. They use more memory than arrays because of the memory used by their pointers (next and prev).
  2. Random access is not possible in linked list. We have to access nodes sequentially.

@varunu28
Copy link
Contributor

@alayek I want to get started with stacks. Please assign that to me.

@raydog99
Copy link

Please assign Trees - Binary Search Trees to me.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants