0% found this document useful (0 votes)
8 views7 pages

1.4 Linked List

lecture note of CSC508 data structure

Uploaded by

2023217748
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

1.4 Linked List

lecture note of CSC508 data structure

Uploaded by

2023217748
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

LINKED LIST

TOPIC 1: LIST

Prepared by: Nik Marsyahariani Nik Daud

Content

Linked list Implementation

Linked list Variation


• SinglyLinkedList
• Doubly LinkedList
• CircularLinkedList

1
Linked list Implementation

Object getFirst(){
if(size() > 0){
void insertAtFront(Object d){
current = head;
node temp = new node(d);
return head.data;
}else if(isEmpty()){
return null; head = current = temp;
} }else{
temp.next = head;
Object getNext(){ head = temp;
if(current.next!= null){ }
current = current.next; size++;
}
return current.data;
}else
return null;
}

LinkedList Variation

• LinkedList(singly Linkedlist)
• Double LinkedList
• Circular LinkedList

2
Singly LinkedList

• Node has ONLY one link pointed to another node

class node{
Object data;
node next;
data next
public node(Object data){
this.data=data;
next=null; node
}
}

Singly LinkedList

class linkedlist{
node head;
node current;
int size;
public linkedlist(){
head=current=null;
size = 1;
}
public void insertAtFront(Object data){
current = new node(data);
if(head == null)
head = current;
else{
current.next = head.next;
head = current;
}
size++;
}}

3
Limitation

▪ Limitations of a single-linked list include:


▪ Can insert a node only after a referenced node
▪ Can remove a node only if we have a reference to its predecessor
node
▪ Can traverse the list only in the forward direction

Doubly LinkedList

▪ The idea of doubly linked list is proposed to overcome the limitation


within singly linked list

4
Doubly LinkedList

• Every node:
– has a next reference variable and a back reference variable
– contains the address of the next node (except the last node)
– contains the address of the previous node (except the first node)

• Can be traversed in either direction

Doubly LinkedList

class node{ class doublylinkedlist{


node head;
Object data; node current;
node next; int size;
node prev; public doublylinkedlist(){
public node(Object head=current=null;
size = 1;
data){ }
this.data=data; public void insertAtFront(Object data){
prev=next=null; current = new node(data);
if(head == null)
} head = current;
else{
} current.next = head.next;
• head.next.prev = current;
head = current;
prev data next }
size++; current = null;
}}
node

10

5
Circular LinkedList

• A linked list in which the last node points to the first node is called
a circular linked list.
• In a circular linked list with more than one node, it is convenient to
make the reference variable first point to the last node of the list.

11

Circular LinkedList

• Advantages

• can traverse in forward or reverse direction even after you have


passed the last or first node.
• Can visit all the list elements from any starting point
• Can never fall off the end of a list.
• Disadvantage
• infinite loop.

12

6
CircularLinkedList

class circularlinkedlist{
...
public void insertAtBack(Object data){
node newNode= new node(data);
if(head == null){
head = newNode;
newNode.next = head;
else{
current = head;
for(int i=0;i<size-1;i++)
current = current.next;
• newNode.next = head;
• current.next = newNode;
}
...
}}

13

Summary

List Concepts
List in Collection Frameworks

Sequential list Concept


Differences with Array
ArrayList in Collection Frameworks
Application using ArrayList

Implementing User Defined Sequential List


Application using User Defined Sequential List

14

You might also like