Data Structures and Algorithms in Java, Study notes of Mathematical Methods

An overview of various data structures and algorithms in the context of java programming. It covers the fundamental concepts of lists, including array-based lists, singly linked lists, circular lists, and doubly linked lists. The characteristics, implementation, and operations of these different list data structures. It also introduces the linkedlist and arraylist classes from the java.util package, which provide built-in list functionality in java. The document aims to equip readers with a solid understanding of list data structures and their practical applications in software development. It includes detailed explanations, code examples, and illustrations to facilitate learning and understanding of these important data structures and algorithms.

Typology: Study notes

2022/2023

Uploaded on 09/19/2023

thieu-duc-dung-qp0938
thieu-duc-dung-qp0938 🇻🇳

1 document

1 / 23

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures and Algorithms in Java 1/23
1. List Data Structure
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download Data Structures and Algorithms in Java and more Study notes Mathematical Methods in PDF only on Docsity!

1. List Data Structure

Objectives

2

  • (^) Describe List structures
  • (^) Describe self-referential structures
  • (^) Explain types of linked lists
  • (^) Singly Linked Lists
  • (^) Circular Lists
  • (^) Doubly Linked Lists
  • (^) Lists in java.util

Drawbacks of Arrays

4

  • (^) Array is a very useful data structure in many

situations. However, it has some important

limitations:

  • (^) They require size information for creation
  • (^) Inserting an element in the middle of an array leads to moving

other elements around

  • (^) Deleting an element from the middle of an array leads to moving

other elements around

  • (^) Other data structures are more efficient in such

situations.

Self-Referential Structures

Many dynamic data structures are implemented through the use of a self-

referential structure.

A self-referential structure is an object, one of whose elements is a

reference to another object of its own type.

With this arrangement, it is possible to create ‘chains’ of data of varying

forms:

DataNode

Employee info;

DataNode next;

trees Linked lists

Employee

String name;

int age;

DataNode

Employee info;

DataNode left;

DataNode right;

Self-Referential Structures

Singly Linked Lists

7

A singly linked list is a list whose node includes two

datafields: info and next. The info field is used to store

information, and this is important to the user. The next

field is used to link to its successor in this sequence

The following image depicts a simple integer linked list.

head (^) tail

Singly Linked List

Singly Linked List Implementation

8

class Node

{int info;

Node next;

Node() {}

Node(int x, Node p)

{info=x;next=p;

void add(int x)

{ if(isEmpty())

head=tail=new Node(x,null);

else

{Node q =new Node(x,null);

tail.next=q; tail=q;

void traverse()

{Node p=head;

while(p!=null)

{System.out.print(" " + p.info);

p=p.next;

System.out.println();

Node search(int x) {...}

void dele(int x) {...}

class MyList

{Node head,tail;

MyList()

{head=tail=null;}

boolean isEmpty()

{return(head==null);

void clear()

{head=tail=null;

10

Singly Linked Lists - 2

Inserting a new node at the end of a list

Inserting a new node at the end of a Singly Linked List

Singly Linked Lists - 3

Deleting a node from the beginning of a list

11

Deleting a node from the beginning of a Singly Linked List

Circular Lists - 1

13

  • (^) A circular list is when nodes form a ring: The

list is finite and each node has a successor

Circular SIngly Linked List

Circular Lists - 2

Inserting nodes

14

Inserting nodes at the front of a circular singly linked list (a) and at its end (b)

Doubly Linked Lists - 1

16 • In a doubly linked list, each

node has two reference

fields, one to the successor

and one to the predecessor

class Node

{int info;

Node prev,next;

Node() {}

Node(int x, Node p, Node q)

{info=x;prev=p; next=q;

class MyList

{Node head,tail;

MyList() {head=tail=null;}

boolean isEmpty()

{return(head==null); }

void clear() {head=tail=null;}

void add(int x)

{if(isEmpty())

head=tail=new Node(x,null,null);

else

{Node q =new Node(x,tail,null);

tail.next=q;

tail=q;

Doubly Linked List

Doubly Linked Lists - 2

Adding a new node at the end

17

Adding new node at the end of Doubly Linked List

Lists in java.util - LinkedList class

boolean add( E o) Appends the specified element to the end of this list.

void addFirst( E o) Inserts the given element at the beginning of this list.

void addLast( E o) Appends the given element to the end of this list.

void clear() Removes all of the elements from this list.

E get(int index) Returns the element at the specified position in this list.

E getFirst() Returns the first element in this list.

E getLast() Returns the last element in this list.

E remove(int index) Removes the element at the specified position in

this list.

E removeFirst() Removes and returns the first element from this list.

E removeLast() Removes and returns the last element from this list.

int size() Returns the number of elements in this list.

Object[] toArray() Returns an array containing all of the elements in this list in

the correct order.

Lists in java.util

LinkedList class example

import java.util.*;

class Node

{ String name;

int age;

Node() {}

Node(String name1, int age1)

{ name=name1; age=age1;

void set(String name1, int age1)

{ name=name1; age=age1;

public String toString()

{ String s = name+" "+age;

return(s);

class Main

public static void main(String [] args)

LinkedList t = new LinkedList();

Node x; int n,i;

x = new Node("A01",25); t.add(x);

x = new Node("A02",23); t.add(x);

x = new Node("A03",21); t.add(x);

for(i=0;i<t.size();i++)

System.out.println(t.get(i));