Download Implementing Singly Linked List in Java and more Study Guides, Projects, Research Data Structures and Algorithms in PDF only on Docsity!
ASSIGNMENT 1 FRONT SHEET
Qualification BTEC Level 5 HND Diploma in Computing
Unit number and title Unit 19: Data Structures and Algorithms
Submission date 12/08/2022 Date Received 1st submission
Re-submission Date 12/12/2022 Date Received 2nd submission
Student Name Le Trung Tai Student ID GBD
Class GCD0905 Assessor name Pham Thanh Son
Student declaration
I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism. I understand that
making a false declaration is a form of malpractice.
Student’s signature Tai
Grading grid
P1 P2 P3 M1 M2 M3 D1 D
Summative Feedback: Resubmission Feedback:
Grade: Assessor Signature: Date:
Internal Verifier’s Comments:
IV Signature:
I. Abstract data type (P1)
1. Definition
Definition:
An abstract data type is an abstraction of a data structure that provides only the interface to which the data
structure must adhere. The interface does not give any specific details about something should be implemented
or in what programming language.
In other words, we can say that abstract data types are the entities that are definitions of data and operations but
do not have implementation details. In this case, we know the data that we are storing and the operations that
can be performed on the data, but we don't know about the implementation details. The reason for not having
implementation details is that every programming language has a different implementation strategy for example;
a C data structure is implemented using structures while a C++ data structure is implemented using objects and
classes.
Figure 1 ADT
For example, a List is an abstract data type that is implemented using a dynamic array and linked list. A queue is
implemented using linked list-based queue, array-based queue, and stack-based queue. A Map is implemented
using Tree map, hash map, or hash tables.
The common ways to represent ADT
Linked 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.
Figure 2 Linked list
The List ADT Functions is given below:
o get() – Return an element from the list at any given position.
o insert() – Insert an element at any position of the list.
o remove() – Remove the first occurrence of any element from a non-empty list.
o removeAt() – Remove the element at a specified location from a non-empty list.
o replace() – Replace an element at any position by another element.
o size() – Return the number of elements in the list.
o isEmpty() – Return true if the list is empty, otherwise return false.
o isFull() – Return true if the list is full, otherwise return false.
Stack
A stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it
behaves like a real-world stack, for example – a deck of cards or a pile of plates, etc.
A real-world stack allows operations at one end only. For example, we can place or remove a card or plate from
the top of the stack only. Likewise, Stack ADT allows all data operations at one end only. At any given time, we
can only access the top element of a stack.
This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here, the element which is placed
(inserted or added) last, is accessed first. In stack terminology, insertion operation is called PUSH operation and
removal operation is called POP operation.
o poll() – Remove and return the first element of the queue, if the queue is not empty.
o peek() – Return the element of the queue without removing it, if the queue is not empty.
o size() – Return the number of elements in the queue.
o isEmpty() – Return true if the queue is empty, otherwise return false.
o isFull() – Return true if the queue is full, otherwise return false.
Arraylist
Arraylist class implements List interface and it is based on an Array data structure. It is widely used because of the
functionality and flexibility it offers. ArrayList in Java, is a resizable-array implementation of the List interface. It
implements all optional list operations and permits all elements, including null. Most of the developers choose
Arraylist over Array as it’s a very good alternative of traditional java arrays.
Figure 5 ArrayList representation
Method of Arraylist:
o add(element) – This method is used to append a specific element to the end of a list.
o add(index, element) -- This method is used to insert a specific element at a specific position index in a list.
o get(index) -- Returns the element at the specified position in this list.
o set(index, element) -- Replaces the element at the specified position in this list with the specified element.
o remove(index) -- Removes the element at the specified position in this list.
o size() -- Returns the number of elements in this list.
o indexOf(element) -- The index the first occurrence of a specific element is either returned, or -1 in case
the element is not in the list.
o contains(element) -- Returns true if this list contains the specified element.
o isEmpty() -- Returns true if this list contains no elements.
- Examples for Linked List
A linked list is a sequence of data structures, which are connected together via links.
Linked List is a sequence of links which contains items. Each link contains a connection to another link. Linked list
is the second most-used data structure after array. Following are the important terms to understand the concept
of Linked List.
o Link − Each link of a linked list can store a data called an element.
o Next − Each link of a linked list contains a link to the next link called Next.
o LinkedList − A Linked List contains the connection link to the first link called First.
Linked List Representation:
Linked list can be visualized as a chain of nodes, where every node points to the next node.
Figure 6 Linked List representation
As per the above illustration, following are the important points to be considered.
o Linked List contains a link element called first.
o Each link carries a data field(s) and a link field called next.
o Each link is linked with its next link using its next link.
o Last link carries a link as null to mark the end of the list.
private Node head; private Node tail; private int size; private static class Node{ public Node next; E element; public Node(E element) { this(element, null); } public Node(E element, Node next){ this.element = element; this.next = next; } }
Basic Operations
Following are the basic operations supported by a list.
Insertion − Adds an element at the beginning of the list.
public void addElement(int index, E element){ checkIndex(index); Node newNode = new Node<>(element) ; Node current = this.head; int i = 0 ; while (i<=index) { if (i == index - 1 ) { Node nextNode = current.next; current.next = newNode; newNode.next = nextNode; break; } else {
current = current.next; i++; } } size++; } SingleLinkedList employees = new SingleLinkedList<>(); employees.addFirst(new Employee( 2 , "tan", 21 , 9 )); employees.addFirst(new Employee( 1 , "tai", 20 , 10 )); employees.addLast(new Employee( 3 , "nam", 22 , 8 )); employees.addLast(new Employee( 4 , "tuan", 19 , 8 )); employees.addElement( 2 , new Employee( 5 , "nhi", 17 , 15 )); System. out .println("List employees: "); employees.print();
Adds an element at the last of the list
Figure 7 Insertion Linked List illustration @Override public void addLast(E element) { Node newNode = new Node<>(element) ; if (size== 0 ) { head = tail= newNode; size++; } else { tail.next = newNode; tail = newNode; size++;
System. out .println(employees.removeFirst()); System. out .println("List employees: "); employees.print();
Deletes an element at the between of the list.
public E deleteElement(int index){ checkIndex(index); E deleteNode = null; if ( size == 2 ){ if (index== 1 ) deleteNode = removeFirst(); else deleteNode = removeLast(); } else{
Node current = this.head; int i = 0 ; while (i<size) { if (i == index- 1 ) { deleteNode = current.next.element; current.next = current.next.next; break; } else { current = current.next; i++; } } } size--; return deleteNode; } SingleLinkedList employees = new SingleLinkedList<>(); employees.addFirst(new Employee( 2 , "tan", 21 , 9 )); employees.addFirst(new Employee( 1 , "tai", 20 , 10 )); employees.addLast(new Employee( 3 , "nam", 22 , 8 )); employees.addLast(new Employee( 4 , "tuan", 19 , 8 )); employees.addElement( 2 , new Employee( 5 , "nhi", 17 , 15 )); employees.print(); System. out .println(employees.deleteElement( 2 )); System. out .println("List employees: "); employees.print();
Deletes an element at the last of the list.
Figure 8 Deletion Linked List illustration
if(result.toString() == ""){ System. out .println("Null"); } else System. out .println(result.toString()); }
Search − Searches an element using the given key.
II. ADT usages
1. Application of Stack in memory (P2)
How the memory is organized
Memory is physically organized as a large number of cells that are capable of storing one bit each. Logically they
are organized as groups of bits called words that are assigned an address. Data and instructions are accessed
through these memory address.
Figure 9 computer system memory
For better understanding, when an operating system runs a program, it first loads the program into main
memory. Memory is used both for the program's machine instructions and for the data that the program
uses. The program divided its memory into regions that performed specific program functions. Although
this memory management technique is now obsolete, having been replaced by paged memory,
programs continue to organize their memory based on the functional units illustrated here.
Paged memory computers manage memory dynamically, compare the amount of memory allocated to a
program is allowed to increase and decrease as the program's needs change. Memory is allocated to the
program and reclaimed by the OS in fixed-size chunks called pages. When the OS loads a program on a
paged-memory computer, it initially allocates a minimal number of pages to the program and allocates
additional memory as needed. Machine code and data that are not immediately needed are not loaded,
and pages storing machine code and data not used recently may be returned to the OS.
Figure 10 Stack
record for plus_one is discarded before main() proceeds. The code then assigns the return value of 1 to
result. Figure 11 illustrates the activation records up to this point.
Figure 11 Call stack
The program then proceeds to call plus_two(result). First, result is evaluated to obtain the value 1. Then
an activation record is created for plus_two(), with space for its parameter x. Observe that this activation
record is located in memory where the previous activation record for plus_one() was – the latter is no
longer in use, so the memory can be reused. After the new activation record is created, the parameter x
is initialized with the argument value 1. Then the program runs the body of plus_two().
The body of plus_two() in turn calls plus_one(x + 1). This evaluates x + 1 to obtain the value 2, creates an
activation record for plus_one(), initializes the value of x in the new activation record to be 2, and then
runs the body of plus_one(). The state of memory at this point is shown in Figure 12.
Figure 12 Call Stack 2
Observe that the new activation record for plus_one() is distinct from the previous one – each invocation
of a function gets its own activation record. In addition, there are now two variables x in the program.
Within the scope of plus_one(), x refers to the object located in the activation record for plus_one(), and
its value is 2. Within plus_two(), x refers to the object in the activation record for plus_two(), and its
value is 1.
The invocation of plus_one() computes 3 as its return value, so that value replaces the call to plus_one(),
and the activation record for plus_one() is discarded. plus_two() returns that same value 3, so the value
3 replaces the call to plus_two() in main(), and the activation record for plus_two() is discarded. Then
main() proceeds to assign the value 3 to result and print it out. Finally, when main() returns, its activation
record too is discarded.
- Application of an ADT (P3)
Scenario
My friend is currently working as an HR manager for a company. He asked me to write an application to
help manage that job faster and easier. I was thinking of using ADT to build the app. And I chose
ArrayList for that because of its convenience: ArrayList can grow and shrink automatically based on the
addition and removal of elements, ArrayList can contain duplicate elements, ArrayList maintains
insertion order, which means elements appear in the same order in which they were inserted, ArrayList
is asynchronous, however we can make it synchronized.
ArrayList
Definition:
ArrayList is a part of the Java collection framework and it is a class of java.util package. It provides us
with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in
programs where lots of manipulation in the array is needed. This class is found in java.util package. The
main advantages of ArrayList are, if we declare an array then it’s needed to mention the size but in
ArrayList, it is not needed to mention the size of ArrayList if you want to mention the size then you can
do it.
Figure 13 ArrayList
Illustration: