Showing posts with label pointers. Show all posts
Showing posts with label pointers. Show all posts

Wednesday, April 22, 2009

Non-Linear Data Structures :: Binary Tree

The first non-linear data structure and most important to understand is the binary search tree. This structure consists of a main node which is called the root and sub nodes which are called children. Each parent node has children nodes starting at the root and going to the bottom of the tree. Binary Search trees work by having a left child and a right child pointer. This does not mean that every parent node will have 2 children. The number of children depend on the values of each node that are inserted into the tree. The first value that is inserted into the tree is the root. From the root each value inserted must be checked against it; if the value is greater than the root then it would become the right child if it were less than the root it would become the left child. This goes on through the entire tree. NO left child will ever be greater than or equal to its parent or sibling.

The following tree was created by inserting these values in order. (10,15,13,24,4,7,9,5,2)

Tuesday, April 21, 2009

Linear Data Structures :: Creating Queues

The concept of a queue is what every small child learns when they go to school. Wait your turn. The queue is used to create a FIFO list also known as first in first out. A queue is similar to a stack in that the top of the stack is like the front of the queue. However, the stack is created using Push at the Top while the queue is created by inserting the new node at the back of the queue.

STACK
The following image is an example of a stack. It shows the TOP of the stack equal to 3. The image demonstrates how the stack would push new nodes. Each new node would push down on the next creating what would look like a tower of blocks. The problem with a stack is that if data at the bottom of the stack needs to be popped off the stack it has to wait for everything else to go first. This is a problem in programs that create stacks of processes. Starvation is the term used for a process that does not get a chance to execute.


QUEUE
The image of the queue can be viewed as a line of people waiting at a bank. The first person in line would get to go first. After that person completes their transaction they can leave the queue and the next person can be in front of the line. A queue is a good way to ensure that everyone gets treated equally and prevents starvation from occuring.

Sunday, April 19, 2009

Linear Data Structures :: Linked List

Usually the first data structure that is taught is the linked list. This is because it is simple and easy to implement. In order to create a linked list it is necessary to understand pointers. (The Pointers lesson is located in the archive) A linked list can be as large or small as the programmer wants it to be; of course memory size has to be taken into consideration. A linked list is individual nodes chained together by pointers. Each node can contain any number of fields of information depending on the structure. The following is an example of defining the structure of a linked list.



Adding a node to a list is easiest to do at the end of the list. Creating the new node can be done by using the following example.


This creates a new node and sets the values of each field in the node equal to a value that would be input from the terminal or from file. Traversing the list and deleting a node from the linked list is more difficult and will be discussed in later posts.

Thursday, April 9, 2009

Introduction to Programming :: Pointers

Reference Operator

Pointers are exactly what the name suggests. They point at something else. The first topic of pointers that is necessary to understand is the reference operator. A reference operator points at a location in memory rather than the actual value stored within that location. Imagine a piece of memory (A) at location 1000 for example is holding a value of 50.

Input
A = 50;
B = A;
C = &A;

Output
B=50
C=1000

B is the value of A and C is the location of A.

Dereference Operator

The following dereference example consists of (A) once again at location 1000 with a stored value of 50.

Input
A= 1000(memory location) [50 is stored here]
B= *A

Output
B= Value of the location A is equivalent to. (50)