33
لیستی اصس زض سید[email protected] email: 1391

2 list--f1notes.ir

Embed Size (px)

Citation preview

لیست سید اصس زضی

[email protected]:

1391

Ehsan
Typewriter
f1notes.ir
Ehsan
Typewriter

معزفی

1391 -ساختمان داد ا -سید واصز رضی

2

لیست آزای با لیست ساشی پیاد پیدی لیست طسف د پیدی لیست

لیست

1391 -ساختمان داد ا -سید واصز رضی

3

است هن آا تستیب ک ای داد عصس هحددی تعداد .لیست. n: لیست اداش (n ≥ 0) n = 0: (پچ) تی لیست

الیي جص ب) قبلی عصس یک دازای عصس س :است خطی داد ساختواى یک لیست .است (عصس آخسیي جص ب) بعدی عصس یک (عصس

ياسط لیست4

ا زشت اش لیست یک .گسهی دست

ListOfStrings public class

()ListOfStrings لیست خالی ایجاد یک

p insert(int p, String item) voidدرج یک عنصز در مکان

p delete(int p) Stringحذف عنصز واقع در مکان

p get(int p) String بزگزداندن عنصز واقع در مکان

P set(int p, String item) Stringقزار دادن یک عنصز جدید در مکان

isEmpty() boolean بزرسی خالی بودن لیست

size() int عناصز موجود در لیست بزگزداندن تعداد

Ehsan
Typewriter
f1notes.ir

پیاد ساشی با استفاد اش آزای پیاد ساشی با استفاد اش اشاز گس

5 پیاد ساسی لیست

پیاد ساسی لیست با آرای

1391 -ساختمان داد ا -سید واصز رضی

6

آزای با لیست ساشی پیاد. آزای ن سس پشت ای خا دز تستیب ب صفس، خا اش شسع با زا لیست عاصس a[] کي ذخیس

null null null null be to not or be to a[]

9 8 7 6 5 4 3 2 1 0

N

capacity = 10

پیاد ساسی در جايا: لیست7

public class FixedCapacityListOfStrings

{

private String[] a;

private int N = 0;

public FixedCapacityListOfStrings(int capacity)

{ a = new String[capacity]; }

public int size()

{ return N; }

public boolean isEmpty()

{ return size() == 0; }

public void insert(int p, String item)

{ /* see next slides */ }

public String delete(int p)

{ /* see next slides */ }

public String get(int p)

{ /* see next slides */ }

public String set(int p, String item)

{ /* see next slides */ }

}

public class FixedCapacityListOfStrings

{

private String[] a;

private int N = 0;

public FixedCapacityListOfStrings(int capacity)

{ a = new String[capacity]; }

public int size()

{ return N; }

public boolean isEmpty()

{ return size() == 0; }

public void insert(int p, String item)

{ /* see next slides */ }

public String delete(int p)

{ /* see next slides */ }

public String get(int p)

{ /* see next slides */ }

public String set(int p, String item)

{ /* see next slides */ }

}

درج

1391 -ساختمان داد ا -سید واصز رضی

8

public void insert(int p, String item)

{

if ( p < 0 || p > N) return;

if ( N == a.length) return;

for (int i = N; i > p; i--)

a[i] = a[i – 1];

a[p] = item;

N++;

}

public void insert(int p, String item)

{

if ( p < 0 || p > N) return;

if ( N == a.length) return;

for (int i = N; i > p; i--)

a[i] = a[i – 1];

a[p] = item;

N++;

}

null null null null be to not or be to a[]

9 8 7 6 5 4 3 2 1 0

N

capacity = 10

p

حذف

1391 -ساختمان داد ا -سید واصز رضی

9

null null null null be to not or be to a[]

9 8 7 6 5 4 3 2 1 0

N

capacity = 10

p

public String delete(int p)

{

if (p < 0 || p >= N) return null;

String item = a[p];

for (int i = p; i < N - 1; i++)

a[i] = a[i + 1];

N--;

return item;

}

public String delete(int p)

{

if (p < 0 || p >= N) return null;

String item = a[p];

for (int i = p; i < N - 1; i++)

a[i] = a[i + 1];

N--;

return item;

}

عملیات دیگز

1391 -ساختمان داد ا -سید واصز رضی

10

public String get(int p)

{

if ( p < 0 || p >= N) return null;

return a[p];

}

public String set(int p, String item)

{

if (p < 0 || p >= N) return null;

String oldItem = a[p];

a[p] = item;

return oldItem;

}

public String get(int p)

{

if ( p < 0 || p >= N) return null;

return a[p];

}

public String set(int p, String item)

{

if (p < 0 || p >= N) return null;

String oldItem = a[p];

a[p] = item;

return oldItem;

}

معایب پیاد ساسی لیست با آرای

1391 -ساختمان داد ا -سید واصز رضی

11

است ایستا داد ساختواى یک آزای بواد ثابت اجسا طل دز شد تعییي طساحی شهاى دز باید آزای اداش

[باشید ها با] لصم صزت دز آزای اداش تغییس .حل زا

حرف دزج عول بدى کد اعوال ایي اش یک س صی تیج دز دازد عاصس سایس جابجایی ب یاش عاصس حرف دزج

است لیست عاصس تعداد با هتاسب

پیدی لیست اش استفاد .حل زا

لیست پیودی

1391 -ساختمان داد ا -سید واصز رضی

12

است تی یا ک است باشگشتی داد ساختواى یک پیدی لیست .باشگشتی تعسیف عصس یک حای خد گس آى ک ک طزی ب است گس یک ب ازجاع یک شاهل یا است دیگس پیدی لیست یک ب ازجاع یک

است تی یا ک است باشگشتی داد ساختواى یک پیدی لیست .باشگشتی تعسیف عصس یک حای خد گس آى ک ک طزی ب است گس یک ب ازجاع یک شاهل یا است دیگس پیدی لیست یک ب ازجاع یک

to be or not to be

null

first

لیست پیودی

1391 -ساختمان داد ا -سید واصز رضی

13

private class Node

{

private Item item;

private Node next;

}

private class Node

{

private Item item;

private Node next;

}

کالس درونی

item

next

است تی یا ک است باشگشتی داد ساختواى یک پیدی لیست .باشگشتی تعسیف عصس یک حای خد گس آى ک ک طزی ب است گس یک ب ازجاع یک شاهل یا است دیگس پیدی لیست یک ب ازجاع یک

است تی یا ک است باشگشتی داد ساختواى یک پیدی لیست .باشگشتی تعسیف عصس یک حای خد گس آى ک ک طزی ب است گس یک ب ازجاع یک شاهل یا است دیگس پیدی لیست یک ب ازجاع یک

ساخته یک لیست پیودی

1391 -ساختمان داد ا -سید واصز رضی

14

to

null

first

Node first = new Node();

first.item = “to”;

Node second = new Node();

second.item = “be”; to

first

be

null

second

first.next = second;

Node third = new Node();

third.item = “or”;

second.next = third;

to

first

be

second

or

null

third

درج در ابتدا

1391 -ساختمان داد ا -سید واصز رضی

15

to first be or

null

Node oldFirst = first;

oldFirst

to be or

null

oldFirst first = new Node();

not

null

first first.item = “not”;

to be or

null

oldFirst

not first

first.next = oldFirst;

حذف اس ابتدا

1391 -ساختمان داد ا -سید واصز رضی

16

to first be or

null

to first be or

null

first = first.next;

درج در اوتا

1391 -ساختمان داد ا -سید واصز رضی

17

to first be or

null

Node oldLast = last;

to be or

null

last = new Node();

first last.item = “not”;

oldLast.next = last;

last

oldLast

not

null

last oldLast

to be or first

not

null

last oldLast

Ehsan
Typewriter
f1notes.ir

درج بعد اس یک عىصز داد ضد

1391 -ساختمان داد ا -سید واصز رضی

18

to first be or

null

x Node y = new Node();

that

y

y.item = “that”;

y.next = x.next;

x.next = y;

to first be or

null

x

that

y

حذف بعد اس یک عىصز داد ضد19

be or not

null

x

to first

Node y = x.next;

y

if (y != null)

x.next = y.next; be or not

null

x

to first

y

y = null;

be not

null

x

to first

پیمایص یک لیست پیودی

1391 -ساختمان داد ا -سید واصز رضی

20

آزای یک پیوایش

پیدی لیست یک پیوایش

for (int i = 0; i < a.length; i++)

{

// process a[i]

}

for (int i = 0; i < a.length; i++)

{

// process a[i]

}

for (Node x = first; x != null; x = x.next)

{

// process x.item

}

for (Node x = first; x != null; x = x.next)

{

// process x.item

}

حذف عىصز اوتایی21

be or not

null

x

to first

last

be or

null

not

null

x

to first

last

Node x = first;

while (x.next != last)

x = x.next

last = x;

last.next = null;

ضمارش تعداد عىاصز یک لیست پیودی یک طزف

1391 -ساختمان داد ا -سید واصز رضی

22

public int count()

{ return count(first); }

public int count(Node x)

{

if (x == null) return 0;

return 1 + count(x.next);

}

public int count()

{ return count(first); }

public int count(Node x)

{

if (x == null) return 0;

return 1 + count(x.next);

}

be or

null

to first

public int count()

{

int count = 0;

for (Node x = first; x ! = null; x = x.next)

count++;

return count;

}

public int count()

{

int count = 0;

for (Node x = first; x ! = null; x = x.next)

count++;

return count;

}

کپی کزدن یک لیست پیودی یک طزف

1391 -ساختمان داد ا -سید واصز رضی

23

public Node copy(Node x)

{

if (x == null) return null;

Node y = new Node();

y.item = x.item;

y.next = copy(x.next);

return y;

}

public Node copy(Node x)

{

if (x == null) return null;

Node y = new Node();

y.item = x.item;

y.next = copy(x.next);

return y;

}

be or not

null

to x

be or not

null

to y

ادغام دي لیست پیودی مزتب در یک لیست جدید

1391 -ساختمان داد ا -سید واصز رضی

24

1 3 10

null

first

2 5

null

first

1

null

first

x

y

L1

L2

L

ادغام دي لیست پیودی مزتب در یک لیست جدید

1391 -ساختمان داد ا -سید واصز رضی

25

1 3 10

null

first

2 5

null

first

1 2

null

first

x

y

L1

L2

L

ادغام دي لیست پیودی مزتب در یک لیست جدید

1391 -ساختمان داد ا -سید واصز رضی

26

1 3 10

null

first

2 5

null

first

1 2 3

null

first

x

y

L1

L2

L

ادغام دي لیست پیودی مزتب در یک لیست جدید

1391 -ساختمان داد ا -سید واصز رضی

27

1 3 10

null

first

2 5

null

first

1 2 3 first

5

null

x

y

L1

L2

L

ادغام دي لیست پیودی مزتب در یک لیست جدید

1391 -ساختمان داد ا -سید واصز رضی

28

1 3 10

null

first

2 5

null

first

1 2 3 first

5 10

null

x

y

L1

L2

L

ادغام دي لیست پیودی مزتب در یک لیست جدید

1391 -ساختمان داد ا -سید واصز رضی

29

1 3 10

null

first

2 5

null

first

1 2 3 first

5 10

null

x

y

L1

L2

L

ادغام دي لیست پیودی مزتب در یک لیست جدید

1391 -ساختمان داد ا -سید واصز رضی

30

public static LinkedList<Integer> merge(LinkedList<Integer> L1, LinkedList<Integer> L2)

{

LinkedList<Integer> L = new LinkedList<Integer>();

Node x = L1.first();

Node y = L2.first();

while (x != null || y != null)

{

String item;

if (x == null) { item = y.item; y = y.next; }

else if (y == null) { item = x.item; x = x.next; }

else if (y.item < x.item) { item = y.item; y = y.next; }

else { item = x.item; x = x.next; }

L.addLast(item);

}

return L;

}

public static LinkedList<Integer> merge(LinkedList<Integer> L1, LinkedList<Integer> L2)

{

LinkedList<Integer> L = new LinkedList<Integer>();

Node x = L1.first();

Node y = L2.first();

while (x != null || y != null)

{

String item;

if (x == null) { item = y.item; y = y.next; }

else if (y == null) { item = x.item; x = x.next; }

else if (y.item < x.item) { item = y.item; y = y.next; }

else { item = x.item; x = x.next; }

L.addLast(item);

}

return L;

}

اش قبل عصس آدزس بعدی، عصس آدزس بس عال عصس س .طسف د پیدی لیست کد هی گدازی یص زا خد

لیست پیودی دي طزف

1391 -ساختمان داد ا -سید واصز رضی

31

to

null

be or not to be

null

last first

private class Node

{

private Item item;

private Node next;

private Node prev;

}

private class Node

{

private Item item;

private Node next;

private Node prev;

}

کالس درونیitem

next

prev

Node y = New Node();

y.item = “or”;

y.next = x.next;

y.prev = x;

x.next = y;

if (y.next != null)

y.next.prev = y;

Node y = New Node();

y.item = “or”;

y.next = x.next;

y.prev = x;

x.next = y;

if (y.next != null)

y.next.prev = y;

درج بعد اس یک عىصز

1391 -ساختمان داد ا -سید واصز رضی

32

to

null

be not

first

to

null

be not

first x

x

or

y

Node y = x.next;

if (y != null) {

x.next = y.next;

if (y.next != null)

y.next.prev = x;

y = null;

}

Node y = x.next;

if (y != null) {

x.next = y.next;

if (y.next != null)

y.next.prev = x;

y = null;

}

حذف بعد اس یک عىصز

1391 -ساختمان داد ا -سید واصز رضی

33

y

to

null

be not

first x

or

to

null

be not

first x y

or

Ehsan
Typewriter
Ehsan
Typewriter
f1notes.ir - by : ehsan-gholami