القوائم المترابطة linked list باستخدام لغة جافا

28
using Java 2015 Data Structure Prepared by: Mahmoud Rafeek Al-farra in Java 6. Linked List

Upload: mahmoud-alfarra

Post on 08-Jan-2017

401 views

Category:

Education


3 download

TRANSCRIPT

Page 1: القوائم المترابطة Linked List باستخدام لغة جافا

using Java

2015

Data Structure Prepared by: Mahmoud Rafeek Al-farra

in Java

6. Linked List

Page 2: القوائم المترابطة Linked List باستخدام لغة جافا

mfarra.cst.ps www.fb.com/MahmoudRFarra

Contents

The Linked List Class

The linked List operations

Introduction

Variations of Linked List

Page 3: القوائم المترابطة Linked List باستخدام لغة جافا

Introductionmfarra.cst.ps www.fb.com/MahmoudRFarra

A list can grow or shrink dynamically.

If your application does not require the insertion

or deletion of elements, an array is the most

efficient data structure.

Page 4: القوائم المترابطة Linked List باستخدام لغة جافا

Introductionmfarra.cst.ps www.fb.com/MahmoudRFarra

A linked list is a collection of objects called

nodes.

Each node is linked to its successor node in the

list using a reference to the successor node.

A node is made up of a field for storing data and

the field for the node reference.

The reference to another node is called a link.

Page 5: القوائم المترابطة Linked List باستخدام لغة جافا

Items of Linked List mfarra.cst.ps www.fb.com/MahmoudRFarra

A list or linked list composed of number of objects of a class, if you want to develop a list of employees, you will insert many objects of employees to your list, and so on …

Ahmad25

yearDevelope

r

Ali28

yearLecture

r

Ola27

yearDesigne

r

....

Page 6: القوائم المترابطة Linked List باستخدام لغة جافا

Items of Linked List mfarra.cst.ps www.fb.com/MahmoudRFarra

Self- referential class is a class which contains an object of it self.

These concept uses to link objects (Nodes) with others.

class Node { private int data; private Node next; public Node( int d ) {// some code } }

next object is an object of Node class, so this class is a self referential class

Page 7: القوائم المترابطة Linked List باستخدام لغة جافا

Application: List of cars mfarra.cst.ps www.fb.com/MahmoudRFarra

CarClass

ListCarClass

CarSystemClass

In this class we will create an object of ListCar class, and then manipulates the list using all operations.

The class of linked list members and operation, this class simulate the list of any thing, the object of this class will be created and used in the class of CarSystem.

A self referential class called Car, contains the attributes and behavior of any car.

1

2

3

Page 8: القوائم المترابطة Linked List باستخدام لغة جافا

The self-referential Classmfarra.cst.ps www.fb.com/MahmoudRFarra

1. class Car2. {3. public int id;4. public Car next;5. public Car()6. {7. id = 0;8. location = "Gaza";9. type = "Private";10. }11. }

Self Study: To convert it to generic class, go to page 907 in our text book. [ind, N.W]

Page 9: القوائم المترابطة Linked List باستخدام لغة جافا

The List Classmfarra.cst.ps www.fb.com/MahmoudRFarra

1. class ListCar2. {3. private int length ; // instance variable to count number of elements in the list4. public Car head; // Object will point to the first element in the list5. public ListCar()6. {7. lenght = 0;8. head = null; // initially the head points to nothing 9. }10. // Here the manipulated operation will be developed11. }

You can declare another object called “tail” which points to the last node of linked list.

Page 10: القوائم المترابطة Linked List باستخدام لغة جافا

Create object of List Carmfarra.cst.ps www.fb.com/MahmoudRFarra

1. static void Main(string[] args)2. {3. ListCar list1 = new ListCar();4. }

This objet of list will be manipulated using the operations.

Page 11: القوائم المترابطة Linked List باستخدام لغة جافا

Add new node at the firstmfarra.cst.ps www.fb.com/MahmoudRFarra

Data 1

Head

Data 2

Data 3

Data n…..

null

New data

1

X2

Page 12: القوائم المترابطة Linked List باستخدام لغة جافا

Add new node at the firstmfarra.cst.ps www.fb.com/MahmoudRFarra

1. class ListCar {2. private int lenght ; 3. public Car head; 4. public ListCar() {5. lenght = 0;6. head = null; } 7. public void addAtFront(Car inserted) {8. Car newc = new Car();9. newc = inserted;10. newc.next = head;11. lenght++;12. head = newc; }13. }

Note: all codes in the PPT files for this course are not complete, so you must depend on the practical files of lectures.The current codes are only to explain the main idea

Page 13: القوائم المترابطة Linked List باستخدام لغة جافا

Add new node at the lastmfarra.cst.ps www.fb.com/MahmoudRFarra

Data 1

Front

Data 2

Data 3

Data n…..

null

New data

2

null

1Back

X 1Back

X 1Back

X 1Back

X

Page 14: القوائم المترابطة Linked List باستخدام لغة جافا

Add new node at the lastmfarra.cst.ps www.fb.com/MahmoudRFarra

1. class ListCar {2. ….3. public void addAtback(Car inserted) {4. Car newc = new Car();5. Car back = head;6. while (back.next != null)7. back = back.next;8. back.next = newc;9. newc.next = null;10. length ++;11. }12. }

Self Study: When the list is empty, What we will do? [ind, N.W]

Page 15: القوائم المترابطة Linked List باستخدام لغة جافا

Delete the first elements mfarra.cst.ps www.fb.com/MahmoudRFarra

Data 1

Head

Data 2

Data 3

Data n…..

null

X

Self Study: Develop our car Project to delete a car from the first. [ind, N.W]

Page 16: القوائم المترابطة Linked List باستخدام لغة جافا

Delete the last elements mfarra.cst.ps www.fb.com/MahmoudRFarra

Data 1

Front

Data 2

Data 3

Data n…..

null

Backcurrent

×1×2

Self Study: Develop our car Project to delete a car from the last. [ind, N.W]

Page 17: القوائم المترابطة Linked List باستخدام لغة جافا

Insert a new element at position kmfarra.cst.ps www.fb.com/MahmoudRFarra

Data 1

Head

Data 2

Data 3

Data n…..

null

New data

1

Will be inserted at position 2 (the third element)

0 1 2 n2×

current

Self Study: Develop our car Project to insert a car at position x. [ind, N.W]

Page 18: القوائم المترابطة Linked List باستخدام لغة جافا

Insert a new element based on an ordermfarra.cst.ps www.fb.com/MahmoudRFarra

Data 1

Head

Data 2

Data 3

Data n…..

null

New data

1Example: Will be inserted after the value x

0 1 2 n2×

currentFront

Self Study: Develop our car Project to insert a car at suitable position [ind, N.W]

Page 19: القوائم المترابطة Linked List باستخدام لغة جافا

Delete an element from position mfarra.cst.ps www.fb.com/MahmoudRFarra

Data 1

Head

Data k-1 Data k Data

n

null

Current

Data k+1

1

× ×Front

Self Study:Develop our car Project to delete a car from position x [ind, N.W]

Page 20: القوائم المترابطة Linked List باستخدام لغة جافا

Linked List Classmfarra.cst.ps www.fb.com/MahmoudRFarra

LinkedList is a linked list implementation of the

List interface.

In addition to implementing the List interface,

this class provides the methods for retrieving,

inserting, and removing elements from both ends

of the list.

Page 21: القوائم المترابطة Linked List باستخدام لغة جافا

Linked List Classmfarra.cst.ps www.fb.com/MahmoudRFarra

Page 22: القوائم المترابطة Linked List باستخدام لغة جافا

Linked List OR Array List Classmfarra.cst.ps www.fb.com/MahmoudRFarra

Which of the two classes you use? 1. If you need to support random access through an

index (not at the beginning of the list) ArrayList offers

the most efficient collection.

2. If your application requires the insertion or deletion of

elements at the beginning of the list, you should

choose LinkedList. Self Study: Explain why ? [ind, N.W]

Page 23: القوائم المترابطة Linked List باستخدام لغة جافا

Linked List OR Array List Classmfarra.cst.ps www.fb.com/MahmoudRFarra

ArrayList is efficient for retrieving elements.

LinkedList is efficient for inserting and removing

elements at the beginning of the list.

Both have the same performance for inserting

and removing elements in the middle or at the

end of the list. Self Study: Explain why ? [ind, N.W]

Page 24: القوائم المترابطة Linked List باستخدام لغة جافا

Linked List Examplemfarra.cst.ps www.fb.com/MahmoudRFarra

Page 25: القوائم المترابطة Linked List باستخدام لغة جافا

Static methods for manipulating lists and collectionsmfarra.cst.ps www.fb.com/MahmoudRFarra

Page 26: القوائم المترابطة Linked List باستخدام لغة جافا

Variations of Linked Listmfarra.cst.ps www.fb.com/MahmoudRFarra

The linked list introduced in the preceding

sections is known as a singly linked list.

It contains a pointer to the list’s first node, and

each node contains a pointer to the next node

sequentially.

Several variations of the linked list are useful in

certain applications.

Page 27: القوائم المترابطة Linked List باستخدام لغة جافا

Variations of Linked Listmfarra.cst.ps www.fb.com/MahmoudRFarra

Page 28: القوائم المترابطة Linked List باستخدام لغة جافا

using Java

2015

FB: M a h m o u d R F a r r aYouTube: M a h m o u d R F a r SlidesShare: mralfarra

Thank you