You are on page 1of 21

Data Structures & Algorithms-[COSC-2101]

Mr. Muhammad Umer


Introduction to Data Structure
Definitions

Data: Collection of raw facts.


Data structure: is representation of the logical
relationship existing between individual
elements of data.
Data structure: is a specialized format for
organizing and storing data in memory that
considers not only the elements stored but also
their relationship to each other.
Introduction

Data structure affects the design of both


structural & functional aspects of a program.
Program=algorithm + Data Structure
You know that a algorithm is a step by step
procedure to solve a particular function.
Classification of Data Structure

Data structure are normally divided into


two broad categories:
◦ Primitive Data Structure
◦ Non-Primitive Data Structure
Primitive Data Structure
There are basic structures and directly
operated upon by the machine instructions.
Data structures that are directly operated
upon the machine-level instructions are
known as primitive data structures.
Integer, Floating-point number, Character
constants, string constants, pointers etc, fall
in this category.
Primitive Data Structure
The most commonly used operation on data
structure are broadly categorized into
following types:
◦ Create
◦ Selection
◦ Updating
◦ Destroy or Delete
Non Primitive Data Structure

There are more sophisticated data


structures.
The Data structures that are derived from the
primitive data structures are called Non-primitive
data structure.
The non-primitive data structures
emphasize on structuring of a group of
homogeneous (same type) or heterogeneous
.
(different type) data items.
Non Primitive Data Structure
Linear Data structures:
◦ Linear Data structures are kind of data structure
that has homogeneous elements.
◦ The data structure in which elements are in a
sequence and form a liner series.
◦ Linear data structures are very easy to implement,
since the memory of the
computer is also organized in a linear fashion.
◦ Some commonly used linear data structures are
Stack, Queue and Linked Lists.
Non Primitive Data Structure
Non-Linear Data structures:
◦ A Non-Linear Data structures is a data structure in
which data item is connected to several other data
items.
◦ Non-Linear data structure may exhibit either a
hierarchical relationship or parent child
relationship.
◦ The data elements are not arranged in a sequential
structure.
◦ The different non-linear data structures are trees and
graphs.
Non Primitive Data Structure

The most commonly used operation on data


structure are broadly categorized into
following types:
◦ Traversal
◦ Insertion
◦ Selection
◦ Searching
◦ Sorting
◦ Merging
◦ Destroy or Delete
Taxonomy of Data Structure
Data Structure

Simple Data Structure


Compound Data Structure

Integer Float Character Pointer

Linear Data Structure Non-Linear Data Structure

Array Linked List Stack Queue Tree Graph Files


Operation on Data Structure

● Add an element
● Delete an element
● Traverse / Display
● Sort the list of elements

Search for a data element
Selecting a Data Structure

● Analyze the problem to determine the resource


constraints a solution must meet.
● Determine the basic operations that must be
supported. Quantify the resource constraints
for each operation.
● Select the data structure that best meets these
requirements.
Pointers

● Are there pointers in Java?


The short answer is “no, there are none”

● That is because the references that Java uses to access


objects are very similar to pointers.

● Lambda expressions are introduced in Java 8 to


facilitates functional programming
Java Pointers Example

public class Java8Tester {


public static void main(String args[]) {
Java8Tester tester = new Java8Tester();
//with type declaration MathOperation addition = (int a, int b) -> a + b;
//with out type declaration MathOperation subtraction = (a, b) -> a - b;
//with return statement along with curly braces MathOperation multiplication =
(int a, int b) -> { return a * b; };
//without return statement and without curly braces MathOperation division =
(int a, int b) -> a / b;
System.out.println("10 + 5 = " + tester.operate(10, 5, addition));
System.out.println("10 - 5 = " + tester.operate(10, 5, subtraction));
System.out.println("10 x 5 = " + tester.operate(10, 5, multiplication));
System.out.println("10 / 5 = " + tester.operate(10, 5, division));
//without parenthesis GreetingService greetService1 = message ->
System.out.println("Hello " + message);
//with parenthesis GreetingService greetService2 = (message) ->
System.out.println("Hello " + message);
greetService1.sayMessage("Mahesh");
greetService2.sayMessage("Suresh");
Java Pointers Example

interface MathOperation {
int operation(int a, int b);
}

interface GreetingService {
void sayMessage(String message);
}

private int operate(int a, int b, MathOperation mathOperation) {


return mathOperation.operation(a, b);
}
}

Output:
10 + 5 = 15
10 - 5 = 5
10 x 5 = 50
10 / 5 = 2
Hello Mahesh
Hello Suresh
Difference between JAVA and C Pointers

1. You can't do pointer arithmetic in Java (i.e. you can't


"add" or "subtract" from a Java reference, you can only
dereferencere it or compare it with another one).

2. You can't cast it to an incompatible type: Java is strongly


type-safe, you can't "re-interpret" the bytes in memory as
some other object.
Memory Management In Java

In Java, memory management is the process of allocation and de-allocation of


objects, called Memory management. Java does memory management
automatically. Java uses an automatic memory management system called a
garbage collector. Thus, we are not required to implement memory management
logic in our application. Java memory management divides into two major parts:

• JVM Memory Structure


• Working of the Garbage Collector
Useful Links

1. https://stackoverflow.com/questions/7480783/pointers-in-java
2. https://dzone.com/articles/pointers-in-java
3. https://www.tutorialspoint.com/Function-pointers-in-Java
4. https://www.javatpoint.com/memory-management-in-java
Any Question

You might also like