You are on page 1of 12

ECSE502 Algorithms and Data Structures

Coursework 1

Module Leader: Mrs. Udayangi Perera


By: R.A.Thushara Kasun Ranawaka Student ID: 2010066 Group B

Coursework 1

Contents
Acknowledgment ................................................................................................................................................................. 1 Introduction ......................................................................................................................................................................... 1 What is a LINKED LIST? ................................................................................................................................................ 1 Why Linked Lists? ........................................................................................................................................................ 2 What is Big-O notion? .................................................................................................................................................. 3 What is a pseudo code?............................................................................................................................................... 3 What is an adjacency list? ........................................................................................................................................... 3 Q & A.................................................................................................................................................................................... 3 Describe in pseudo code and give the justified complexity in Big-O notation for the following operations:- ............... 3 Provide an implementation code in Java for the following operations:- ........................................................................ 6 Attachments ........................................................................................................................................................................ 9 References ......................................................................................................................................................................... 10

0|Page

Coursework 1 Acknowledgment.
Thank you very much everyone who help me to deliver this course work

Introduction
What is a LINKED LIST?
In computer science, a linked list is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of a datum and a reference(in other words, a link) to the next node in the sequence; more complex variants add additional links. This structure allows for efficient insertion or removal of elements from any position in the sequence.

A linked list whose nodes contain two fields: an integer value and a link to the next node. The last node is linked to a terminator used to signify the end of the list. Linked lists are among the simplest and most common data structures. They can be used to implement several other common abstract data structures, including stacks, queues, associative arrays, and symbolic expressions, though it is not uncommon to implement the other data structures directly without using a list as the basis of implementation. The principal benefit of a linked list over a conventional array is that the list elements can easily be inserted or removed without reallocation or reorganization of the entire structure because the data items need not be stored contiguously in memory or on disk. Linked lists allow insertion and removal of nodes at any point in the list, and can do so with a constant number of operations if the link previous to the link being added or removed is maintained during list traversal. On the other hand, simple linked lists by themselves do not allow random access to the data, or any form of efficient indexing. Thus, many basic operations such as obtaining the last node of the list (assuming that the last node is not maintained as separate node reference in the list structure), or finding a node that contains a given datum, or locating the place where a new node should be inserted may require scanning most or all of the list elements.

1|Page

Coursework 1
Why Linked Lists?

Linked lists and arrays are similar since they both store collections of data. The terminology is that arrays and linked lists store "elements" on behalf of "client" code. The specific type of element is not important since essentially the same structure works to store elements of any type. One way to think about linked lists is to look at how arrays work and think about alternate approaches. Here's a diagram to help you realize the main disadvantage of arrays but not Linked Lists:-

Another drawback of arrays is that if you delete an element from the middle and want no holes in your array (e.g. (1, 2, 4, null) instead of (1, 2, null, 4)), you will need to shift everything after the deleted element down in O(n) time. If you're trying to add an element somewhere other than the very end of an array, you will need to shift some elements towards the end by one (also O(n) time) to make room for the new element, and if you're writing an application which needs to perform well and needs to do these operations often, you should consider using Linked Lists instead. This should help you understand Linked Lists:-

2|Page

Coursework 1
What is Big-O notion?
Big O notation is used in Computer Science to describe the performance or complexity of an algorithm. Big O specifically describes the worst-case scenario, and can be used to describe the execution time required or the space used (e.g. in memory or on disk) by an algorithm.

What is a pseudo code?


In computer science and numerical computation, pseudo code is a compact and informal high-level description of the operating principle of a computer program or other algorithm. It uses the structural conventions of a programming language, but is intended for human reading rather than machine reading. Pseudo code typically omits details that are not essential for human understanding of the algorithm, such as variable declarations, system-specific code and some subroutines. The programming language is augmented with natural language descriptions of the details, where convenient, or with compact mathematical notation. The purpose of using pseudo code is that it is easier for humans to understand than conventional programming language code, and that it is an efficient and environment-independent description of the key principles of an algorithm. It is commonly used in textbooks and scientific publications that are documenting various algorithms, and also in planning of computer program development, for sketching out the structure of the program before the actual coding takes place.

What is an adjacency list?


In graph theory, an adjacency list is the representation of all edges or arcs in a graph as a list. If the graph is undirected, every entry is a set (or multiset) of two nodes containing the two ends of the corresponding edge; if it is directed, every entry is a tuple of two nodes, one denoting the source node and the other denoting the destination node of the corresponding arc.

Q&A
Describe in pseudo code and give the justified complexity in Big-O notation for the following operations:1) Initialisation of a data structure
public class Node value; next; public Node() Initialize value to zero Initialize next to null

3|Page

Coursework 1
public Node(x) Initialize value to x Initialize next to null

Big-O notation = O (4)

2) The SEARCH operation for searching particular number of this data structure
public void searchByValue(value) Initialize found to true Initialize colCount to zero for(i =0; i < 7; i++) Initialize colCount to zero for(j = colList[i]; j != null; j = j.next) found = true; if(j.value!=value) found = false; else print("Element found! \n" + value + "In list :" + i + "at position " + colCount); found = true; colCount++;

if(found == false) print("Value is not found")

Big-O notation = O (7)+(O (n)* O (n))

3) The DELETE operation for a particular number on this data structure


public void deleteValue(value,column) Initialize previous to null Initialize found to true for(i =0; i < 7; i++) if (i == column) for(current = colList[i]; current != null; current = current.next) if(current.value!=value) Initialize previous to current Initialize found to false else if(previous != null)

4|Page

Coursework 1
previous.next=current.next Initialize found to true else colList[i] = current.next Initialize found to true if(found == true) print("list is deleted") if(found == false) print("Unable to find list")

Big-O notation = O (11)+(O (n)* O (n))

4) The INSERT operation for given number and a given linked list on this data structure (no order is assumed)
public void insertAtHead(value,column) Node l = new Node(value) if(colList[column] == null) Initialize head to l Initialize colList[column] to l Else Initialize l.next to colList[column] Initialize head to l Initialize colList[column] to l

Big-O notation = O (6)

5) The DELETE operation for a particular linked list in this data structure
public void deleteList(column) Initialize found to true for(i =0; i < 7; i++) if (i == column) Initialize colList[i] to null Else Initialize found to false if(found == true) print("List deleted") if(found == false) print("Unable to find list")

Big-O notation = O (3)+O (n) 5|Page

Coursework 1
6) The INSERT for a particular linked list on this data structure(no order is assumed) Provide an implementation code in Java for the following operations:-

1) Initialisation of a data structure


package linkedlist; public class Node{ int value; Node next; public Node(){ value=0; next=null; } public Node(int x){ value = x; next = null; } }

package linkedlist; public class List{ Node head; Node[] colList = new Node[7]; public List(){ head = null; }

2) The SEARCH operation for searching particular number of this data structure
public void searchByValue(int value){ boolean found = true; int colCount = 0; for(int i =0; i < 7; i++) { colCount = 0; for(Node j = colList[i]; j != null; j = j.next){ found = true;

6|Page

Coursework 1
if(j.value!=value){ found = false; } else{ System.out.println("Element found! \n" + value + "In list :" + i + "at position " + colCount); found = true; } colCount++; } } if(found == false){ System.out.println("Value is not found"); } }

3) The DELETE operation for a particular number on this data structure


public void deleteValue(int value, int column){ Node previous = null; boolean found = true; for(int i =0; i < 7; i++) { if (i == column){ for(Node current = colList[i]; current != null; current = current.next){ if(current.value!=value){ previous =current; found = false; } else{ if(previous != null){ previous.next=current.next; found = true; } else{ colList[i] = current.next; found = true; } } } } } if(found == true) System.out.println("list is deleted"); if(found == false) System.out.println("Unable to find list"); }

7|Page

Coursework 1

4) The INSERT operation for given number and a given linked list on this data structure (no order is assumed)
public void insertAtHead(int value, int column){ Node l = new Node(value); if(colList[column] == null){ head = l; colList[column] = l; } else{ l.next = colList[column]; head = l; colList[column] = l; } } public void printList(int column){ System.out.print("List " + column + ":- "); for(Node x = colList[column]; x != null; x = x.next){ System.out.print(x.value + " , "); if(x.next == null || colList[column] == null) { System.out.print("End"); } } System.out.println(); }

5) The DELETE operation for a particular linked list in this data structure
public void deleteList(int column){ boolean found = true; for(int i =0; i < 7; i++) { if (i == column) { colList[i] = null; } else {found = false;} } if(found == true){ System.out.println("List deleted");

8|Page

Coursework 1
} if(found == false){ System.out.println("Unable to find list"); } }

6) The INSERT for a particular linked list on this data structure(no order is assumed)
public void insertList(){ }

Attachments

9|Page

Coursework 1 References

. 2011. . [ONLINE] Available at: http://cslibrary.stanford.edu/103/LinkedListBasics.pdf. [Accessed 03 November 2011].

Linked Lists Tutorial, Examples, and Java code. 2011. Linked Lists Tutorial, Examples, and Java code. [ONLINE] Available at:http://www.mycstutorials.com/articles/data_structures/linkedlists. [Accessed 03 November 2011].

Linked list - Wikipedia, the free encyclopedia. 2011. Linked list - Wikipedia, the free encyclopedia. [ONLINE] Available at: http://en.wikipedia.org/wiki/Linked_list. [Accessed 03 November 2011]. A Beginners Guide to Big O Notation Rob Bell. 2011. A Beginners Guide to Big O Notation Rob Bell. [ONLINE] Available at: http://rob-bell.net/2009/06/a-beginners-guide-to-big-o-notation/. [Accessed 03 November 2011].

Adjacency list - Wikipedia, the free encyclopedia. 2011. Adjacency list - Wikipedia, the free encyclopedia. [ONLINE] Available at:http://en.wikipedia.org/wiki/Adjacency_list. [Accessed 14 November 2011].

The End

10 | P a g e

You might also like