0% found this document useful (0 votes)
3 views

Module 1 Introduction to Data Structures

This document introduces data structures and algorithms, outlining their importance in organizing and storing data efficiently in computer memory. It distinguishes between linear and non-linear data structures, explaining their characteristics and examples, such as arrays, stacks, queues, trees, and graphs. Additionally, it covers the concept of algorithms, their features, and the use of pointers and indirection in data representation.

Uploaded by

Darlene Soberano
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Module 1 Introduction to Data Structures

This document introduces data structures and algorithms, outlining their importance in organizing and storing data efficiently in computer memory. It distinguishes between linear and non-linear data structures, explaining their characteristics and examples, such as arrays, stacks, queues, trees, and graphs. Additionally, it covers the concept of algorithms, their features, and the use of pointers and indirection in data representation.

Uploaded by

Darlene Soberano
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

MODULE 01

INTRODUCTION TO
DATA STRUCTURES

LEARNING OBJECTIVES

At the end of this module, you should be able to:

1. describe data structures and algorithm;


2. show understanding of the concept of computer memory and
data structures;
3. show understanding of pointers and indirection; and
4. categorize linear and non-linear data structures.

LOADING YOUR KNOWLEDGE

You are about to begin an exciting journey: learning the different


data structures and their algorithms. This module will introduce new
types of data structures. In this module, the basic concepts of software
engineering are presented.

CC 33 | Data Structures & Algorithms 1


LESSON 1.1
INTRODUCTION TO DATA STRUCTURES

LET’S PROCESS

During this lesson, we will examine several concepts with focus on


algorithm and data structures.

Algorithm

An algorithm is a sequence of statements to be executed in order, to


produce some desired result. Alternately, it is a finite set of rules which gives a
sequence of operations for solving a specific type of problem. As such, an
algorithm must be precise enough to be understood by human beings. However,
in order to be executed by a computer, we will generally need a program that is
written in a rigorous formal language; and since computers are quite inflexible
compared to the human mind, programs usually need to contain more details
than algorithms.

Reasons for writing algorithms

The following are the reasons why we should write algorithm:


1. to define the nature of a program; i.e., what function it performs;
2. a summary or guide to writing the program;
3. allows you to concentrate on logic and organization vs how to express the
ideas in a computer language;
4. after program is coded, programmer can use summary as check for
completeness; and
5. provides common medium for porting programs from one machine to
another.

Features of algorithm

1. Algorithms are written for people to read, not for computers.


2. An algorithm should not be confused with a computer program. A
computer program is a translation of an algorithm into a programming
language and it is a specific implementation of an algorithm.

CC 33 | Data Structures & Algorithms 2


3. An algorithm must be/have:
a. Finite – It terminates after a finite number of steps and in finite
time.
b. Definite or Unambiguous - Each step/action is precisely defined.
c. Input/Output - Has zero or more inputs, one or more outputs.
d. General - i.e. convert input number of inches to centimeters.
e. Effective/Correct - Operations must be sufficiently basic/correct.

Approaches to Representing Algorithms

1. Pseudocode
Pseudocode is a compact and informal high-level description of a
computer programming algorithm that uses the structural conventions of
some programming language, but is intended for human reading rather
than machine reading. It is the English-like representation of logic.
Pseudocode can become comments and can be applied to any program.
There is an easy translation from pseudocode into program.

2. Flowcharts
Flowcharts are visual representation of logic flow showing required
operations. It emphasizes flow of events. Flowcharts are easier to review
by peers because it is appropriate for designs that follow logical sequence
of events.

3. Finite State Automata


Finite State Automata (FSA) are visual representation showing
available states and state transitions. It emphasizes states and state
changes and used when system reacts to environment/conditions.

Data Structure

A data structure is a special way of organizing and storing data in a


computer so that it can be used efficiently. Array, LinkedList, Stack, Queue,
Tree and Graph are all data structures that stores the data in a special way so
that we can access and use the data efficiently. Each of these mentioned data
structures has a different special way of organizing data so we choose the data

CC 33 | Data Structures & Algorithms 3


structure based on the requirement, we will cover each of these data structures
during the whole semester.

Data Structure Types

We have two types of data structures (see figure 1.1):

1. Linear Data Structure


Data structure where data elements are arranged sequentially or
linearly where the elements are attached to its previous and next
adjacent in what is called a linear data structure. In linear data structure,
single level is involved. Therefore, we can traverse all the elements in
single run only. Linear data structures are easy to implement because
computer memory is arranged in a linear way. Its examples
are array, stack, queue and linked list.

2. Non-linear data structures


Data structures where data elements are not arranged sequentially
or linearly are called non-linear data structures. In a non-linear data
structure, single level is not involved. Therefore, we can’t traverse all the
elements in single run only. Non-linear data structures are not easy to
implement in comparison to linear data structure. It utilizes computer
memory efficiently in comparison to a linear data structure. Its examples
are trees and graphs.

Source: https://www.geeksforgeeks.org/difference-between-linear-and-non-linear-data-structures/
Figure 1.1. Types and examples of data structures.

The linear and non-linear data structures will be discussed further in


Lesson 1.4.

CC 33 | Data Structures & Algorithms 4


Advantages of data structures

We need data structures because there are several advantages of using


them, few of them are as follows:

1. Data Organization
We need a proper way of organizing the data so that it can be
accessed efficiently when we need that particular data. Data structure
provides different ways of data organization so we have options to store
the data in different data structures based on the requirement.

2. Efficiency
The main reason we organize the data is to improve the efficiency.
We can store the data in arrays then why do we need linked lists and
other data structures? Because when we need to perform several
operations such as add, delete, update and search on arrays, it takes
more time in arrays than some of the other data structures. So the fact
that we are interested in other data structures is because of the
efficiency.

CC 33 | Data Structures & Algorithms 5


LESSON 1.2
THE COMPUTER MEMORY

LET’S PROCESS

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.

Table 1.1. List of Names and Positions in company XYZ


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

But this list (table 1.1) only shows one view of the company. You also
want your database to represent the relationships between management and
employees at XYZ. Although your list contains both name and position, it does
not tell you which managers are responsible for which workers and so on. After
thinking about the problem for a while, you decide that a tree diagram is a
much better structure for showing the work relationships at XYZ.

Figure 1.2. A tree structure of the employees in company XYZ

CC 33 | Data Structures & Algorithms 6


These two diagrams (table 1.1 and figure 1.2) 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.

Computer Memory

To understand how a computer can represents large data structures like


our tree diagram, we first need to understand some basic facts about computer
memory. Every piece of data that is stored in a computer is kept in a memory
cell with a specific address (see figure 1.3). 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.

Source: http://courses.cs.vt.edu/~csonline/DataStructures/Lessons/Introduction/index.html
Figure 1.3. Sample memory cell with a specific address

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

CC 33 | Data Structures & Algorithms 7


stores data in the memory cells, it can access the data by using the address of
the data cells. For example, consider figure 1.4 showing instructions for adding
two integers together.

Source: http://courses.cs.vt.edu/~csonline/DataStructures/Lessons/Introduction/index.html
Figure 1.4. Sample instruction and computer memory allocation.

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 (see figure 1.5).

Source: http://courses.cs.vt.edu/~csonline/DataStructures/Lessons/Introduction/index.html
Figure 1.5. String ‘apple’ in the computer’s memory.

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.

Source: http://courses.cs.vt.edu/~csonline/DataStructures/Lessons/Introduction/index.html
Figure 1.6. The names of company XYZ employees in a linear structure.

CC 33 | Data Structures & Algorithms 8


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 (see figure 1.6). 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 lesson 3 of this
module, we will see one solution to this problem.

CC 33 | Data Structures & Algorithms 9


LESSON 1.3
POINTERS AND INDIRECTION

LET’S PROCESS

In lesson 1 of this module, 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 as shown in figure
1.5.

Source: http://courses.cs.vt.edu/~csonline/DataStructures/Lessons/Pointers/index.html
Figure 1.5. Using pointer in the computer's linear memory

In figure 1.5, 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. Figure 1.6 shows an
example of double indirection. Notice that we must follow two pointers this
time to reach the stored data.

Source: http://courses.cs.vt.edu/~csonline/DataStructures/Lessons/Pointers/index.html
Figure 1.6. Example of double indirection

CC 33 | Data Structures & Algorithms 10


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 trade-off between
complexity and flexibility. We will consider some examples of this trade-off 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.

CC 33 | Data Structures & Algorithms 11


LESSON 1.4
LINEAR AND NON-LINEAR DATA STRUCTURES

LET’S PROCESS

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. The data structure can be the interpretation of logical relationship
existing between the solitary elements of data. The linear and non-linear data
structure is the sub classification of the data structure which comes under the
non-primitive data structure. The crucial difference between them is that the
linear data structure arranges the data into a sequence and follows some sort
of order while the non-linear data structure does not organize the data in
a sequential manner as shown in figure 1.7.

Source: https://techdifferences.com/difference-between-linear-and-non-linear-data-structure.html
Figure 1.7. Sample illustration of linear and non-linear data structures

The linear data structure is a single level data structure while non-linear
data structures are the multilevel data structure. The data structure priorly
describes how the data is organised, accessed, associated and processed.

Linear Data Structure

The data structure is considered to be linear if the data elements


construct a sequence of a linear list. The elements are adjacently attached to
each other and in a specified order. It consumes linear memory space, the data
elements are required to store in a sequential manner in the memory. While
implementing the linear data structure the necessary amount of memory is

CC 33 | Data Structures & Algorithms 12


declared previously. It does not make a good utilization of memory and result
in memory wastage. The data elements are visited sequentially where only a
single element can be directly reached.

The examples included in the linear data structure are array, stack and
queue. An array is a group of a definite number of homogeneous elements or
data items. Stack and queue are also an ordered collection of the elements like
an array but there is a special condition where stack follows LIFO (Last in first
out) order and queue employ FIFO (First in first out) to insert and delete the
elements.

Non-linear Data Structure

Non-linear data structure does not arrange the data consecutively rather
it is arranged in sorted order. In this, the data elements can be attached to
more than one element exhibiting the hierarchical relationship which involves
the relationship between the child, parent, and grandparent. In the non-linear
data structure, the traversals of data elements and insertion or deletion are not
done sequentially.

The non-linear data structure utilizes the memory efficiently and does
not require the memory declaration in advance. There are the two common
examples of the non-linear data structure – tree and graph. A tree data
structure organizes and stores the data elements in a hierarchical relationship.

Key Differences between Linear and Non-linear Data Structure

1. In the linear data structure, the data is organized in a linear order in


which elements are linked one after the other. As against, in the non-
linear data structure the data elements are not stored in a sequential
manner rather the elements are hierarchically related.

2. The traversing of data in the linear data structure is easy as it can make
all the data elements to be traversed in one go, but at a time only one
element is directly reachable. On the contrary, in the non-linear data
structure, the nodes are not visited sequentially and cannot be traversed
in one go.

CC 33 | Data Structures & Algorithms 13


3. Data elements are adjacently attached in the linear data structure, which
means only two elements can be linked to two other elements while this is
not the case in the non-linear data structure where one data element can
be connected to numerous other elements.

4. The linear data structures are easily implemented relative to the non-
linear data structure.

5. A single level of elements is incorporated in the linear data structure.


Conversely, non-linear data structure involves multiple levels.

6. Examples of the linear data structure are array, queue, stack, linked list,
etc. In contrast, tree and graph are the examples of the non-linear data
structure.

7. The memory is utilized efficiently in the non-linear data structure where


linear data structure tends to waste the memory.

Table 1.2. Comparison Chart

BASIS FOR LINEAR DATA NON-LINEAR DATA


COMPARISON STRUCTURE STRUCTURE

Basic The data items are It arranges the data in a


arranged in an orderly sorted order and there
manner where the exists a relationship
elements are attached between the data elements.
adjacently.

Traversing of The data elements can be Traversing of data elements


the data accessed in one time in one go is not possible.
(single run).

Ease of Simpler Complex


implementation

Levels involved Single level Multiple level

Examples Array, queue, stack, Tree and graph.


linked list, etc.

Memory Ineffective Effective


utilization

The linear data structures involve a single level of data elements and
represent the linear relationship. On the other hand, the non-linear data

CC 33 | Data Structures & Algorithms 14


structure is said to be multi-level data structure constitute a hierarchical
relationship among the data.

COMPILE YOUR KNOWLEDGE

For this module, these are the key points:

 An algorithm is a sequence of statements to be executed in order, to


produce some desired result.

 An algorithm must be finite, unambiguous, general and effective; and


must have input/output.

 The three approaches to representing algorithms are flowchart,


pseudocode and finite state automata.

 There are two types of data structures: the linear and non-linear data
structures.

 The advantages of data structures are data organization and efficiency.

REFERENCES

1. Bullinaria, J. (2019). Lecture Notes for Data Structures and


Algorithms. Retrieved from
https://www.cs.bham.ac.uk/~jxb/DSA/dsa.pdf

2. Singh, C. (2020). DS introduction. Begineer’s Book. Retrieved from


https://beginnersbook.com/2018/10/data-structure-
introduction/

3. https://www.geeksforgeeks.org/difference-between-linear-and-
non-linear-data-structures/

CC 33 | Data Structures & Algorithms 15


4. http://courses.cs.vt.edu/~csonline/DataStructures/Lessons/Intro
duction/index.html

5. http://courses.cs.vt.edu/~csonline/DataStructures/Lessons/Com
puterMemory/index.html

6. https://techdifferences.com/difference-between-linear-and-non-
linear-data-structure.html

CC 33 | Data Structures & Algorithms 16

You might also like