SlideShare a Scribd company logo
1 of 33
Download to read offline
‫لیست‬
‫زضَی‬ ‫ًاصس‬ ‫سید‬
razavi@iust.ac.iremail:
1391
f1notes.ir
‫معزفی‬
‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-1391
2
‫لیست‬
ُ‫پیاد‬‫ساشی‬‫لیست‬‫با‬ِ‫آزای‬
‫لیست‬‫پیًَدی‬
‫لیست‬‫پیًَدی‬ٍ‫د‬ِ‫طسف‬
‫لیست‬
‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-1391
3
‫لیست‬.‫تعداد‬‫هحدٍدی‬‫عٌصس‬ُ‫داد‬‫ای‬ِ‫ک‬‫تستیب‬‫آًْا‬‫هْن‬‫است‬.
n:ُ‫اًداش‬‫لیست‬(n ≥ 0)
n = 0:‫لیست‬‫تْی‬(‫پَچ‬)
‫لیست‬‫یک‬‫ساختواى‬ُ‫داد‬‫خطی‬‫است‬:‫ّس‬‫عٌصس‬‫دازای‬‫یک‬‫عٌصس‬‫قبلی‬(ِ‫ب‬‫جص‬‫اٍلیي‬
‫عٌصس‬)ٍ‫یک‬‫عٌصس‬‫بعدی‬(ِ‫ب‬‫جص‬‫آخسیي‬‫عٌصس‬)‫است‬.
‫لیست‬ ‫ياسط‬
4
‫دست‬‫گسهی‬.‫یک‬‫لیست‬‫اش‬ِ‫زشت‬‫ّا‬
ListOfStringspublic class
‫یک‬ ‫ایجاد‬‫خالی‬ ‫لیست‬ListOfStrings()
‫مکان‬ ‫در‬ ‫عنصز‬ ‫یک‬ ‫درج‬pinsert(int p, String item)void
‫مکان‬ ‫در‬ ‫واقع‬ ‫عنصز‬ ‫حذف‬pdelete(int p)String
‫مکان‬ ‫در‬ ‫واقع‬ ‫عنصز‬ ‫بزگزداندن‬pget(int p)String
‫مکان‬ ‫در‬ ‫جدید‬ ‫عنصز‬ ‫یک‬ ‫دادن‬ ‫قزار‬Pset(int p, String item)String
‫لیست‬ ‫بودن‬ ‫خالی‬ ‫بزرسی‬isEmpty()boolean
‫تعداد‬ ‫بزگزداندن‬‫لیست‬ ‫در‬ ‫موجود‬ ‫عناصز‬size()int
f1notes.ir
ِ‫آزای‬ ‫اش‬ ُ‫استفاد‬ ‫با‬ ‫ساشی‬ ُ‫پیاد‬
‫گس‬ ُ‫اشاز‬ ‫اش‬ ُ‫استفاد‬ ‫با‬ ‫ساشی‬ ُ‫پیاد‬
‫لیست‬ ‫ساسی‬ ٌ‫پیاد‬ 5
ٍ‫آرای‬ ‫با‬ ‫لیست‬ ‫ساسی‬ ٌ‫پیاد‬
‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-1391
6
ُ‫پیاد‬‫ساشی‬‫لیست‬‫با‬ِ‫آزای‬.
‫عٌاصس‬‫لیست‬‫زا‬‫با‬‫شسٍع‬‫اش‬ًِ‫خا‬،‫صفس‬ِ‫ب‬‫تستیب‬‫دز‬ًِ‫خا‬‫ّای‬‫پشت‬‫سس‬‫ّن‬ِ‫آزای‬a[]ُ‫ذخیس‬‫کي‬
nullnullnullnullbetonotorbetoa[]
9876543210
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++;
}
nullnullnullnullbetonotorbetoa[]
9876543210
N
capacity = 10
p
‫حذف‬
‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-1391
9
nullnullnullnullbetonotorbetoa[]
9876543210
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
tofirst be or
null
Node oldFirst = first;
oldFirst
to be or
null
oldFirstfirst = new Node();
not
null
first
first.item = “not”;
to be or
null
oldFirst
notfirst
first.next = oldFirst;
‫ابتدا‬ ‫اس‬ ‫حذف‬
‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-1391
16
tofirst be or
null
tofirst be or
null
first = first.next;
‫اوتُا‬ ‫در‬ ‫درج‬
‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-1391
17
tofirst be or
null
Node oldLast = last;
to be or
null
last = new Node();
first
last.item = “not”;
oldLast.next = last;
last
oldLast
not
null
lastoldLast
to be or
first
not
null
lastoldLast
f1notes.ir
ٌ‫ضد‬ ٌ‫داد‬ ‫عىصز‬ ‫یک‬ ‫اس‬ ‫بعد‬ ‫درج‬
‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-1391
18
tofirst be or
null
x
Node y = new Node();
that
y
y.item = “that”;
y.next = x.next;
x.next = y;
tofirst be or
null
x
that
y
ٌ‫ضد‬ ٌ‫داد‬ ‫عىصز‬ ‫یک‬ ‫اس‬ ‫بعد‬ ‫حذف‬
19
be or not
null
x
tofirst
Node y = x.next;
y
if (y != null)
x.next = y.next; be or not
null
x
tofirst
y
y = null;
be not
null
x
tofirst
‫پیًودی‬ ‫لیست‬ ‫یک‬ ‫پیمایص‬
‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-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
tofirst
last
be or
null
not
null
x
tofirst
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
tofirst
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
tox
be or not
null
toy
‫جدید‬ ‫لیست‬ ‫یک‬ ‫در‬ ‫مزتب‬ ‫پیًودی‬ ‫لیست‬ ‫دي‬ ‫ادغام‬
‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-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
f1notes.ir - by : ehsan-gholami

More Related Content

More from ehsangh100

Genral list-f1notes.ir
Genral list-f1notes.irGenral list-f1notes.ir
Genral list-f1notes.irehsangh100
 
Stack&quee linklist-f1notes.ir
Stack&quee linklist-f1notes.irStack&quee linklist-f1notes.ir
Stack&quee linklist-f1notes.irehsangh100
 
Union f1notes.ir
Union f1notes.irUnion f1notes.ir
Union f1notes.irehsangh100
 
Sakhtoman dadeha 91-92 - nimsal 1 -F1notes.ir
Sakhtoman dadeha   91-92 - nimsal 1 -F1notes.irSakhtoman dadeha   91-92 - nimsal 1 -F1notes.ir
Sakhtoman dadeha 91-92 - nimsal 1 -F1notes.irehsangh100
 
Sakhtoman dadeha 90-91 - nimsal 2 -F1notes.ir
Sakhtoman dadeha   90-91 - nimsal 2 -F1notes.irSakhtoman dadeha   90-91 - nimsal 2 -F1notes.ir
Sakhtoman dadeha 90-91 - nimsal 2 -F1notes.irehsangh100
 
Sakhtoman dadeha 90-91 - nimsal 1 -F1notes.ir
Sakhtoman dadeha   90-91 - nimsal 1 -F1notes.irSakhtoman dadeha   90-91 - nimsal 1 -F1notes.ir
Sakhtoman dadeha 90-91 - nimsal 1 -F1notes.irehsangh100
 
Sakhtoman dadeha 91-92 - nimsal 2 -F1notes.ir
Sakhtoman dadeha   91-92 - nimsal 2 -F1notes.irSakhtoman dadeha   91-92 - nimsal 2 -F1notes.ir
Sakhtoman dadeha 91-92 - nimsal 2 -F1notes.irehsangh100
 
پشته و کاربرد ها
پشته و کاربرد ها پشته و کاربرد ها
پشته و کاربرد ها ehsangh100
 

More from ehsangh100 (9)

Chand jomlei
Chand jomleiChand jomlei
Chand jomlei
 
Genral list-f1notes.ir
Genral list-f1notes.irGenral list-f1notes.ir
Genral list-f1notes.ir
 
Stack&quee linklist-f1notes.ir
Stack&quee linklist-f1notes.irStack&quee linklist-f1notes.ir
Stack&quee linklist-f1notes.ir
 
Union f1notes.ir
Union f1notes.irUnion f1notes.ir
Union f1notes.ir
 
Sakhtoman dadeha 91-92 - nimsal 1 -F1notes.ir
Sakhtoman dadeha   91-92 - nimsal 1 -F1notes.irSakhtoman dadeha   91-92 - nimsal 1 -F1notes.ir
Sakhtoman dadeha 91-92 - nimsal 1 -F1notes.ir
 
Sakhtoman dadeha 90-91 - nimsal 2 -F1notes.ir
Sakhtoman dadeha   90-91 - nimsal 2 -F1notes.irSakhtoman dadeha   90-91 - nimsal 2 -F1notes.ir
Sakhtoman dadeha 90-91 - nimsal 2 -F1notes.ir
 
Sakhtoman dadeha 90-91 - nimsal 1 -F1notes.ir
Sakhtoman dadeha   90-91 - nimsal 1 -F1notes.irSakhtoman dadeha   90-91 - nimsal 1 -F1notes.ir
Sakhtoman dadeha 90-91 - nimsal 1 -F1notes.ir
 
Sakhtoman dadeha 91-92 - nimsal 2 -F1notes.ir
Sakhtoman dadeha   91-92 - nimsal 2 -F1notes.irSakhtoman dadeha   91-92 - nimsal 2 -F1notes.ir
Sakhtoman dadeha 91-92 - nimsal 2 -F1notes.ir
 
پشته و کاربرد ها
پشته و کاربرد ها پشته و کاربرد ها
پشته و کاربرد ها
 

2 list--f1notes.ir

  • 2. ‫معزفی‬ ‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-1391 2 ‫لیست‬ ُ‫پیاد‬‫ساشی‬‫لیست‬‫با‬ِ‫آزای‬ ‫لیست‬‫پیًَدی‬ ‫لیست‬‫پیًَدی‬ٍ‫د‬ِ‫طسف‬
  • 3. ‫لیست‬ ‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-1391 3 ‫لیست‬.‫تعداد‬‫هحدٍدی‬‫عٌصس‬ُ‫داد‬‫ای‬ِ‫ک‬‫تستیب‬‫آًْا‬‫هْن‬‫است‬. n:ُ‫اًداش‬‫لیست‬(n ≥ 0) n = 0:‫لیست‬‫تْی‬(‫پَچ‬) ‫لیست‬‫یک‬‫ساختواى‬ُ‫داد‬‫خطی‬‫است‬:‫ّس‬‫عٌصس‬‫دازای‬‫یک‬‫عٌصس‬‫قبلی‬(ِ‫ب‬‫جص‬‫اٍلیي‬ ‫عٌصس‬)ٍ‫یک‬‫عٌصس‬‫بعدی‬(ِ‫ب‬‫جص‬‫آخسیي‬‫عٌصس‬)‫است‬.
  • 4. ‫لیست‬ ‫ياسط‬ 4 ‫دست‬‫گسهی‬.‫یک‬‫لیست‬‫اش‬ِ‫زشت‬‫ّا‬ ListOfStringspublic class ‫یک‬ ‫ایجاد‬‫خالی‬ ‫لیست‬ListOfStrings() ‫مکان‬ ‫در‬ ‫عنصز‬ ‫یک‬ ‫درج‬pinsert(int p, String item)void ‫مکان‬ ‫در‬ ‫واقع‬ ‫عنصز‬ ‫حذف‬pdelete(int p)String ‫مکان‬ ‫در‬ ‫واقع‬ ‫عنصز‬ ‫بزگزداندن‬pget(int p)String ‫مکان‬ ‫در‬ ‫جدید‬ ‫عنصز‬ ‫یک‬ ‫دادن‬ ‫قزار‬Pset(int p, String item)String ‫لیست‬ ‫بودن‬ ‫خالی‬ ‫بزرسی‬isEmpty()boolean ‫تعداد‬ ‫بزگزداندن‬‫لیست‬ ‫در‬ ‫موجود‬ ‫عناصز‬size()int f1notes.ir
  • 5. ِ‫آزای‬ ‫اش‬ ُ‫استفاد‬ ‫با‬ ‫ساشی‬ ُ‫پیاد‬ ‫گس‬ ُ‫اشاز‬ ‫اش‬ ُ‫استفاد‬ ‫با‬ ‫ساشی‬ ُ‫پیاد‬ ‫لیست‬ ‫ساسی‬ ٌ‫پیاد‬ 5
  • 6. ٍ‫آرای‬ ‫با‬ ‫لیست‬ ‫ساسی‬ ٌ‫پیاد‬ ‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-1391 6 ُ‫پیاد‬‫ساشی‬‫لیست‬‫با‬ِ‫آزای‬. ‫عٌاصس‬‫لیست‬‫زا‬‫با‬‫شسٍع‬‫اش‬ًِ‫خا‬،‫صفس‬ِ‫ب‬‫تستیب‬‫دز‬ًِ‫خا‬‫ّای‬‫پشت‬‫سس‬‫ّن‬ِ‫آزای‬a[]ُ‫ذخیس‬‫کي‬ nullnullnullnullbetonotorbetoa[] 9876543210 N capacity = 10
  • 7. ‫لیست‬:‫جايا‬ ‫در‬ ‫ساسی‬ ٌ‫پیاد‬ 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 */ } }
  • 8. ‫درج‬ ‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-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++; } nullnullnullnullbetonotorbetoa[] 9876543210 N capacity = 10 p
  • 9. ‫حذف‬ ‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-1391 9 nullnullnullnullbetonotorbetoa[] 9876543210 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; }
  • 10. ‫دیگز‬ ‫عملیات‬ ‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-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; }
  • 11. ٍ‫آرای‬ ‫با‬ ‫لیست‬ ‫ساسی‬ ٌ‫پیاد‬ ‫معایب‬ ‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-1391 11 ِ‫آزای‬‫یک‬‫ساختواى‬ُ‫داد‬‫ایستا‬‫است‬ ُ‫اًداش‬ِ‫آزای‬‫باید‬‫دز‬‫شهاى‬‫طساحی‬‫تعییي‬‫شَد‬ٍ‫دز‬‫طَل‬‫اجسا‬‫ثابت‬‫بواًد‬ ُ‫زا‬‫حل‬.‫تغییس‬ُ‫اًداش‬ِ‫آزای‬‫دز‬‫صَزت‬‫لصٍم‬[‫با‬‫ها‬‫باشید‬] ‫کٌد‬‫بَدى‬‫عول‬‫دزج‬ٍ‫حرف‬ ‫دزج‬ٍ‫حرف‬‫عٌاصس‬‫ًیاش‬ِ‫ب‬‫جابجایی‬‫سایس‬‫عٌاصس‬‫دازد‬ٍ‫دز‬ِ‫ًتیج‬ٌِ‫ّصی‬‫ّس‬‫یک‬‫اش‬‫ایي‬‫اعوال‬ ‫هتٌاسب‬‫با‬‫تعداد‬‫عٌاصس‬‫لیست‬‫است‬ ُ‫زا‬‫حل‬.ُ‫استفاد‬‫اش‬‫لیست‬‫پیًَدی‬
  • 12. ‫پیًودی‬ ‫لیست‬ ‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-1391 12 ‫تعسیف‬‫باشگشتی‬.‫لیست‬‫پیًَدی‬‫یک‬‫ساختواى‬ُ‫داد‬‫باشگشتی‬‫است‬ِ‫ک‬‫یا‬‫تْی‬‫است‬ ‫یا‬‫شاهل‬‫یک‬‫ازجاع‬ِ‫ب‬‫یک‬ُ‫گس‬‫است‬ِ‫ب‬‫طَزی‬ِ‫ک‬ِ‫ک‬‫آى‬ُ‫گس‬‫خَد‬‫حاٍی‬‫یک‬‫عٌصس‬ ٍ‫یک‬‫ازجاع‬ِ‫ب‬‫یک‬‫لیست‬‫پیًَدی‬‫دیگس‬‫است‬ ‫تعسیف‬‫باشگشتی‬.‫لیست‬‫پیًَدی‬‫یک‬‫ساختواى‬ُ‫داد‬‫باشگشتی‬‫است‬ِ‫ک‬‫یا‬‫تْی‬‫است‬ ‫یا‬‫شاهل‬‫یک‬‫ازجاع‬ِ‫ب‬‫یک‬ُ‫گس‬‫است‬ِ‫ب‬‫طَزی‬ِ‫ک‬ِ‫ک‬‫آى‬ُ‫گس‬‫خَد‬‫حاٍی‬‫یک‬‫عٌصس‬ ٍ‫یک‬‫ازجاع‬ِ‫ب‬‫یک‬‫لیست‬‫پیًَدی‬‫دیگس‬‫است‬ to be or not to be null first
  • 13. ‫پیًودی‬ ‫لیست‬ ‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-1391 13 private class Node { private Item item; private Node next; } private class Node { private Item item; private Node next; } ‫درونی‬ ‫کالس‬ item next ‫تعسیف‬‫باشگشتی‬.‫لیست‬‫پیًَدی‬‫یک‬‫ساختواى‬ُ‫داد‬‫باشگشتی‬‫است‬ِ‫ک‬‫یا‬‫تْی‬‫است‬ ‫یا‬‫شاهل‬‫یک‬‫ازجاع‬ِ‫ب‬‫یک‬ُ‫گس‬‫است‬ِ‫ب‬‫طَزی‬ِ‫ک‬ِ‫ک‬‫آى‬ُ‫گس‬‫خَد‬‫حاٍی‬‫یک‬‫عٌصس‬ ٍ‫یک‬‫ازجاع‬ِ‫ب‬‫یک‬‫لیست‬‫پیًَدی‬‫دیگس‬‫است‬ ‫تعسیف‬‫باشگشتی‬.‫لیست‬‫پیًَدی‬‫یک‬‫ساختواى‬ُ‫داد‬‫باشگشتی‬‫است‬ِ‫ک‬‫یا‬‫تْی‬‫است‬ ‫یا‬‫شاهل‬‫یک‬‫ازجاع‬ِ‫ب‬‫یک‬ُ‫گس‬‫است‬ِ‫ب‬‫طَزی‬ِ‫ک‬ِ‫ک‬‫آى‬ُ‫گس‬‫خَد‬‫حاٍی‬‫یک‬‫عٌصس‬ ٍ‫یک‬‫ازجاع‬ِ‫ب‬‫یک‬‫لیست‬‫پیًَدی‬‫دیگس‬‫است‬
  • 14. ‫پیًودی‬ ‫لیست‬ ‫یک‬ ‫ساخته‬ ‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-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
  • 15. ‫ابتدا‬ ‫در‬ ‫درج‬ ‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-1391 15 tofirst be or null Node oldFirst = first; oldFirst to be or null oldFirstfirst = new Node(); not null first first.item = “not”; to be or null oldFirst notfirst first.next = oldFirst;
  • 16. ‫ابتدا‬ ‫اس‬ ‫حذف‬ ‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-1391 16 tofirst be or null tofirst be or null first = first.next;
  • 17. ‫اوتُا‬ ‫در‬ ‫درج‬ ‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-1391 17 tofirst be or null Node oldLast = last; to be or null last = new Node(); first last.item = “not”; oldLast.next = last; last oldLast not null lastoldLast to be or first not null lastoldLast f1notes.ir
  • 18. ٌ‫ضد‬ ٌ‫داد‬ ‫عىصز‬ ‫یک‬ ‫اس‬ ‫بعد‬ ‫درج‬ ‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-1391 18 tofirst be or null x Node y = new Node(); that y y.item = “that”; y.next = x.next; x.next = y; tofirst be or null x that y
  • 19. ٌ‫ضد‬ ٌ‫داد‬ ‫عىصز‬ ‫یک‬ ‫اس‬ ‫بعد‬ ‫حذف‬ 19 be or not null x tofirst Node y = x.next; y if (y != null) x.next = y.next; be or not null x tofirst y y = null; be not null x tofirst
  • 20. ‫پیًودی‬ ‫لیست‬ ‫یک‬ ‫پیمایص‬ ‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-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. ‫اوتُایی‬ ‫عىصز‬ ‫حذف‬ 21 be or not null x tofirst last be or null not null x tofirst last Node x = first; while (x.next != last) x = x.next last = x; last.next = null;
  • 22. ٍ‫طزف‬ ‫یک‬ ‫پیًودی‬ ‫لیست‬ ‫یک‬ ‫عىاصز‬ ‫تعداد‬ ‫ضمارش‬ ‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-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 tofirst 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; }
  • 23. ٍ‫طزف‬ ‫یک‬ ‫پیًودی‬ ‫لیست‬ ‫یک‬ ‫کزدن‬ ‫کپی‬ ‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-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 tox be or not null toy
  • 24. ‫جدید‬ ‫لیست‬ ‫یک‬ ‫در‬ ‫مزتب‬ ‫پیًودی‬ ‫لیست‬ ‫دي‬ ‫ادغام‬ ‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-1391 24 1 3 10 null first 2 5 null first 1 null first x y L1 L2 L
  • 25. ‫جدید‬ ‫لیست‬ ‫یک‬ ‫در‬ ‫مزتب‬ ‫پیًودی‬ ‫لیست‬ ‫دي‬ ‫ادغام‬ ‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-1391 25 1 3 10 null first 2 5 null first 1 2 null first x y L1 L2 L
  • 26. ‫جدید‬ ‫لیست‬ ‫یک‬ ‫در‬ ‫مزتب‬ ‫پیًودی‬ ‫لیست‬ ‫دي‬ ‫ادغام‬ ‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-1391 26 1 3 10 null first 2 5 null first 1 2 3 null first x y L1 L2 L
  • 27. ‫جدید‬ ‫لیست‬ ‫یک‬ ‫در‬ ‫مزتب‬ ‫پیًودی‬ ‫لیست‬ ‫دي‬ ‫ادغام‬ ‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-1391 27 1 3 10 null first 2 5 null first 1 2 3 first 5 null x y L1 L2 L
  • 28. ‫جدید‬ ‫لیست‬ ‫یک‬ ‫در‬ ‫مزتب‬ ‫پیًودی‬ ‫لیست‬ ‫دي‬ ‫ادغام‬ ‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-1391 28 1 3 10 null first 2 5 null first 1 2 3 first 5 10 null x y L1 L2 L
  • 29. ‫جدید‬ ‫لیست‬ ‫یک‬ ‫در‬ ‫مزتب‬ ‫پیًودی‬ ‫لیست‬ ‫دي‬ ‫ادغام‬ ‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-1391 29 1 3 10 null first 2 5 null first 1 2 3 first 5 10 null x y L1 L2 L
  • 30. ‫جدید‬ ‫لیست‬ ‫یک‬ ‫در‬ ‫مزتب‬ ‫پیًودی‬ ‫لیست‬ ‫دي‬ ‫ادغام‬ ‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-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; }
  • 31. ‫لیست‬‫پیًَدی‬ٍ‫د‬ِ‫طسف‬.‫ّس‬‫عٌصس‬ٍُ‫عال‬‫بس‬‫آدزس‬‫عٌصس‬،‫بعدی‬‫آدزس‬‫عٌصس‬‫قبل‬‫اش‬ ‫خَد‬‫زا‬‫ًیص‬‫ًگْدازی‬‫هی‬‫کٌد‬ ٍ‫طزف‬ ‫دي‬ ‫پیًودی‬ ‫لیست‬ ‫رضًی‬ ‫واصز‬ ‫سید‬-‫َا‬ ٌ‫داد‬ ‫ساختمان‬-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
  • 32. 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
  • 33. 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 f1notes.ir - by : ehsan-gholami