Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

UNIT-1

Introduction to Basic Terminology: Data and Information, ADT, Data Organization


and types of Data Structure

DATA STRUCTURES
Data may be organized in many different ways. The logical or mathematical model of aparticular
organization of data is called a data structure.
The choice of a particular data model depends on the two considerations
1. It must be rich enough in structure to mirror the actual relationships of the data in thereal
world.
2. The structure should be simple enough that one can effectively process the data
whenever necessary.

Basic Terminology: Elementary Data Organization:

Data: Data are simply values or sets of values.

Data items: Data items refers to a single unit of values.

Data items that are divided into sub-items are called Group items. Ex: An Employee Namemay
be divided into three subitems- first name, middle name, and last name.

Data items that are not able to divide into sub-items are called Elementary items.
Ex: SSN

Entity: An entity is something that has certain attributes or properties which may be assignedvalues.
The values may be either numeric or non-numeric.
Ex: Attributes- Names, Age, Sex, SSN
Values- Rohland Gail, 34, F, 134-34-5533

Entities with similar attributes form an entity set. Each attribute of an entity set has a range ofvalues,
the set of all possible values that could be assigned to the particular attribute.
The term “information” is sometimes used for data with given attributes, of, in other words
meaningful or processed data.

Field is a single elementary unit of information representing an attribute of an entity.

Record is the collection of field values of a given entity.

File is the collection of records of the entities in a given entity set.


Each record in a file may contain many field items but the value in a certain field may uniquely
determine the record in the file. Such a field K is called a primary key and the values k1, k2,
….. in such a field are called keys or key values.

Records may also be classified according to length.


A file can have fixed-length records or variable-length records.
• In fixed-length records, all the records contain the same data items with the same amountof
space assigned to each data item.
• In variable-length records file records may contain different lengths.

Example: Student records have variable lengths, since different students take different numbers
of courses. Variable-length records have a minimum and a maximum length.

The above organization of data into fields, records and files may not be complex enough to maintain
and efficiently process certain collections of data. For this reason, data are also organized into more
complex types of structures.

The study of complex data structures includes the following three steps:
1. Logical or mathematical description of the structure
2. Implementation of the structure on a computer
3. Quantitative analysis of the structure, which includes determining the amount ofmemory
needed to store the structure and the time required to process the structure.

Need for Data Structure


• It gives different level of organization data.
• It tells how data can be stored and accessed in its elementary level.
• Provide operation on group of data, such as adding an item, looking up highest priority item.
• Provide a means to manage huge amount of data efficiently.
• Provide fast searching and sorting of data

Goals of Data Structure


Data structure basically implements two complementary goals.
Correctness: Data structure is designed such that it operates correctly for all kinds of input, which is
based on the domain of interest. In other words, correctness forms the primary goal of data structure,
which always depends on the specific problems that the data structure is intended to solve.
Efficiency: Data structure also needs to be efficient. It should process the data at high speed without
utilizing much of the computer resources such as memory space. In a real time state, the efficiency of a
data structure is an important factor that determines the success and failure of the process.

Features of Data Structure


Some of the important features of data structures are:
Robustness: Generally, all computer programmers wish to produce software that generates correct
output for every possible input provided to it, as well as execute efficiently on all hardware platforms.
This kind of robust software must be able to manage both valid and invalid inputs.

Adaptability: Developing software projects such as word processors, Web browsers and Internet search
engine involves large software systems that work or execute correctly and efficiently for many years.
Moreover, software evolves due to ever changing market conditions or due to emerging technologies.

Reusability: Reusability and adaptability go hand-in-hand.


It is a known fact that the programmer requires many resources for developing any software, which
makes it an expensive enterprise. However, if the software is developed in a reusable and adaptable way,
then it can be implemented in most of the future applications. Thus, by implementing quality data
structures, it is possible to develop reusable software, which tends to be cost effective and time saving.

Data Types

In C programming language, data types are used to define the type of data that a variable can hold.

Each data type has a specific size and range of values that it can represent.

There are several built-in data types in C, which include:

• int - used to store integers (whole numbers)

• float - used to store floating-point numbers (numbers with a decimal point)

• double - used to store double-precision floating-point numbers

• char - used to store single characters

• short - used to store small integers

• long - used to store large integers

• long long - used to store very large integers

• unsigned int - used to store non-negative integers

• unsigned char - used to store non-negative single characters

• void - used to indicate an empty data type, usually used with pointers

These data types can be modified using different specifiers like signed, unsigned, short, and long to
provide additional variations.

For example, "short int" is used to store small integers, and "long double" is used to store floating-point
numbers with greater precision than the standard "double" type.

Example 1:
int age = 25;
The above example declares a variable named "age" of the "int" data type and assigns it a value of 25.

Example 2:
float price = 9.99;
The above example declares a variable named "price" of the "float" data type and assigns it a value of
9.99.

Classification of Data Types in C


In C programming language, data types can be classified into three categories:

1. Basic Data Types


These are the fundamental data types in C that are directly supported by the programming language.

They include:

• int: Used to store whole numbers.

• char: Used to store a single character.

• float: Used to store floating-point numbers.

• double: Used to store double-precision floating-point numbers.

• void: Used to indicate an empty data type.

2. Derived Data Types


The derived data types in C are derived from the basic data types and can be created using various
operators.

They include:

• Arrays: A collection of elements of the same data type.

• Pointers: A variable that stores the memory address of another variable.

• Structures: A collection of variables of different data types grouped together under a single
name.

• Unions: A special type of structure that can store only one value at a time.

3. User-defined Data Types


The user-defined data types in C are defined by the programmer and are created using the basic and
derived data types.
They include:

• Enums: A user-defined data type that represents a set of named constants.

• Typedefs: A way to create a new name for an existing data type to improve code readability and
maintainability.

Range of Values of C Data Type

The range of all the C language data types are present in the table given below
CLASSIFICATION OF DATA STRUCTURES

Data structures are generally classified into:

• Primitive data Structures


• Non-primitive data Structures

1. Primitive data Structures: Primitive data structures are the fundamental data types which are
supported by a programming language. Basic data types such as integer, real, character and
Boolean are known as Primitive data Structures. These data types consists of characters that
cannot be divided and hence they also called simple data types.

2. Non- Primitive data Structures: Non-primitive data structures are those data structures which
are created using primitive data structures. Examples of non-primitive data structures is the
processing of complex numbers, linked lists, stacks, trees, and graphs.
Based on the structure and arrangement of data, non-primitive data structures is furtherclassified
into
1. Linear Data Structure
2. Non-linear Data Structure

1.Linear Data Structure:


A data structure is said to be linear if its elements form a sequence or a linear list. There are
basically two ways of representing such linear structure in memory.
1. One way is to have the linear relationships between the elements represented by meansof
sequential memory location. These linear structures are called arrays.
2. The other way is to have the linear relationship between the elements represented by
means of pointers or links. These linear structures are called linked lists.

The common examples of linear data structure are Arrays, Queues, Stacks, Linked list
Types of Linear Data Structures
1.Arrays:
The simplest type of data structure is a linear (or one dimensional) array. A list of a finite number n
of similar data referenced respectively by a set of n consecutive numbers, usually 1,2, 3 . . . . . . .
n. if A is chosen the name for the array, then the elements of A are denoted by subscript notation
a1, a2, a3….. an
or
by the parenthesis notation A (1), A (2), A (3) ............... A (n)
or
by the bracket notation A [1], A [2], A [3] ............... A [n]

Example 1: A linear array STUDENT consisting of the names of six students is pictured in
below figure. Here STUDENT [1] denotes John Brown, STUDENT [2] denotes Sandra Gold,
and so on.

Linear arrays are called one-dimensional arrays because each element in such an array is referenced
by one subscript. A two-dimensional array is a collection of similar data elements where each
element is referenced by two subscripts.

Example 2: A chain of 28 stores, each store having 4 departments, may list its weekly sales as in
below fig. Such data can be stored in the computer using a two-dimensional array in which the first
subscript denotes the store and the second subscript the department. If SALES is the name given to
the array, then
SALES [1, 1] = 2872, SALES [1, 2] - 805, SALES [1, 3] = 3211,…., SALES [28, 4] = 982

2. Stack: A stack, also called a fast-in first-out (LIFO) system, is a linear list in which insertions and
deletions can take place only at one end, called the top. This structure is similar in its operation to a
stack of dishes on a spring system as shown in fig.
Note that new 4 dishes are inserted only at the top of the stack and dishes can be deleted only from
the top of the Stack.

3.Queue: A queue, also called a first-in first-out (FIFO) system, is a linear list in which deletions can
take place only at one end of the list, the "from'' of the list, and insertions can take place only at the
other end of the list, the “rear” of the list.
This structure operates in much the same way as a line of people waiting at a bus stop, as pictured in
Fig. the first person in line is the first person to board the bus. Another analogy is with automobiles
waiting to pass through an intersection the first car in line is the first car through.

4.Linked Lists

A Linked List is another example of a linear data structure used to store a collection of data elements
dynamically. Data elements in this data structure are represented by the Nodes, connected using links or
pointers. Each node contains two fields, the information field consists of the actual data, and the pointer
field consists of the address of the subsequent nodes in the list. The pointer of the last node of the linked
list consists of a null pointer, as it points to nothing. Unlike the Arrays, the user can dynamically adjust
the size of a Linked List as per the requirements.

A Linked List

2. Non-linear Data Structure:


A data structure is said to be non-linear if the data are not arranged in sequence or a linear. The
insertion and deletion of data is not possible in linear fashion. This structure is mainly used to
represent data containing a hierarchical relationship between elements. Trees and graphs are the
examples of non-linear data structure.
Trees
Data frequently contain a hierarchical relationship between various elements. The data structure
which reflects this relationship is called a rooted tree graph or a tree.

Figure: Tree
Some of the basic properties of tree are explained by means of examples

Example 1: Record Structure


Although a file may be maintained by means of one or more arrays a record, where one indicates both
the group items and the elementary items, can best be described by means of a tree structure.
For example, an employee personnel record may contain the following data items:
Social Security Number, Name, Address, Age, Salary, Dependents

However, Name may be a group item with the sub-items Last, First and MI (middle initial). Also
Address may be a group item with the subitems Street address and Area address, where Area itself
may be a group item having subitems City, State and ZIP code number.
This hierarchical structure is pictured below
Another way of picturing such a tree structure is in terms of levels, as shown below

1. Graph: Data sometimes contain a relationship between pairs of elements which is not necessarily
hierarchical in nature. For example, suppose an airline flies only between the cities connected by lines
in Fig. The data structure which reflects this type of relationship is called a graph.

DATA STRUCTURES OPERATIONS


The data appearing in data structures are processed by means of certain operations. The
following four operations play a major role in this text:
1. Traversing: accessing each record/node exactly once so that certain items in the record may
be processed. (This accessing and processing is sometimes called “visiting” the record.)
2. Searching: Finding the location of the desired node with a given key value, or finding the
locations of all such nodes which satisfy one or more conditions.
3. Inserting: Adding a new node/record to the structure.
4. Deleting: Removing a node/record from the structure.
5. Create: Create is an operation used to reserve memory for the data elements of the program.
We can perform this operation using a declaration statement. The creation of data structure can
take place either during the following:
a. Compile-time
b. Run-time
For example, the malloc() function is used in C Language to create data structure.

6. Selection: Selection means selecting a particular data from the available data. We can select
any particular data by specifying conditions inside the loop.
7. Update: The Update operation allows us to update or modify the data in the data structure. We
can also update any particular data by specifying some conditions inside the loop, like the
Selection operation.
8. Splitting: The Splitting operation allows us to divide data into various subparts decreasing the
overall process completion time.

The following two operations, which are used in special situations:


1. Sorting: Arranging the records in some logical order (e.g., alphabetically according to some
NAME key, or in numerical order according to some NUMBER key, such as social security
number or account number)
2. Merging: Combining the records in two different sorted files into a single sorted file.

Abstract Data Type


Abstract Data type (ADT) is a type (or class) for objects whose behavior is defined by a set of values
and a set of operations. The definition of ADT only mentions what operations are to be performed but
not how these operations will be implemented. It does not specify how data will be organized in
memory and what algorithms will be used for implementing the operations. It is called “abstract”
because it gives an implementation-independent view.
The process of providing only the essentials and hiding the details is known as abstraction.

The user of data type does not need to know how that data type is implemented, for example, we have
been using Primitive values like int, float, char data types only with the knowledge that these data type
can operate and be performed on without any idea of how they are implemented.
So a user only needs to know what a data type can do, but not how it will be implemented. Think of
ADT as a black box which hides the inner structure and design of the data type. Now we’ll define three
ADTs namely List ADT, Stack ADT, Queue ADT.
1. List ADT
View of list
• The data is generally stored in key sequence in a list which has a head structure consisting
of count, pointers and address of compare function needed to compare the data in the list.
• The data node contains the pointer to a data structure and a self-referential pointer which
points to the next node in the list.
• The List ADT Functions is given below:
• get() – Return an element from the list at any given position.
• insert() – Insert an element at any position of the list.
• remove() – Remove the first occurrence of any element from a non-empty list.
• removeAt() – Remove the element at a specified location from a non-empty list.
• replace() – Replace an element at any position by another element.
• size() – Return the number of elements in the list.
• isEmpty() – Return true if the list is empty, otherwise return false.
• isFull() – Return true if the list is full, otherwise return false.

2. Stack ADT

View of stack
• In Stack ADT Implementation instead of data being stored in each node, the pointer to data
is stored.
• The program allocates memory for the data and address is passed to the stack ADT.
• The head node and the data nodes are encapsulated in the ADT. The calling function can
only see the pointer to the stack.
• The stack head structure also contains a pointer to top and count of number of entries
currently in stack.
• push() – Insert an element at one end of the stack called top.
• pop() – Remove and return the element at the top of the stack, if it is not empty.
• peek() – Return the element at the top of the stack without removing it, if the stack is not
empty.
• size() – Return the number of elements in the stack.
• isEmpty() – Return true if the stack is empty, otherwise return false.
• isFull() – Return true if the stack is full, otherwise return false.

3. Queue ADT

View of Queue
• The queue abstract data type (ADT) follows the basic design of the stack abstract data type.
• Each node contains a void pointer to the data and the link pointer to the next element in the
queue. The program’s responsibility is to allocate memory for storing the data.
• enqueue() – Insert an element at the end of the queue.
• dequeue() – Remove and return the first element of the queue, if the queue is not empty.
• peek() – Return the element of the queue without removing it, if the queue is not empty.
• size() – Return the number of elements in the queue.
• isEmpty() – Return true if the queue is empty, otherwise return false.
• isFull() – Return true if the queue is full, otherwise return false.

Features of ADT:

Abstract data types (ADTs) are a way of encapsulating data and operations on that data into a
single unit. Some of the key features of ADTs include:
• Abstraction: The user does not need to know the implementation of the data structure only
essentials are provided.
• Better Conceptualization: ADT gives us a better conceptualization of the real world.
• Robust: The program is robust and has the ability to catch errors.
• Encapsulation: ADTs hide the internal details of the data and provide a public interface for
users to interact with the data. This allows for easier maintenance and modification of the
data structure.
• Data Abstraction: ADTs provide a level of abstraction from the implementation details of
the data. Users only need to know the operations that can be performed on the data, not how
those operations are implemented.
• Data Structure Independence: ADTs can be implemented using different data structures,
such as arrays or linked lists, without affecting the functionality of the ADT.
• Information Hiding: ADTs can protect the integrity of the data by allowing access only to
authorized users and operations. This helps prevent errors and misuse of the data.
• Modularity: ADTs can be combined with other ADTs to form larger, more complex data
structures. This allows for greater flexibility and modularity in programming.
Advantages:
• Encapsulation: ADTs provide a way to encapsulate data and operations into a single unit,
making it easier to manage and modify the data structure.
• Abstraction: ADTs allow users to work with data structures without having to know the
implementation details, which can simplify programming and reduce errors.
• Data Structure Independence: ADTs can be implemented using different data structures,
which can make it easier to adapt to changing needs and requirements.
• Information Hiding: ADTs can protect the integrity of data by controlling access and
preventing unauthorized modifications.
• Modularity: ADTs can be combined with other ADTs to form more complex data
structures, which can increase flexibility and modularity in programming.
Disadvantages:
• Overhead: Implementing ADTs can add overhead in terms of memory and processing,
which can affect performance.
• Complexity: ADTs can be complex to implement, especially for large and complex data
structures.
• Learning Curve: Using ADTs requires knowledge of their implementation and usage,
which can take time and effort to learn.
•Limited Flexibility: Some ADTs may be limited in their functionality or may not be
suitable for all types of data structures.
• Cost: Implementing ADTs may require additional resources and investment, which can
increase the cost of development
.
Some Applications of Data Structures

The following are some applications of Data Structures:

1. Data Structures help in the organization of data in a computer's memory.


2. Data Structures also help in representing the information in databases.
3. Data Structures allows the implementation of algorithms to search through data (For example,
search engine).
4. We can use the Data Structures to implement the algorithms to manipulate data (For example,
word processors).
5. We can also implement the algorithms to analyse data using Data Structures (For example, data
miners).
6. Data Structures support algorithms to generate the data (For example, a random number
generator).
7. Data Structures also support algorithms to compress and decompress the data (For example, a zip
utility).
8. We can also use Data Structures to implement algorithms to encrypt and decrypt the data (For
example, a security system).
9. With the help of Data Structures, we can build software that can manage files and directories (For
example, a file manager).
10. We can also develop software that can render graphics using Data Structures. (For example, a
web browser or 3D rendering software).

You might also like