Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

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!

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.

Monday, June 22, 2015

Starting on OOP(Object Oriented Programming) through Java




Hi readers,

I've been wondering if I should throw something out there about getting started with java, since many self taught programmers learn java some time in their life, or not. I teach an introductory class in college, well I guess I instruct. In that class students are introduced to the Java programming language and they learn Object Oriented Programming concepts.

So, in this post, I'd like to write something about getting some tools ready and set to learn the language and concepts that it teaches.


1. Set Up an IDE or Such
Setting up an IDE is important if you want to check or compile your code. There are different IDEs available for Java just like there are several for other programming languages. To see different types of them see here.

2. Use Online Resources
I give my students TutorialsPoint references very often. They give information by variable types; Strings, arrays, etc. If sometimes you don't see the information you want, using Google to search the site and the topic usually will bring up a TutorialsPoint link for it.


Java Oracle Documentation: Many of my students don't like using this site since it's not always the simplest explained. Nevertheless, some still find this page useful for finding what they need.








Stackoverflow: Yes yes, it doesn't give documentation like oracle or tutorialspoint, but it does give you insight to similar problems and similar solutions once you get started. Stackoverflow is also useful for other languages as well.

3. Tutorials
Oracle also gives tutorials here, although the effectiveness of the tutorials are up to you. There are many Youtube videos that give great amounts of info about java, going through things step by step and talking slower so that you can process concepts more properly. Derek Banas give really fast tutorials but if you slow it down or you pause to think through it, it will work out well. His tutorials are more like reviews sometimes so you possibly want to use it as a review when you think you've got the jist of Java.

Through this mini guide I hope that you find some useful resources on where to look to learn Java! :D
I also hope to put up a few more posts/tutorials for Java and possibly videos to go with it, I look forward to helping anyone with the language through my tutorials.

Tuesday, June 16, 2015

Java 101 - 01 What is Java and What is it Made of?

 Whether you're a computer Science major or just someone thinking of picking up a pastime skill, this post will explain what Java is and also give some beginning introduction to it. :)


An Introduction to Java's birth and life:
Java was born on June 1991. Created by James Gosling, Mike Sheridan, and Patrick Naughto. Apparently I learned through some research that Java was created for the purpose of television improvement but it was too advanced to be used during that time. It was originally named Oak after some oak tree. Later, the name changed to green during their green project and then changed to Java after the coffee. Something I know about Java is that things are being added and have been added since the creation of it and everything accumulated from all these years is still in the language. Reason of this that there were five established goals at the time they created it.

1. It must be "simple, object-oriented and familiar"
2. It must be "robust and secure".
3. It must be "architecture-neutral and portable".
4. It must execute with "high performance".
5. It must be "interpreted, threaded, and dynamic".

With these rules the now commonly used language was birthed. If you like to know more about the creator or the language, visit the wiki or webpage I have linked to each of the creators.

How this little tutorial is going to work:
In this tutorial (or any future tuts) I will try to go through the talking step by step, where each tutorial will go along the development of a small program. Each step will include little snippets of code, where at the end I will make a code combination. The final code combination allows you to see what you have done after the whole tutorial, as well as see what you have learned. These tutorials, hopefully, can be easily followed and the code snippets can be copied into your own compiler for you to learn.

Suggestion: Use a text editor or a simple compiler program to help you with the code. In our University Java class we use Dr. Java. Usually the jar file is easier to use. It's also portable so you can stick in on a flash drive.

Straight to the basics:
Now to the fun. We will get started with the basic syntax since all background information is provided in the section above.

Syntax:
Java code is case sensitive, so beware your capitalization or your keyboard's auto-capitalization features. Many times my students go nuts over their code error forever to only find that it was one letter. ONE LETTER!!!!

Naming Conventions:
Naming conventions are just the rules of how to keep organized names of different things. Class names should have the first letter capitalized,with each word's first letter also capitalized.
class MyClass{
}

Method Names:
Method names should have the first letter lowercase,with other words' first letter capitalized.
 public void thisMethodNotAllLowercase(){

}

The Main Method:
The main method is a mandatory part of a java program; it is used to tell the Java program where to go and what to do.
public static void main(String []args){

}

Program File Naming:
The name of your program file should always be named the same as your class name. Going with the class name example we put above, our program file would be named MyClass.java


Keywords:
Keywords are special words that are used to "command" the program. Words like class, public, or static(like those used in example above). So, don't use these keywords as names unless you want your compiler to yell at you. And you don't want your compiler to yell at you.

Summary:
We learned about how to name things in our Java program in this tutorial; don't worry I will explain what class method and all that jazz is in a later tutorial! But, here is the end code if you want to try it anyways.
//this is a comment. We'll talk about it later don't worry.
//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(){

}
}


If I made any errors please point out to me with comments! I'm human, despite what my nephew says; Unfortunately I make mistakes too.