1. Trang chủ
  2. » Công Nghệ Thông Tin

Singly linked lists

30 390 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 30
Dung lượng 395 KB

Nội dung

next element next next next element element element Baltimore Rome Seattle Toronto link: The next reference inside a node is a link or pointer to another node... head next element next

Trang 1

Singly Linked Lists

Trang 2

Definition: A linked list is a colleciton of nodes that

together form a linear ordering

node: A compound object that stores a reference to an

element and a reference, called next, to another node

Reference to another node

Reference to an element

next Element Node

Trang 3

next

element

next next next

element element element

Baltimore Rome Seattle Toronto

link: The next reference inside a node is a link or pointer to

another node

Trang 4

We can start from a given node, and move from it to the next

and so on This is called link hopping or pointer hopping

head

next

element

next next next

element element element

Baltimore Rome Seattle Toronto

Trang 5

head: The first node of a linked list

tail: The last node of a linked list - it has a null next reference

head

next

element

next next next

element element element

Baltimore Rome Seattle Toronto

Such a linked list is called a singly linked list

tail

Trang 6

pointer to anext node

pointer to

an elementnode

Illustration of a linked list in memory:

Trang 7

pointer to anext node

pointer to

an elementnode

Trang 8

pointer to anext node

pointer to

an elementnode

Trang 9

pointer to anext node

pointer to

an elementnode

Trang 11

Singly Linked Lists and Arrays

Trang 12

Class Node

Here is an implementation of nodes in Java:

public class Node {

private Object element;

private Node next;

Trang 13

Object getElement() {

return element }

Trang 14

Insertion of an Element at the

Head

Before the insertion:

head

Trang 15

Have a new node:

head

next element

The following statement is not correct:

x.element = new String(“Baltimore”));

Trang 16

After the insertion:

head

next element

Trang 17

Deleting an Element at the Head

Before the deletion:

head

next element

next element element elementBaltimore Rome Seattle Toronto

Trang 18

Remove the node from the list:

head

next element

Trang 19

After the deletion:

head

Trang 20

Insertion of an Element at the

Tail

Before the insertion:

head

tail

Trang 21

Have a new node:

head

next

element

next next next

element element element

Baltimore Rome Seattle Toronto

tail

Node x = new Node( );

x.setElement(new String(“Baltimore”));x.setNext(null);

tail.setNext(x);

tail = x;

Trang 22

After the insertion:

head

next

element

Baltimore Rome Seattle Toronto

tail

Trang 23

How to keep “head” and “tail”?

public class Head_and_Tail {

Node head;

Node tail;

Head_and_Tail(Node x, Node y) {

head = x;

tail = y;

}}

Trang 24

How to keep “head” and “tail”?

public class GeneratingList {

Node head = null;

Node tail = null;

Public Head_and_Tail linked_list () {

Node x = null;

for (int i = 0; i < 10; i++)

{x = new Node(); x.setElement(new Integer(i));

if (i == 0 ) {x.setNext(null); tail = x;} else x.setNext(head);

head = x;

}

return new Head_and_Tail(head, tail);}

}

Trang 25

Deleting an Element at the Tail

Deletion of an element at the tail of a singly linked list takes more effort

The difficulty is related with the fact that the last node does not have a link to the previous node which will become the new tail of the list

Trang 26

Scott: Who is McFee?

Gary: I don’t know

Trang 27

Before the deletion:

head

next

element

Baltimore Rome Seattle Toronto

tail

Trang 28

Remove the node: How can we find the new tail?

next

element

next next next

element element element

Baltimore Rome Seattle Toronto

should be removed

Trang 29

How to insert a new node in the middle of a singly linkedlist?

How to remove a node which in the middle of a singlylinked list?

Trang 30

Data Structure Exercises 5.1

Write a Java program to create a linked list as shown below

Φ

… …

Ngày đăng: 24/10/2014, 01:17

TỪ KHÓA LIÊN QUAN

w