OzZy's Blog

July 18, 2010

Multi-directional Linked Lists and More Templates

Filed under: OOP344,Seneca — by ozzym @ 1:43 pm

This week we learned about different types of linked lists.  We can create a linked list with pointers going not only forward, but backwards as well.  This makes searching faster.

We also learned about Templates.  These are my notes for the “templates” section of that lecture:

Templates
A way to write fill in the blanks code, and the blanks are the data types.

Code is often repeated (copy, past, and substitute) because of differences in data type of the parameters.
Templates allow us to write generic code where data type is filled in automatically at compile time.

***NOTE***
need to overload the insertion operator to display your struct/class if you are using a struct/class for a template.

if you need to write a function to print an array and a double you would need to write 2 functions where the only difference is a few keywords.

we first declare a template class:

#include
using namespace std;

template  //T is like  a blank … When the compiler sees T it
//knows that T needs to be filled in

// you need to modify your function
void printarray(T arr[],int sz){
for(int i=0;i
cout << arr[i] << endl;
}
}

int main(void){
int arr1[5]={5,3,2,1,6};
double arr2[5]={3.3,1.2, 3.14,6.0,7.8};
char arr3[5][10]={“apples”,”oranges”,”banana”,”pears”,”kiwi”};
}

***NOTE***
You cannot use templates in C, only C++

You can also use Templates for Classes.
There are some rules for Template use in Classes.
•    If you you’re doing templates, they go in headers.
tlist.h has the code for templates.

If you need multiple templates, you have to declare it as such:
template

you also have to change the declaration a bit:

if your class or struct uses a Template, the compiler doesn’t know the size of the object so it cannot make it  so it doesn’t see it.
so when you declare one, you have to declare it as such:

Node* x;
You have to use the angle brackets.

Instead of primitive data types, you can use classes in those angle brackets, but you need to make sure that the operations being used are supported for that class. (copy ctor, overloaded operators, etc)

Another thing to not with templates is that with the given code:
template
struct Node{
TYPE data_;
Node* next_;
};

that template declaration ONLY applies to the struct underneath it.
A Template declaration ONLY applies the NEXT code block ONLY.

SO, each function, class, struct that uses templates, needs to have those declarations before it.

So the class needs to have a template declaration.
The function prototypes in the class header needs to have its data types replaced with the template name.

July 11, 2010

Stacks

Filed under: OOP344,Seneca — by ozzym @ 3:56 pm

This week we also learned about stacks.  Stacks are list type structure that have data stores in a First In Last Out (FILO) manner.  So to put it visually, if you have a stack of books, you would always put the book on top of the stack to add to it.  If you wanted to remove data from a stack it would also be from the top and only from the top.  It is not possible to remove something not from the top of the stack.  You usually can only see what is happening at the top of the stack and not beneath it.  A stack can be implemented in various ways including linked lists.

Linked Lists

Filed under: OOP344,Seneca — by ozzym @ 3:41 pm

This week, we learned about linked lists.  The linked lists were very confusing at first, but I sort of got them in the end.  They are a nice way to organize data without having to worry to much about memory reallocation; like you have to with C-style null terminated strings.

As with a lot of things, they have their pros and cons.  Linked lists are not stored in contiguous memory, so it is easier to change and re-change the same of the list as needed.  But this comes at the prices of having slower index times.  Unlike C-style null terminated strings, where you have a simple index number to go by, linked lists have a starting point; this starting point has a pointer to the first set of data, or node.  These nodes all contain data and a pointer to the next node in the series.  So to get to any given set of data in the list, you would have to go through all of the nodes prior to it.

A linked list is not a standard item in the C/C++ library, it is concept.  So there is not a specific library or keyword to create one.  They are just classes or structs.  You need to have two different classes for a linked list.  The starting point class, and a node class.   The starting point class not only holds the location (pointer) to the first node item in the list, it also holds all the operations that can be done with the lists through member functions, operators, con/destructors, etc.  The node class is simply holds data and a pointer to the next node.  To signify the end of a linked list, the last node would have a NULL value for the pointer to the next node.  Also, if a linked list is empty, the pointer to the first node would also have a NULL value.  One last important item of note, is that when adding nods to a linked list the new node is pushed to the front of the list; between the starting point and the previously newest node.

June 13, 2010

Unions

Filed under: Uncategorized — by ozzym @ 12:56 pm
Tags:

We learned about Unions this week.
I did not know they existed, but it makes sense.
I do not think we will use them though.
They work the same way as structs, but they take the size of the largest data member, this is because it overwrites the memory with new data.

May 29, 2010

3rd Week

Filed under: OOP344,Seneca — by ozzym @ 12:13 am

Had a class over IRC this week.  It didn’t see very successful, but that is probably because that was a lot of people’s first time using IRC.  We didn’t get very much course content done over the time spent, however I did learn various ways to optimizing code so that the program runs faster.  So even though the quantity of what we learned was small, the quality of it was actually really high!  We also went over how the C++ language handles evaluation with && and ||.  I already knew how a C++ compiler would skip the rest of an If statement if the first part of an && expression was false (I love you Visual Studio Debugger!).  This way of optimization did not seem to matter in the mickey mouse programs that I have been creating in school so far, but it is a good idea to make a habit of programming that way.  We also learned Pointer Arithmetic and Pointer Notation.

I am anxious to jump right into the project, it seems that  a lot of classes this semester are taking it REAL slow at the beginning.  Maybe it is because second semester is sooo fast paced?  Maybe it is because it is summer?  All I know is that everyone is probably going to end up back loading there courses :(   Which is especially bad for summer semester because a lot of students, like me, have a full course squished into the last half of the semester!

On a side note, I had some REALLY interesting conversations with tdot and CloudScorpion before and after the class!   They explained some stuff about .Net technologies, cleared up some common misconceptions about PHP, and really got me thinking about trying to learn C# instead of JAVA before the next semester. tdot explained to be one of the subtle differences of C# over C++.

Everything (how he described it to me) in C# is handled as a pointer, but isn’t referred to as such.  For example: In C++ if you just use a class’ name when passing it to a function, it passes by value.

FunctionOne(Student X);

To pass by reference, you have to use the & operator.  As we all SHOULD have learned in OOP244, it is advantageous to pass by reference because passing by value (I hope I’m getting the terminology right! Don’t have OOP244 notes on this computer!) copies the entire object when moving into the function.  And as we know, an object could be VERY big in size.

FunctionTwo(Student& X);

Anyways in C# if you put just the class’ name in a function call, it automatically passes by reference; the & operator does not have to be inputted.   Another neat little quirk is that accessing instance variables and function members still uses the . operator, not the -> operator.  Even though you are working with pointers!  This is awesome, because Chris threw a lot of new operators at us last semester, and quite often he gave us multiple ways of writing them >_<  This is what tdot told me.  Maybe he was lying about the whole thing!!!!

I also found out that CloudScorpion gets REALLY lonely/bored when he’s supposed to be working! :)   And is a noob because he thinks: (Sonic Adventure 2 > Sonic Adventure 1) !  CLEARLY he was trolling!!

May 23, 2010

First Meeting

Filed under: Uncategorized — by ozzym @ 11:26 pm
Tags:

The HOTYS just had their first meeting. The logs are up. I just realized that I HATE the wikicode that wikis use. I knew EXACTLY what I wanted to do in xHTML/CSS, but wiki was DETERMINED to make it impossible to do.

We have come up with coding style set that we are all comfortable with.
Now we wait for the course work….

May 20, 2010

2nd Week

Filed under: Uncategorized — by ozzym @ 4:54 pm
Tags:

It seems that no matter how old you are, making teams is ALWAYS excruciatingly painful. Now that the first round of making teams are done, I decided to get straight to work! Although we cannot do much (Got class next period), our team is going to have our first meeting this Sunday on IRC. Not much to talk about yet, but I thought that we should get some ground rules down first for styles and whatnot.

I’ve done many group oriented assignments and projects before, as has everyone, so I wanted to minimize any many possible cracks and bumps now to attain the highest cohesion between our members. We seem to have met consensus on the time, so we are a go!

May 13, 2010

Test 1

Filed under: Uncategorized — by ozzym @ 4:57 pm

This is my first time using a blog, and am testing it out.

Hello world!

Filed under: Uncategorized — by ozzym @ 1:40 pm

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!

Theme: Toni. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.