Sie sind auf Seite 1von 6

Introduction to Data Structures

Imagine that you are hired by company XYZ


to organize all of their records into a computer
database. The first thing you are asked to do is create a database of names with all the company's
management and employees. To start your work, you make a list of everyone in the company
along with their position.
Name

Position

Aaron

Manager

Charles

VP

George

Employee

Jack

Employee

Janet

VP

John

President

Kim

Manager

Larry

Manager

Martha

Employee

Patricia

Employee

Rick

Secretary

Sarah

VP

Susan

Manager

Thomas

Employee

Zack

Employee

You want your database to represent the relationships between management and employees at.
Although your list contains both name and position, it does not tell you which managers are
responsible for which workers and so on. You decide that a tree diagram is a much better
structure for showing the work relationships at XYZ.

These two diagrams are examples of different data structures. In one of the data structures, your
data is organized into a list. This is very useful for keeping the names of the employees in
alphabetical order so that we can locate the employee's record very quickly. However, this
structure is not very useful for showing the relationships between employees. A tree structure is
much better suited for this purpose.
In computer science, data structures are an important way of organizing information in a
computer. Just like the diagrams above illustrate, there are many different data structures that
programmers use to organize data in computers. Some data structures are similar to the tree
diagram because they are good for representing relationships between data. Other structures are
good for ordering data in a particular way like the list of employees. Each data structure has
unique properties that make it well suited to give a certain view of the data.
During these lessons, you will learn how data structures are created inside a computer. You will
find there is quite a difference between your mental picture of a data structure and the actual way
a computer stores a data structure in memory. You will also discover that there are many different
ways of creating the same data structure in a computer. These various approaches are tradeoffs
that programmers must consider when writing software. Finally, you will see that each data
structure has certain operations that naturally fit with the data structure. Often these operations
are bundled with the data structure and together they are called a data type. By the end of this
study, you should be able to do the following:

Show how data structures are represented in the computer,


Identify linear and nonlinear data structures,

Manipulate data structures with basic operations, and

Compare different implementations of the same data structure.

How Computer Memory Works With Data

Every piece of data that is stored in a computer is kept in a memory cell with a specific address.
We can think of these memory cells as being a long row of boxes where each box is labeled with
an address. If you have ever used a computer spreadsheet before, you know that spreadsheets
also have labeled boxes that can hold data. Computer memory is similar to this with the
exception that computer memory is linear. That's why we think of computer memory as being
organized in a row rather than a grid like a spreadsheet.

The computer can store many different types of data in its memory. You have already learned
about some of the basic types of data the computer uses. These include integers, real numbers,
and characters. Once the computer stores data in the memory cells, it can access the data by
using the address of the data cells. For example, consider the following instructions for adding
two integers together.
Instructions

Computer Memory

1. Store '1' in cell 2003.


2. Store '5' in cell 2004.
3. Add cells 2003 and 2004 and store the result in
cell 2006.
Notice how the computer performs operations by referring to the address of the memory cells.
These addresses are a very important component in creating various data structures in computer
memory. For example, suppose we want a data structure that can store a group of characters as a
word. In many computer languages, this data structure is called a string. If we store the
characters for the string 'apple' in the computer's memory, it might look something like this.

In order for the computer to recognize that 'apple' is a string, it must have some way of
identifying the start and end of the characters stored in memory. This is why the addresses of the
memory cells are important. By using the addresses to refer to a group of memory cells as a
string, the computer can store many strings in a row to create a list. This is one way that we
could create a data structure to represent our list of employees at company XYZ.

But what happens when we try to represent our tree diagram of company XYZ? It doesn't make
sense to store the names one after the other because the tree is not linear. Now we have a
problem. We want to represent a nonlinear data structure using computer memory that is linear.
In order to do this, we are going to need some way of mapping nonlinear structures like trees or
spreadsheet tables onto linear computer memory. In the next lesson we will see one solution to
this problem.
Pointers and Indirection

In our last lesson, we discovered a problem with representing data structures that are not linear.
We need some way to map these data structures to the computer's linear memory. One solution is
to use pointers. Pointers are memory locations that are stored in memory cells. By using a
pointer, one memory cell can "point" to another memory cell by holding a memory address rather
than data. Let's see how it works.

In the diagram above, the memory cell at address 2003 contains a pointer, an address of another
cell. In this case, the pointer is pointing to the memory cell 2005 which contains the letter 'c'.
This means that we now have two ways of accessing the letter 'c' as stored data. We can refer to
the memory cell which contains 'c' directly or we can use our pointer to refer to it indirectly. The
process of accessing data through pointers is known as indirection. We can also create multiple
levels of indirection using pointers. The diagram below shows an example of double indirection.
Notice that we must follow two pointers this time to reach the stored data.

As you can see, pointers can become very complex and difficult to use with many levels of
indirection. In fact, when used incorrectly, pointers can make data structures very difficult to
understand. Whenever we use pointers in constructing data structures, we have to consider the
tradeoff between complexity and flexibility. We will consider some examples of this tradeoff in
the next few lessons.
The idea of pointers and indirection is not exclusive to computer memory. Pointers appear in
many different aspects of computer use. A good example is hyperlinks in web pages. This links
are really pointers to another web page. Perhaps you have even experienced "double indirection"
when you went to visit a familiar web site and found the site had moved. Instead of the page you
expected, you saw a notice that the web pages had been moved and a link to the new site. Rather
than clicking a single link, you had to follow two links or two pointers to reach the web page.
Linear Data Structures
In our previous lesson, we saw that it is very simple to create data structures that are organized
similar to the way the computer's memory is organized. For example, the list of employee's from
the XYZ company is a linear data structure. Since the computer's memory is also linear, it is very
easy to see how we can represent this list with the computer. Any data structure which organizes
the data elements one after the other is a linear data structure. So far we have seen two examples
of linear data structures: the string data structure (a list of characters) and the XYZ company list
(a list of strings).
Example String

Example List

You may have noticed that these two examples of linear data structures resemble each other. This
is because they are both really different kinds of lists. In general, all linear data structures look
like a list. However, this does not mean that all linear data structures are exactly the same.
Suppose I want to design a list to store the names of the XYZ employees in the computer. One
possible design is to organize the names similar to the example picture above. Another possible
design is to use the pointers we learned about in the last lesson. While these two designs provide
the same functionality (i.e. a list that can hold names), the way they are implemented in the
computer is much different. This means that there is an abstract view of a list which is distinct
from any particular computer implementation. We will return to this idea of an abstract view of a
data structure in the next few lessons.
Name
Aaron
Charles
George
Jack
Janet

You may have also noticed that the example


picture of the XYZ employees is not exactly the same as the
original list. Take another look at the employee list to the
right. When we make a list of names, we tend to organize this
list in a column rather than a row. In this case, the conceptual
or logical representation of a list is a column of names.
However, the physical representation of the list in the
computer's memory is a row of strings. For most data
structures, the way that we think about them is far different
from the way they are implemented in the computer. In other
words, the physical representation is much different than the
logical representation, especially in data structures that use
pointers.
During the next few lessons, we will examine several different linear data structures with a focus
on the following ideas:

The abstract view versus the implementation


The logical representation verses the physical representation

Comparison of various implementations

Das könnte Ihnen auch gefallen