0% found this document useful (0 votes)
10 views11 pages

CPE6 Unit 4

The document discusses linked lists and their operations. It describes singly linked lists and how to implement an insert method in a singly linked list class. It also lists common linked list operations like insert, clear, append, and functions to traverse the list.

Uploaded by

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

CPE6 Unit 4

The document discusses linked lists and their operations. It describes singly linked lists and how to implement an insert method in a singly linked list class. It also lists common linked list operations like insert, clear, append, and functions to traverse the list.

Uploaded by

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

Computer Engineering - CPE6

Data
Structures and
Algorithms
LinkedList Operations

Week 4
Computer Engineering - CPE6

Contents of this presentation

Here’s what you’ll find in this presentation:

1.Singly Linked Lists


2.Operations in Linked Lists

Week 4
Computer Engineering - CPE6

Singly Linked List


Linked List data structure is one where each
node(element) has reference to the next node.

Week 4
Computer Engineering - CPE6

Singly Linked List


node1 node2 node3 node4

null

Week 4
Computer Engineering - CPE6

Singly Linked
List

node1 node2 node3 node4

null

A node in a singly linked list has the following properties: data


and next. data is the value for the linked list node, and next is a
pointer to another instance of SinglyLinkedListNode.

Week 4
Computer Engineering - CPE6

Let’s implement the


insert method in Singly
Linked List

Week 4
Computer Engineering - CPE6

Singly Linked list example


“insert”
Nodes: Terminal Output:

node1 node2
null node3
null null

Method:
insert(“node3”)
insert(“node1”)
insert(“node2”)

Week 4
Computer Engineering - CPE6

Steps in SLL insert(practice)


1. Create 2 classes: MainLL and SinglyLL. MainLL should contain the main method.

2. In SinglyLL class, declare the following fields:


o head and next which is of type SinglyLL.
o size with int data type and initialized it to 0;
o data with String data type.

3. Create an empty constructor without an implementation and a constructor that accepts a


parameter d with String data type. Assign d to data inside the constructor’s implementation.

4. Create an insert() method with public access modifier and void as return type. Accept a
parameter named value which is of type String.

Week 4
Computer Engineering - CPE6

Steps in SLL insert(practice)


5. Inside the insert() method check if head is null. If yes, assign to head an instance of
SinglyLL with the value inside the constructor. If not, check if head.next is null.
5.1. If head.next is null, assign to head.next an instance of SinglyLL with the value inside the constructor
5.2. If head.next is not null, call setNext() method and pass head.next and value as parameter.

6. Create a setNext() method with public access modifier and void as return type. Accept a
parameter named sllNext which is of type SinglyLL and v which is of type String.

7. Inside setNext(), check if sllNext.next is null.


7.1. If sllNext.next is null, assign to sllNext.next an instance of SinglyLL with the v inside the constructor.
7.2. If sllNext.next is not null, call setNext() method and pass sllNext.next and v as parameter.

8. Create a getValue() method with public access modifier and String as return type. Return
data.

Week 4
Computer Engineering - CPE6

List ADT
insert() – Insert an element at the current location.
clear() – Remove all elements in the list.
append() – Insert an element at the end of the list.
moveToStart() – Set the current location to the start of the list.
moveToEnd() – Set the current location to the end of the list.
length() – Returns the number of elements in the list.
currPos() – Returns the current position in the list.
moveToPos(int pos) – set the current position to “pos”.
getValue() – returns the value of the current position.
next() – Move to the next node.
prev() – Move to the previous node.

Week 3
Computer Engineering - CPE6

THANKS!
DO YOU HAVE ANY QUESTIONS?

Louievic Samortin Sancon

Week 4

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy