Introduction To Data Structures

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

Introduction to Data Structures

Specific Objectives
• What is Data Structure?
• Categories of Data Structure
• Linear Data Structure
• Nonlinear Data Structure
• Why do we need Data Structures?

Introduction to Data Structures 2


Introduction
• In the modern world, Data and its information is an essential part, and various
implementations are being made to store in different ways.
• Data are just a collection of facts and figures, or you can say data are values or a
set of values that are in a particular format.
• A data item refers to a single set of values.
• Data items are then further categorized into sub-items which are the group of
items which are not being called a plain elementary form of items.

Introduction to Data Structures 3


Introduction (Cont.)

• In an example where the name of the student may be divided into three sub-
items namely: first name, middle name and last name.
• The ID that is assigned to a student would normally be considered as a single
item.
• In the example mentioned above such as ID, Age, Gender, First, Middle, Last,
Street, Area, etc. are elementary data items, whereas (Name, Address) is group
data items.

Introduction to Data Structures 4


What is Data Structure?
• In computer terms, a data structure is a specific way to store and organize data in
a computer's memory so that these data can be used efficiently later.
• Data may be arranged in many different ways such as the logical or mathematical
model for a particular organization of data is termed as a data structure.
• The variety of a specific data model depends on the two factors -
o Firstly, it must be loaded enough in structure to reflect the actual relationships of the data
with the real world object.
o Secondly, the formation should be simple enough so that anyone can efficiently process the
data each time it is necessary.

Introduction to Data Structures 5


Categories of Data Structure
• The data structure can be subdivided into major types:
o Linear Data Structure
o Non-linear Data Structure

Introduction to Data Structures 6


Linear Data Structure
• A data structure is said to be linear if its elements combine to form any specific
order.
• There are two techniques of representing such linear structure within memory.
o The first way is to provide the linear relationships among all the elements represented using
linear memory location.
o These linear structures are termed as arrays.
o The second technique is to provide a linear relationship among all the elements represented
by using the concept of pointers or links.
o These linear structures are termed as linked lists.
• The common examples of the linear data structure are:
o Arrays
o Queues
o Stacks
o Linked lists

Introduction to Data Structures 7


Non-linear Data Structure
• This structure is mostly used for representing data that contains a hierarchical
relationship among various elements.
• Examples of Non-Linear Data Structures are listed below:
o graphs
o the family of trees and
o table of contents
• Tree
o In this case, data often contain a hierarchical relationship among various elements.
o The data structure that reflects this relationship is termed as a rooted tree graph or a tree.
• Graph
o In this case, data sometimes hold a relationship between the pairs of elements which is not
necessarily following the hierarchical structure.
o Such a data structure is termed as a Graph.

Introduction to Data Structures 8


Why do we need Data Structures?
• Data Organization
o We need a proper way of organizing the data so that it can accessed efficiently when we
need that particular data.
o Data structure provides different ways of data organization so we have options to store the
data in different data structures based on the requirement.
• Efficiency
o The main reason we organize the data is to improve the efficiency.
o We can store the data in arrays then why do we need linked lists and other data structures?
o When we need to perform several operation such as add, delete update and search on arrays
, it takes more time in arrays than some of the other data structures.
o So the fact that we are interested in other data structures is because of the efficiency.

Introduction to Data Structures 9


Arrays and Linked List
• An array is a set of similar data objects stored in sequential memory locations
under a common heading or a variable name.
• A linked list is a data structure which contains a sequence of the elements where
each element is linked to its next element.
• There are two fields in an element of linked list

Introduction to Data Structures 10


Arrays
• An array is defined as a set of a definite number of homogeneous elements or
data items.
• It means an array can contain one type of data only, either all integers, all
floating-point numbers, or all characters.
• Declaration of an array is as follows:
int a [10];
o Where int specifies the data type or type elements array stores.

o “a” is the name of an array

o the number specified inside the square brackets is the number of elements an

array can store, this is also called size or length of the array.

Introduction to Data Structures 11


Arrays (Cont.)

• The individual elements of an array can be accessed by describing the name of the
array, followed by index or subscript (determining the location of the element in the
array) inside the square brackets.
• For example, to retrieve 5th element of the array, we need to write a statement
a[4].
• In any case the elements of an array will be stored in a consecutive memory
location.
Introduction to Data Structures 12
Arrays (Cont.)
• The very first element of the array has index zero [0].
• It means the first and last element will be specified as a[0], and a[9] respectively.
• The number of elements that can be stored in an array, i.e., the size of an array or
its length is given by the following equation:
(upper bound-lower bound) + 1
• For the above array, it would be (9-0) + 1 =10.
• Where 0 is the lower bound of the array, and 9 is the upper bound of the array.

Introduction to Data Structures 13


Arrays (Cont.)
• Arrays can be read or written through the loop.
• If we read the one-dimensional array, it requires one loop for reading and other for
writing (printing) the array, for example:
a. For reading an array
int num[3];
int i;
for ( i= 0; i <= 3; i++)
cin>>num[i];
b. For writing an array
for (i = 0 ; i <= 3 ; i++) {
cout<<num[i]<<"\n\"<<endl;}
In the case of a 2-D array, it would require two loops and similarly n-dimensional array
would need n loops.

Introduction to Data Structures 14


Arrays (Cont.)
• Operations performed on arrays are:
1. Creation of array
2. Traversing an array
3. Insertion of new elements
4. Deletion of required elements.
5. Modification of an element.
6. Merging of arrays
• See example on next slide for the creation of an array

Introduction to Data Structures 15


Arrays (Cont.)
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {8, 6, 7, 13, 36};
cout << "The numbers are: ";
for (int i = 0; i < 5; ++i) {
cout << numbers[i] << " ";
}
return 0;
}

Introduction to Data Structures 16


Linked Lists
• Data can be organized and processed sequentially using an array, called a
sequential list
• Problems with an array
o Array size is fixed

o Unsorted array: searching for an item is slow

o Sorted array: insertion and deletion is slow

Introduction to Data Structures 17


Linked Lists (Cont.)
• Linked list: collection of components (nodes)
• A list is a homogeneous collection of elements, with a linear relationship
between elements.
• That is, each list element (except the first) has a unique predecessor, and each
element (except the last) has a unique successor.
• Every node (except last) has address of next node
• Every node has two components
• Address of the first node of the list is stored in a separate location, called head or
first

Introduction to Data Structures 18


Linked Lists (Cont.)

Introduction to Data Structures 19


Linked Lists (Cont.)
• A list of items, called nodes, in which the order of the nodes is determined by the
address, called the link, stored in each node

• The down arrow in the last node indicates that this link field is NULL
• The arrow in each node indicates that the address of the node to which it is
pointing is stored in that node

Introduction to Data Structures 20


Linked Lists (Cont.)

• Linked list above has four nodes


• Address of first node is stored in head
• Each node has two components:
o Info: stores the info

o Link: stores address of the next node

Introduction to Data Structures 21


Linked Lists (Cont.)
Types of Linked Lists
• There are 3 different implementations of Linked List available, they are:
1. Singly Linked List
2. Doubly Linked List
3. Circular Linked List

Introduction to Data Structures 22


Linked Lists (Cont.)
Singly Linked List
• Singly linked lists contain nodes which have a data part as well as an address part
i.e. next, which points to the next node in the sequence of nodes.
• The operations we can perform on singly linked lists are insertion, deletion and
traversal.

Introduction to Data Structures 23


Linked Lists (Cont.)
Doubly Linked List
• In a doubly linked list, each node contains a data part and two addresses, one for
the previous node and one for the next node.

Introduction to Data Structures 24


Linked Lists (Cont.)
Circular Linked List
• In circular linked list the last node of the list holds the address of the first node
hence forming a circular chain.

Introduction to Data Structures 25


Assignment 1 – Due Next Class (1-23-2021) – 8 % Assessment

1. Write a C++ program to initialize an array up to five elements. Explain your


output.

2. Traversing is an operation of displaying or listing all elements of an array.


Write a C++ program that explains traversing with a one-dimensional
array.

3. Write a C++ program to insert an element into position two of an array.


Explain your output.

Introduction to Data Structures 26

You might also like