You are on page 1of 10

Data Structure

Chapter 1

Data : collection of facts from which a conclusion may be drawn

( ‫) مجموعة من الحقائق التي يمكن استخالص استنتاجات منها‬


types of data ( textual, numeric, audio )

Data Structure :

a particular way of storing and organizing data in a computer so it can be used


efficiently
a group of data elements grouped together under one name
types of data structure ( array, linked list, stack, queue, tree )

Importance of data structure


goal : we need to organize data

purpose : facilitate efficient

storage of data

retrieval of data

manipulation of data

Operations performed on data structure

Traversing : visiting/Accessing each data element exactly once so that a


certain item in the data may be processed.

Searching : finding the location of a given data element ( key ) in the structure.

Data Structure 1
Insertion : Adding a new data element to the structure.

Deletion : removing a data element in the structure.

Sorting : arrange the data elements in some logical fashion ( ascending or


descending )

Merging : Combining data elements from two or more data strucure into one.

Types of data structure

Based on memory allocation ( ‫) بناء على تخصيص الذاكرة‬

- Static (or fixed sized) data structures ( ‫) ثابت‬


Such as arrays

-
Dynamic data structures (change size as needed) ( ‫) متغير‬
Such as Linked Lists

Based on Representation ( ‫) بناء على التمثيل‬

Data Structure 2
- Linear representation
Such as arrays and linked lists

- Non-linear representation
Such as trees and graphs

What is the Array?


an array is a collection of homogeneous data elements

all data elements are of the same type

array elements are stored in contiguous/ consecutive memory locations (


‫) متتابعه ومتجاوره‬

an array is static data structure ( it cannot grow or shrink during program


execution.. the size is fixed )

Array Dimensionality

one dimensional ( just a linear list )

only one subscript is required to access an individual element

two dimensional ( matrix or table ) - 2D

You must give a row/column


First element is at row 0, column 0

Array Operations

Accessing/indexing an element using its index

performance is very fast

we can access an index immediately without searching

Data Structure 3
Inserting : add an element at a certain index

adding an element in the beginning is very slow because we have to shift all
elements over one position

adding an element in the end is fast because no need to shift

Removal : remove an element at a certain index

removing an element in the beginning is slow because we have to shift all


elements backwards

removing an element in the end is fast because no need to shift

Searching trough the array

depends o the algorithm

some algorithms are faster than others

Chapter 2

code of linear search ( boolean )

public static boolean search(int[] a, int value){


for( int i =0; i < a.length ; i++ ){
if(value == a[i])
return true;
}
return false;
}

code of linear search ( with index)

Data Structure 4
public static int search(int[] a, int value){
for(int i =0; i < a.length; i++){
if(value == a[i])
return i;
}
return -1;
}

code of Binary search ( boolean )

public static boolean binSearch(int[] a, int value){


int low = 0;
int high = a.length - 1;
while(low <= high){
int mid = (high+low)/2;
if(a[mid] == value)
return true;
else if(a[mid] > value)
high = mid - 1;
else if(a[mid] < value)
high = mid + 1;
}
return false;
}

code of Binary search ( with index)

public static int binSearch(int[] a, int value){


int low = 0;
int high = a.length - 1;
while(low <= high){
int mid = (high+low)/2;

Data Structure 5
if(a[mid] == value)
return mid;
else if(a[mid] > value)
high = mid - 1;
else if(a[mid] < value)
high = mid + 1;
}
return -1;
}

Sorted List Matching Problem

first code:

public static void printMatches(String[] list1, String[] list2){


int i;
for(i = 0; i < list1.length; i++){
if(binSearch(list1,list[i]))
System.out.println(list[i]);
}
}

second code:

public static int binSearch(String[] names, String key){


int low = 0;
int high = names.length - 1;
while(low < high){
int mid = (low + high)/2;
if(key.compareTo(names[mid]) < 0)
high = mid -1;
else if(key.compareTo(names[mid]) > 0)
high = mid +1;
else

Data Structure 6
return true;
}
return false;
}

binary search is an algorithm for locating the position of an


item in a sorted array.

binary search algorithm: log2n


linear search algorithm: n

Chapter 3
What is linked list: abstraction of a list, a sequence of nodes which each node is
linked to the node following it ( ordered collection of data )

Why linked list?

dynamic, length can be increase or decrease as necessary

each node does not necessarily follow the previous one in memory

insertion and deletion is easy

Array Linked list

static ( fixed size ) dynamic

each element is stored in contiguous does not necessarily follow the other node
location in memory in memory

insertion and deletion is slow insertion and deletion is fast

Data Structure 7
each node has two parts:

linked list node class code

public class ll_node {


public int data;
public ll_node next;

public ll_node(int i){


this(i, null);
}

public ll_node(int i, ll_node n){


data = i;
next = n;
}
}

Data Structure 8
Head of the list
To reach the first node you must have a reference variable that simply points to
the first node of the list
So we will need two Java Classes:

1. The first class is for the individual nodes of the linked list

class ll_node

2. class that stores access to the actual list

class SLinkedList

This class defines the head/front reference variable

It can also define a “tail” (or end of list) reference variable

Depends on your own implementation

This class also defines many methods that operate on the list
insertion, deletion, searching, printing nodes, etc

code of class SLinkedList

public class SLinkedList {


private ll_node myList;
public SLinkedList(){
muList = null;
}

public boolean isEmpty(){


return myList == null;
}

Data Structure 9
--------
}

code of traverse and Print out data of a linked list

public void printAllNodes(){


ll_node help_ptr = myList;

while( help_ptr != null){


System.out.println(help_ptr.data + " ");
help_ptr = help_ptr.next;
}
}

code of traverse and Modifying data of a linked list


example: adding 10 to all data elements

public void modifyAllNodes(){


ll_node help_ptr = myList;

while( help_pte != null){


help_ptr.data = help_ptr.data + 10;
help_ptr = help_ptr.next;
}
}

Data Structure 10

You might also like