-
-
Notifications
You must be signed in to change notification settings - Fork 305
Create Articles : Data structures #1044
Comments
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. |
I code in python2, if that's okay, I can start right up. :) |
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 |
Udacity has recently launched a course titled "Technical Interviews" which covers some of these topics in Python. |
@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. |
@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. |
@alayek Yeah I am talking about those and also like the ones for which you mentioned Udacity course. |
@varunu28 I am following something like this Hash Tables and Hashing FunctionsIntroduction to hashingHashing is designed to solve the problem of needing to efficiently find or store an item in a collection. What is hashing?TODO How hashing worksTODO How to use hashing in your codeTODO |
@ds-249 What exactly are we looking to put in "What is" and "How" part? |
@varunu28 I'll add another heading for Implementation in programs, where I'll add code snippets of python and java in it. |
ok. Do we need to add in both in Python and Java? |
I am starting with Arrays for this |
@varunu28 ideally we would want a separate implementation section, and subsection with Python and another subsection with Java. |
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. |
Linked listA 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:
Basic Operations
Implementation:
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
// 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
Disadvantages
|
@alayek I want to get started with stacks. Please assign that to me. |
Please assign Trees - Binary Search Trees to me. |
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.
The text was updated successfully, but these errors were encountered: