Вы находитесь на странице: 1из 4

Anjie V.

Bogayao 09/17/2019

SFIT-2B CC104

Assignment No. 1

Linked List in Java

Function

Linked List are linear data structures where the elements are not stored in contiguous locations and every
element is a separate object with a data part and address part. The elements are linked using pointers
and addresses. Each element is known as a node. Due to the dynamicity and ease of insertions and
deletions, they are preferred over the arrays.

It also has few disadvantages like the nodes cannot be accessed directly instead we need to start from the
head and follow through the link to reach to a node we wish to access. To store the elements in a linked
list we use a doubly linked list which provides a linear data structure and also used to inherit an abstract
class and implement list and deque interfaces.

In Java, LinkedList class implements the list interface. The LinkedList class also consists of various
constructors and methods like other java collections.

Constructors for Java LinkedList:

LinkedList(): Used to create an empty linked list.

LinkedList(Collection C): Used to create a ordered list which contains all the elements of a specified
collection, as returned by the collection’s iterator.

Advantages

 Dynamic Data Structure

Linked list is a dynamic data structure so it can grow and shrink at runtime by allocating and deallocating
memory. So, there is no need to give initial size of linked list.

 Insertion and Deletion

Insertion and deletion of nodes are really easier. Unlike array here we don’t have to shift elements after
insertion or deletion of an element. In linked list we just have to update the address present in next
pointer of a node.

 No Memory Wastage

As size of linked list can increase or decrease at run time so there is no memory wastage. In case of array
there is lot of memory wastage, like if we declare an array of size 10 and store only 6 elements in it then
space of 4 elements are wasted. There is no such problem in linked list as memory is allocated only when
required.

 Implementation

Data structures such as stack and queues can be easily implemented using linked list.
import java.util.*;
public class Test
{
public static void main(String args[])
{
// Creating object of class linked list
LinkedList<String> object = new LinkedList<String>();
// Adding elements to the linked list
object.add("A");
object.add("B");
object.addLast("C");
object.addFirst("D");
object.add(2, "E");
object.add("F");
object.add("G");
System.out.println("Linked list : " + object);
// Removing elements from the linked list
object.remove("B");
object.remove(3);
object.removeFirst();
object.removeLast();
System.out.println("Linked list after deletion: " + object);
// Finding elements in the linked list
boolean status = object.contains("E");
if(status)
System.out.println("List contains the element 'E' ");
else
System.out.println("List doesn't contain the element 'E'");
// Number of elements in the linked list
int size = object.size();
System.out.println("Size of linked list = " + size);
// Get and set elements from linked list
Object element = object.get(2);
System.out.println("Element returned by get() : " + element);
object.set(2, "Y");
System.out.println("Linked list after change : " + object);
}
}
Output:
Linked list : [D, A, E, B, C, F, G]
Linked list after deletion: [A, E, F]
List contains the element 'E'
Size of linked list = 3
Element returned by get() : F
Linked list after change : [A, E, Y]
Anjie V. Bogayao 09/17/2019

SFIT-2B CC104

Assignment No. 2

Stacks in Java

Function

The Stack is a generic data structure which represents a LIFO (last in, first out) collection of objects
allowing for pushing/popping elements in constant time. It is a collection that is based on the last-in-
first-out (LIFO) policy. By tradition, we name the stack insert method push() and the stack remove
operation pop(). Stack is a linear Data Structure which follow a particular order in which operations are
performed. The order can be FILO(First in last Out) or it may be LIFO. So in simple, In order to design
Stack we generally use Array(if size is fixed) else Linked List and then we perform operations of stack.
The operations are:

Push : It is used to insert a element in stack.

Pop : It is used to retrieve a element from stack.

Peek : Just to know what is top of stack.

empty : To know whether stack is empty or not.

Advantages

 Easy to started
 Less Hardware Requirement
 Cross- Platform
 Stacks provide a unique way to work with contiguous memory. Very similar to Arrays and Lists,
Stacks provide a way for users to access different pieces of contiguous data in a Last In First Out
manner.

// Java code for stack implementation


import java.io.*;
import java.util.*;
class Test
{
// Pushing element on the top of the stack
static void stack_push(Stack<Integer> stack)
{
for(int i = 0; i < 5; i++)
{
stack.push(i);
}
}
// Popping element from the top of the stack
static void stack_pop(Stack<Integer> stack)
{
System.out.println("Pop :");
for(int i = 0; i < 5; i++)
{
Integer y = (Integer) stack.pop();
System.out.println(y);
}
}
// Displaying element on the top of the stack
static void stack_peek(Stack<Integer> stack)
{
Integer element = (Integer) stack.peek();
System.out.println("Element on stack top : " + element);
}
// Searching element in the stack
static void stack_search(Stack<Integer> stack, int element)
{
Integer pos = (Integer) stack.search(element);
if(pos == -1)
System.out.println("Element not found");
else
System.out.println("Element is found at position " + pos);
}
public static void main (String[] args)
{
Stack<Integer> stack = new Stack<Integer>();
stack_push(stack);
stack_pop(stack);
stack_push(stack);
stack_peek(stack);
stack_search(stack, 2);
stack_search(stack, 6);
}
}
Output:
Pop :
4
3
2
1
0
Element on stack top : 4
Element is found at position 3
Element not found

Вам также может понравиться