Thursday, October 29, 2015

Data Structures - Linked Lists Part 2 (and Queues)


This is a second part to my first post about Arrays and Linked lists. Here I have (finally) implemented and tested two separate queues. Notice that you probably wouldn't need a queue structure if you are utilizing only one queue. The queue structure is so that I can use several different queues. Lots and lots of comments to guide you through:

#include <stdio .h>
#include <stdlib .h>

typedef struct mynode{ //this is the node structure, which includes a data int
  int data;            //and a pointer to the next node
 struct mynode* next;
}node;

typedef struct{ //this is the queue structure, which includes a head and tail pointer.
  node* head;   //a head and a tail pointer points to the first and last nodes of a queue
  node* tail;
}queue;

void enqueue(queue* que, int new_data); //here a list of the function prototypes
void dequeue(queue* que);
int take_queue_data(queue* que);
void print(queue* que);

int main()  //main function to test out
{
    //test cases hereeee
    queue* testq1 = (queue*)malloc(sizeof(queue));
    queue* testq2 = (queue*)malloc(sizeof(queue));
    enqueue(testq1, 1); print(testq1);
    enqueue(testq2, 2); print(testq2);
    enqueue(testq1, 3); print(testq1);
    enqueue(testq2, 4); print(testq2);
    enqueue(testq1, 5); print(testq1);
    enqueue(testq2, 6); print(testq2);
    dequeue(testq1); print(testq1);
    dequeue(testq2); print(testq2);
    dequeue(testq1); print(testq1);
    dequeue(testq2); print(testq2);
    dequeue(testq1); print(testq1);
    dequeue(testq2); print(testq2);
}

void enqueue(queue* que, int new_data){
  node* tmp = (node*)malloc(sizeof(node)); //make a temporary node
  tmp->data = new_data; //set the node->data to what we want to insert   
  tmp->next = NULL; //the next of that node would just be the nothing.

  if (que->head == NULL && que->tail == NULL){// if queue is empty
    que->head = tmp;// here we set the head and tail to this one node
    que->tail = tmp;// because this is the first node ever
  }
  else{ //if queue isnt empty, then just set it as the new tail.
    (que->tail)->next = tmp; 
    que->tail = tmp;// and then remember to link this node to the last one's "next" pointer
  }
  return;
}//end of enqueue

void dequeue(queue* que){
    node* tmp = que->head; //temporary for head of queue
  if (que->head == NULL){ //in case queue is empty
  printf("Queue is empty! \n");
    return;
  }
  else if(que->head == que->tail){
    que->head = NULL; //delete that last element
    que->tail = NULL; //and everything back to NULL
  }
  else{//there is more than one element in queue
      que->head = (que->head)->next;
  }
  free(tmp); //we cant hold on to this mem space forever, or cannnnn we. :(
}// end of dequeue

int take_queue_data(queue* que){//this will give us the first data in queue
    if(que->head == NULL){
     printf("Queue is empty, nothing to look at.");
     return 0;
    }
    else{
     return (que->head)->data;   
    }
}

void print(queue* que){//this is to look at whole queue
    node* tmp = que->head; //set tmp to the front of the queue to walk
    while(tmp != NULL){
        printf("%d ", tmp->data);
        tmp = tmp->next;
    }    
    printf("\n");
}

I hope this helps you understand a linked list better(or how to implement it). Remember there is a doubly linked list too, and it's simple to implement with a alittle tweaking of this code. :D
If you see any problems, have any suggestions, or have any questions feel free to ask in the comments below!

Monday, October 12, 2015

Data Structures - Arrays and Linked Lists

An array is a random access data structure, while a linked list is a sequential access data structure. So, what's the difference between random and sequential?

Well, random means you can access something directly, and sequential means in order to access something, you have to go one by one. See this:

Arrays:

Linked lists:

or

The differences between the two different linked lists, we'll talk about later. But the difference between arrays and linked lists, we will talk about :D

Okay, the difference: Arrays, can be access directly, like some array A and some index -> A[0].
In the above example of an array, A[0] would = 10, A[1] = 6,  A[2] = 7, and so on. The way to access these indices is very direct, like opening a book to a specific page, assuming that there's bookmarks to every page...

Linked lists on the other hand, are linked someway, the first one in only one direction. For the first linked list, it is analogous to......to.......... walking into a huge cave and every step you get a boulder behind you, you can't go back, sadly.

The second one is linked bidirectionally, which means you can go back and forth between elements, like a string of rooms each with a door. 

Yup, that's all.

I'll add more to this if I find that I have left anything out, and I hope this explains to you arrays and linked lists!

Here's to part 2: Implementation



Wednesday, October 7, 2015

Java 101 - 02 Comments, Objects, and Classes


Summary of last post(link here): We looked over the basic introduction of Java and some start on the syntax and specifics how Java should work. Here's the code that we ended up with to refresh your mind:
//This is the Myclass class naming
class MyClass{
//This is the main method we always need
public static void main(String []args){

}
//This is some random method we made, notice the capitalization rules.
public void thisMethodNotAllLowercase(){

}
}


In this post will go over commenting(which you've seen some from last post), Objects, and how classes are related to those Objects.

This is going to be a pretty simple a straight forward post about commenting in the Java programming language. Commenting in any language is useful if a few ways:

  1. If you are working on a crazy long project, comments will help you skim through what your lines are doing and refresh your mind quicker. That is both a time saver as well as a big life saver.
  2. If you are working with other team members and they need to know what you are doing, they can skim through your lines without sitting there forever trying to firgure out. That will save you and your team time, as well as get some work doneeeee.
  3. This sort of piggy backs with the first two, but if you decide to come back to it after a break then you won't have to worry about forgetting, it's all there!
Alright then, to the hows:
  • To comment just on one line, simply type "//" without the quotes.
  • To comment several rows simply start with "/*" and end with "*/" , also without the quotes.

//This is one line comment

/*
This
is
several
line
comment!
*/

/*
* This
* Just
* looks
* Prettier
*/

So, this basically ends it. Pretty simple eh? Now you can comment you can try creating your own code without having to remember everything by mind. Hope you learn something from this post and I'll try to post more soon.

*If you find a typo or error let me know! I'm human and I make silly mistakes too.