You are on page 1of 17

Continuous Assessment Cover Sheet

Faculty of Engineering
Module Details
Module Code EC2731 Module Title Data Structures and Algorithms
Program: SLIIT Course: BSc
Stream: Civil/Electronics/Mechanical/

Assessment details

Title Implement ng a Data Group assignment NO


Structure using If yes, Group No.
Dynamic Arrays and
Linked List

Lecturer/ Prof. Chulantha Date of Performance 24 / 08 / 2022


Instructor
Due date statement
Student and signature
03.08.2022 Date submitted 03.08.2022
By this declaration, I/we confirm my/our understanding and acceptance that the work reported in this report is my/our own work. I/we also
understand the consequences of engaging in plagiarism or copying others work without proper citation. Any material used in this work
(whether from published sources, the internet or elsewhere) have been fully acknowledged and referenced and are without fabrication or
falsification of data.
[Copying or plagiarism will result in a “0” mark for the continuous assessment and “F” for the module after an investigation on academic
misconduct;
All academic misconduct is considered seriously and defined as dishonest and in direct opposition to the values of a learning community.
Misconduct may result in penalties from failure to exclusion from the campus.
Further help and guidance on how to avoid academic misconduct can be obtained from your academic advisor/tutor]

By this declaration, I/we confirm my understanding and acceptance that-


• I/we have adhered to relevant ethical guidelines and procedures in the completion of the assignment.
• I/we have not allowed another student to have access to or copy from this work.
• This work has not been submitted previously.
[The Institute may request an electronic copy of this work for submission to the Plagiarism detection facility (TURNITIN). You must make sure
that an electronic copy of your work is available in these circumstances]

Details of the student/s submitting the assignment Signature


ID Number Name (As per the institute records)

EN21464614 Jayasundara D.P.C.D. Chamod

OFFICE USE ONLY

Receiving Officer Specific comments about the work (including overall comments and guidelines for
(seal, signature, date) improvement)
Tutor: Signature: Date:
Marks: [All marks are subject to external moderation and approval of board of
examinations]

1|Page
SRI LANKA INSTITUTE OF
INFORMATION TECHNOLOGY

Department of Electrical and Electronic


Engineering

EC2731 – Data Structure and


Algorithms

Assignment 02 – Implementing a Data


Structure Using Dynamic Arrays and
Linked Lists

Name - Jayasundara D.P.C.D.

EN Number - EN21464614

2|Page
Task 01
Developing a container for 10,000 integer data points using a dynamic array.

A dynamic array is an array that has automated scaling as a significant enhancement. Because arrays
have a fixed size, one drawback is that you must predetermine how many elements your array will
include. The size of a dynamic array grows as you add additional components. Therefore, there is no
need to estimate the size in advance because the size is doubled once more when this array's maximum
is reached.

package task1;

import task1.task1;

public class task1 {

int array[]; // Creating an new array


int capacity; // Total storage capacity of the Array
int current; // Num of elements stored in the list currently

// Constructor to initialize the initial capacity of one element


public task1(){
array = new int[1];
capacity = 1;
current = 0;
}

// Method to add elements


public void push(int data){
if (current == capacity) {
// When the number of elements and the capacity is equal there is no space for more
elements. So, the capacity will be doubled.

int temp[] = new int[2 * capacity];


for (int i = 0; i < capacity; i++) // Copy old array
temp[i] = array[i];
capacity *= 2;
array = temp;
}
array[current] = data;
current++;
}

// Method to add elements to indexes


void push(int data, int index){
if (index == capacity) //if index is equal to capacity, then this
function acts ad same as the push function
push(data);
else
array[index] = data;
}

// Method to extract the elements


int get(int index){
if (index < current)
return array[index];
return -1; // if index is outside return value -1
}

// Method to delete the last element

3|Page
void pop(){current--;}

// Method to get the size of Array


int size(){return current; }

// Method to show the capacity of Array


int getcapacity(){return capacity;}

// Method to print Array List elements


void print(){
for (int i = 0; i < current; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}

// Getting the “task1” Class for Array List


public static void main(String args[]){
task1 v = new task1();

int n=10000; // Array with 10000 data points

for(int i=0;i<n;i++) {
v.push(i+1);
}

// Printing Statements
System.out.println("Array List Elements:- ");
v.print();
System.out.println("Size of the Array List: " + v.size());
System.out.println("Capacity of the Array List: " + v.getcapacity());

}
}

The results which obtained after running the above code are below.

Array List elements:-


1 2 3 4 5 6 7 8 9 10 11 12 … 9998 9999 10000
Size of the Array List: 10000
Capacity of the Array List: 16384

4|Page
Task 02
Finding the worst-case running times for indexing for the dynamic array implementation

To find the worst-case running times for indexing, main code was modified as this.

// Getting the “task1” Class for Array List


public static void main(String args[]){
t1 time = new t1();

int n=10000; // Array with 10000 data points


for(int i=0;i<n;i++) {
time.push(i+1);
}

long startTime = System.nanoTime();


time.get(n); // Get element in last position of array
long endTime = System.nanoTime();
long Time = (endTime - startTime);
System.out.println("The Worst Case Running Time for Size "+n+" is"+ Time +"
ns"); // Print Statement

The results which obtained after running the above code are below.

The Worst Case Running Time for Size 1 is 2200 ns


The Worst Case Running Time for Size 2 is 500 ns
The Worst Case Running Time for Size 4 is 400 ns
The Worst Case Running Time for Size 8 is 400 ns
The Worst Case Running Time for Size 16 is 500 ns
The Worst Case Running Time for Size 32 is 500 ns
The Worst Case Running Time for Size 64 is 300 ns
The Worst Case Running Time for Size 128 is 400 ns
The Worst Case Running Time for Size 256 is 400 ns
The Worst Case Running Time for Size 512 is 400 ns
The Worst Case Running Time for Size 1024 is 500 ns
The Worst Case Running Time for Size 2048 is 500 ns
The Worst Case Running Time for Size 4096 is 400 ns
The Worst Case Running Time for Size 4192 is 600 ns

According to the Results the running times for array sizes are mentioned below.

Array Size Running Time (ns)


1 2200
2 500
4 400
8 400
16 500
32 500
64 300
128 400
256 400
512 400
1024 500
2048 500

5|Page
4096 400
8192 600

Then, the plot of array size vs running time was created using above data.

Array Sizes vs Running Time


2500

2000
Running Time (ns)

1500

1000

500

0
0 1000 2000 3000 4000 5000 6000 7000 8000
Array Size

This proves that the time taken to index an array is irrespective to its size and i=that time is about 300-
400 ns. Here, the Big-O is also a constant ( O[1] ). That time complexity confirms it.

6|Page
Task 03
Developing a container for 10,000 integer data points using a singly linked list.

A variation of the linked list known as the singly linked list only permits forward traversal of linked
lists. This is a straightforward form, but it works for a variety of issues, including Big Integer
computations.

package task3;

import task3.Node;

class Node {
int data;
Node next;

// Inserting New Public Constructor


public Node(){
next = null;
data = 0;
}

// Inserting New Public Constructor


public Node(int d,Node n){
data = d;
next = n;
}

// Writing Function for Linking Nodes


public void setLink(Node n){
next = n;
}

// Writing Function to Implement the Data


public void setData(int d){
data = d;
}

// Writing Function to link the Next Node


public Node getLink(){
return next;
}

// Writing Function to Get Data from the Node


public int getData(){
return data;
}
}

public class task3 {


Node head;
Node tail ;
int size ;

public task3() {
head = null;
tail = null; // Setting head and tail as null values
size = 0;
}

7|Page
// Inserting new node at the front
public void push(int new_data){
Node new_node = new Node(new_data, null); // New node and with new
data
size++ ;

if(head == null){ // Checkingt


head = new_node;
tail = head;
} else{
new_node.setLink(head); // Connect the new node to the head node
head = new_node; // Move the head point to new Node
}
}

// Writing Function to Print Elements


public void display(){
System.out.print("Single Linked List = ");
if (size == 0){
System.out.print("empty\n");
return;
}
if (head.getLink() == null){
System.out.println(head.getData() );
return;
}
Node ptr = head;
System.out.print(head.getData()+ ">");
ptr = head.getLink();
while (ptr.getLink() != null){
System.out.print(ptr.getData()+ ">");
ptr = ptr.getLink();
}
System.out.print(ptr.getData()+ "\n");
}

// Finding the Specific Element Given Position


public int GetValAtPosition(int k) {
Node temp = head;
int count = 0;
while (count!=k){
temp = temp.next;
count++;
}
return temp.data;
}

// Finding Position of a Specific Element in the List


public int GetPostionofval(int x) {
Node temp = head;
int count = 0;
while (temp.data!=x){
temp = temp.next;
count++;
}
return count+1;
}
public static void main(String args[]){
t3 x = new t3(); //Start with the empty list

8|Page
int n=10000;
for(int i=0;i<n;i++) {
x.push(n-i);
}
x.display();
}
}

The results which obtained after running the above code are below.

Single Linked List=1>2>3>4>5>6>7>8>9>10>11>12>13>14>15>…>10000

Task 04
Finding the worst-case running times for indexing for the singly linked list implementation.

To find the worst-case running times for indexing, main code was modified as this.

public static void main(String args[]){


for(int n=1;n<=10000;n=n*2) {
task3 y = new task3(); //Start with the empty list
for(int i=0;i<n;i++) {
y.push(n-i);
}
long startTime = System.nanoTime();
y.GetValAtPosition(n-1);
long endTime = System.nanoTime();
long Time = (endTime - startTime);
System.out.println("The Worst Case Running Time for Singly Linked List
"+n+" is "+ Time +" ns");
}
}

The results which obtained after running the above code are below.

The Worst Case Running Time for Singly Linked List 1 is 1400 ns
The Worst Case Running Time for Singly Linked List 2 is 700 ns
The Worst Case Running Time for Singly Linked List 4 is 300 ns
The Worst Case Running Time for Singly Linked List 8 is 300 ns
The Worst Case Running Time for Singly Linked List 16 is 300 ns
The Worst Case Running Time for Singly Linked List 32 is 300 ns
The Worst Case Running Time for Singly Linked List 64 is 500 ns
The Worst Case Running Time for Singly Linked List 128 is 1000 ns
The Worst Case Running Time for Singly Linked List 256 is 1500 ns
The Worst Case Running Time for Singly Linked List 512 is 2900 ns
The Worst Case Running Time for Singly Linked List 1024 is 5900 ns
The Worst Case Running Time for Singly Linked List 2048 is 11500 ns
The Worst Case Running Time for Singly Linked List 4096 is 22600 ns
The Worst Case Running Time for Singly Linked List 8192 is 45000 ns

9|Page
According to the results the running times for single linked list size sizes are mentioned below.

Running Time to go for last node of List Sizes


Size Time (ns)
1 1400
2 700
4 300
8 300
16 300
32 300
64 500
128 1000
256 1500
512 2900
1024 5900
2048 11500
4096 22600
8192 45000

Then, the plot of Size vs Time was created using above data.

Single Linked List Sizes vs Running Time


50000
45000
40000
35000
Running Time (ns)

30000
25000
20000
15000
10000
5000
0
0 1000 2000 3000 4000 5000 6000 7000 8000 9000
Size

This proves that in the worst-case time taken to index a single linked list increasing linearly. Here, the
Big-O is also a “n” ( O[n] ). That time complexity confirms it.

10 | P a g e
Task 05
Developing a container for 10,000 integer data points using a doubly linked list.

A variant of a linked list called a doubly linked list makes it easier to go both ahead and backward than a
single linked list does.

package t5;

public class t5 {
Node head;
Node tail;
int size;

// Inserting New Public Constructor


public t5(){
head = null;
tail = null;
size = 0;
}

// Placing new now At front


public void push(int new_data){
Node new_node = new Node(new_data,null,null);
if(head == null){
head = new_node;
tail = head;
}else{
head.setLinkPrev(new_node);
new_node.setLinkNext(head);
head = new_node;
}
size++;
}

// Search for the element


public int GetAtPos(int k) {
Node temp = head;
int count = 0;
while (count!=k){
temp = temp.next;
count++;
}
return temp.data;
}

// Search the position of a given element in linked list


public int GetPosofval(int n) {
Node temp = head;
int count = 0;
while (temp.data!=n && temp.next != null){
temp = temp.next;
count++;
}
return count+1;
}

11 | P a g e
// Print Statements
public void display() {
System.out.print("Double linked list elements = ");
if (size == 0){
System.out.print("empty\n");
return;
}
if (head.getLinkNext() == null) {
System.out.println(head.getData() );
return;
}
Node ptr = head;
System.out.print(head.getData()+ " <> ");
ptr = head.getLinkNext();
while (ptr.getLinkNext() != null) {
System.out.print(ptr.getData()+ " <> ");
ptr = ptr.getLinkNext();
}
System.out.print(ptr.getData()+ "\n");
}

// Main Function
public static void main(String args[]) {
t5 x = new t5(); //Start with the empty list
int n=10000;
for(int i=0;i<n;i++) {
x.push(n-i);
}
x.display();
}
}

class Node {
int data;
Node prev;
Node next;
public Node() {
next = null;
prev = null;
data = 0;
}
public Node(int d, Node n, Node p) {
data = d;
next = n;
prev = p;
}

// Link to Next Node


public void setLinkNext(Node n) {
next = n;
}

// Link to Previous Node


public void setLinkPrev(Node p) {
prev = p;
}

// Get the Link to Next Node


public Node getLinkNext() {
return next;

12 | P a g e
}

// To get link to previous node


public Node getLinkPrev() {
return prev;
}

// To set the data to the node


public void setData(int d) {
data = d;
}

// To get data from node


public int getData() {
return data;
}
}

The results which obtained after running the above code are below.

Double linked list elements = 1 <> 2 <> 3 <> 4 <> 5 <> 6 <> 7 <> 8 <> 9 <>
10 <> 11 <> 12 <> 13 <> 14 <> 15 <> 16 <> 17 <> 18 <> 19 <> 20 <> 21 <> 22
<> 23…<>10000

Task 06
Finding the worst-case running times for indexing for the doubly linked list implementation

To find the worst-case running times for indexing, main section was modified as this.

public static void main(String args[]) {


for(int n=1;n<=10000;n=n*2) {
task5 x = new task5(); //Started with the empty list
for(int i=0;i<n;i++) {
x.push(n-i);
}

long startTime = System.nanoTime();


x.GetAtPos(n-1);
long endTime = System.nanoTime();
long Time = (endTime - startTime);
System.out.println("The Worst Case Running Time for Doubly Linked List
"+n+" is "+ Time +" ns");
}

13 | P a g e
The results which obtained after running the above code are below.
The Worst Case Running Time for the Doubly Linked List 1 is 1700 ns
The Worst Case Running Time for the Doubly Linked List 2 is 800 ns
The Worst Case Running Time for the Doubly Linked List 4 is 300 ns
The Worst Case Running Time for the Doubly Linked List 8 is 300 ns
The Worst Case Running Time for the Doubly Linked List 16 is 300 ns
The Worst Case Running Time for the Doubly Linked List 32 is 400 ns
The Worst Case Running Time for the Doubly Linked List 64 is 600 ns
The Worst Case Running Time for the Doubly Linked List 128 is 1100 ns
The Worst Case Running Time for the Doubly Linked List 256 is 1700 ns
The Worst Case Running Time for the Doubly Linked List 512 is 3200 ns
The Worst Case Running Time for the Doubly Linked List 1024 is 6700 ns
The Worst Case Running Time for the Doubly Linked List 2048 is 12900 ns
The Worst Case Running Time for the Doubly Linked List 4096 is 25500 ns
The worst Case Running Time for the Doubly Linked List 8192 is 50600 ns

According to the results the running times for double linked list size sizes are mentioned below.

Running Time to go for last node of List Sizes


Size Time (ns)
1 1700
2 800
4 300
8 300
16 300
32 400
64 600
128 1100
256 1700
512 3200
1024 6700
2048 12900
4096 25500
8192 50600

Then, the plot of Size vs Time was created using above data.

Double Linked List Sizes vs Running Time

60000

50000
Running Time (ns)

40000

30000

20000

10000

0
0 1000 2000 3000 4000 5000 6000 7000 8000 9000
Size

14 | P a g e
This proves that in the worst-case time taken to index a double linked list increasing linearly. Here, the
Big-O is also a “n” ( O[n] ). That time complexity confirms it.

When considering all three cases, using dynamic array structure is more efficient in terms of the
indexing operation. The Big-O notation justify it.

Array Type Big-O


Dynamic Array O (1)
Singly LinkedList O (n)
Doubly Linked List O (n)

15 | P a g e
DISCUSSION
The structure of the two data types, array and linked list, is where they diverge most. Each member of an
array has an associated index since they are index-based data structures. The linked list, on the other
hand, depends on references, and each node contains data as well as pointers to the element before it and
element after it. Basically, 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 not constrained to a certain size and can grow and shrink as needed during execution,
whereas an array has a set size and must be specified beforehand.

While a linked list is a type of data structure that holds a succession of entries connected to one another.
A linked list element has two fields. The actual value to be saved and processed is included in the Data
field, while the Link field is the other. The address of the following data item in the linked list is also
included in the link field. A pointer is the address used to reach a certain node.

These are the differences between Arrays and Linked Lists.

An array is a data structure that consists of a group of similar-type data elements, whereas a
linked list is a non-primitive data structure that consists of a group of unordered linked items
called nodes.
The items of the array are indexed, therefore, to access the fourth element you must provide the
variable's name in the square bracket along with its index or location. But to get to the fourth
entry in a linked list, you must start at the top and work your way down.
A linked list is much slower than an element array since it takes linear time to reach each item.
The insertion and deletion operations in arrays take a long time. However, these actions are
carried out quickly in linked lists.
Arrays have a set size. Linked lists, on the other hand, are dynamic and adaptable and can grow
and shrink in size.
Memory is allocated for arrays at build time, but for linked lists, it is done during runtime.
Linked lists store items randomly, while arrays store them in a sequential order.
Because the actual data is contained within the index of the array, less memory is needed. In
contrast, Linked Lists use more memory since they must store extra next and prior referencing
components.
Additionally, the array's use of memory is wasteful. In contrast, the array's memory usage is
effective.

A singly linked list is a set of nodes where each node has two fields ‘data’ and ‘link’. The ‘data’ field
stores actual piece of information and ‘link’ field is used to point to next node. A Doubly Linked
List contains an extra pointer, typically called previous pointer, together with next pointer and data
which are there in singly linked list.

Singly Doubly
16 | P a g e
SLL DLL
Contain 02 fields called data fields called ‘data Contain 03 fields called data fields called ‘data
field’ and ‘next link field’ field’, ‘previous link field’ and ‘next link field’
Only the next node link can be used to do the Either the previous node connection or the
traverse. This means that just one direction of following node link can be used to do the traverse.
traversal is available. As a result, traversal is feasible both ways
(forward and backward).
As it only contains 2 fields, the SLL uses less Due to its three fields, the DLL uses more memory
memory than the DLL. than the SLL.

If possible, utilize a singly linked list to execute Due to the fact that traversals can be done from
stacks. either the beginning or the end
We choose a single linked list when we don't need When better implementation is available, we favor
to execute any searching operations and wish to using doubly linked lists for searching.
conserve memory.
Memory usage is lower for a singly linked list Memory usage for the doubly linked list is higher
compared to a doubly linked list. than for the single linked list.

Reference

(Vaghani, n.d.) (Ismail, n.d.)

Ismail, S. (n.d.). Tech Differences. Retrieved from https://techdifferences.com/difference-between-array-


and-linked-list.html#KeyDifferences
Vaghani, R. (n.d.). Geek for Geeks. Retrieved from https://www.geeksforgeeks.org/difference-between-
singly-linked-list-and-doubly-linked-list/

17 | P a g e

You might also like