Listi Java

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 45

3.

2
. ,
. . ,
.

(linked lists).
.
.
.
,
.
, , ,
( ).
(ADT)
ADT
.
3.1
. Link.
,
. Link (
next) .
. 3.1 .

3.1:
Link :
class Link

{
public int iData; // data
public double dData; // data
public Link next; // reference to next link
}

-
next . Link
: int double.
. , , , , ,
.
:
class Link
{
public inventoryItem iI; // object holding data
public Link next; // reference to next link
}
3.2
Link Link. Java Link
Link . next Link
Link.
.
.
.
Link.
Java int double .

7 3.14159.
new:
Link someLink = new Link();
someLink .
3.2.
C++ Java, C++

Link next;
Link. C++ -
( Link).
.
.

3.2:
3.3
.
:

Link LinkList.
Link .
class Link
{
public int iData;
// data item
public double dData;
// data item
public Link next;
// next link in list
// ------------------------------------------------------------public Link(int id, double dd)
// constructor
{
iData = id;
// initialize data
dData = dd;
// (next is automatically
}
// set to null)
// -------------------------------------------------------------

public void displayLink()


// display ourself
{
System.out.print({ + iData + , + dData + } );
}
} // end class Link
displayLink(),
. .
next null . null
.
public .
private.
public .
default package access (
).
.
LinkList , .
first.
.
first next.
class LinkList
{
private Link first;
// ref to first link on list
// ------------------------------------------------------------public void LinkList()
// constructor
{
first = null;
// no items on list yet
}
// ------------------------------------------------------------public boolean isEmpty()
// true if list is empty
{
return (first==null);
}
// ------------------------------------------------------------// ... other methods go here
}
LinkList first null.
null . isEmpty()
first null
.
insertFirst()
. . next
first, e first
. 3.3.
insetFirst() new .
.

3.3:

// insert at start of list


public void insertFirst(int id, double dd)
{
// make new link
Link newLink = new Link(id, dd);
newLink.next = first;
// newLink --> old first
first = newLink;
// first --> newLink
}
deleteFirst() . first
first .
next .
public Link deleteFirst()
{
Link temp = first;
first = first.next;
return temp;
}

// delete first item


// (assumes list not empty)
// save reference to link
// delete it: first-->old next
// return deleted link

deleteFirst() Link.
. 3.4.
C++
. Java garbage collection ,
.

deleteFirst() .
isEmpty().

3.4:
displayList() first
. current ( )
. first
.
current = current.next;
current
next.
public void displayList()
{
System.out.print(List (first-->last): );
Link current = first; // start at beginning of list
while(current != null) // until end of list,
{
current.displayLink(); // print data
current = current.next; // move to next link
}
System.out.println();
}
next null
. while
. 3.5 current
. displayList() displayLink()
.
linkList.java
. main().

main() , insertFirst()
.

3.5:
// linkList.java
// demonstrates linked list
// to run this program: C>java LinkListApp
////////////////////////////////////////////////////////////////
class Link
{
public int iData;
// data item (key)
public double dData;
// data item
public Link next;
// next link in list
// ------------------------------------------------------------public Link(int id, double dd)
// constructor
{
iData = id;
// initialize data
dData = dd;
// (next is automatically
}
// set to null)
// ------------------------------------------------------------public void displayLink()
// display ourself
{
System.out.print({ + iData + , + dData + } );
}
} // end class Link
////////////////////////////////////////////////////////////////
class LinkList
{
private Link first;
// ref to first link on list
// ------------------------------------------------------------public LinkList()
// constructor
{
first = null;
// no items on list yet
}

// ------------------------------------------------------------public boolean isEmpty()


// true if list is empty
{
return (first==null);
}
// ------------------------------------------------------------// insert at start of list
public void insertFirst(int id, double dd)
{
// make new link
Link newLink = new Link(id, dd);
newLink.next = first;
// newLink --> old first
first = newLink;
// first --> newLink
}
// ------------------------------------------------------------public Link deleteFirst()
// delete first item
{
// (assumes list not empty)
Link temp = first;
// save reference to link
first = first.next;
// delete it: first-->old next
return temp;
// return deleted link
}
// ------------------------------------------------------------public void displayList()
{
System.out.print(List (first-->last): );
Link current = first;
// start at beginning of list
while(current != null)
// until end of list,
{
current.displayLink();
// print data
current = current.next;
// move to next link
}
System.out.println();
}
// ------------------------------------------------------------} // end class LinkList
////////////////////////////////////////////////////////////////
class LinkListApp
{
public static void main(String[] args)
{
LinkList theList = new LinkList(); // make new list
theList.insertFirst(22, 2.99);
theList.insertFirst(44, 4.99);
theList.insertFirst(66, 6.99);
theList.insertFirst(88, 8.99);

// insert four items

theList.displayList();

// display list

while( !theList.isEmpty() )
// until its empty,
{
Link aLink = theList.deleteFirst(); // delete link

System.out.print(Deleted ); // display it
aLink.displayLink();
System.out.println();
}
theList.displayList();
// display list
} // end main()
} // end class LinkListApp
////////////////////////////////////////////////////////////////
, while deleteFirst()
. . :
List (first-->last): {88, 8.99} {66, 6.99} {44, 4.99} {22, 2.99}
Deleted {88, 8.99}
Deleted {66, 6.99}
Deleted {44, 4.99}
Deleted {22, 2.99}
List (first-->last):
3.3.1

. find delete
,
// linkList2.java
// demonstrates linked list
// to run this program: C>java LinkList2App
////////////////////////////////////////////////////////////////
class Link
{
public int iData;
// data item (key)
public double dData;
// data item
public Link next; // next link in list
// ------------------------------------------------------------public Link(int id, double dd)
// constructor
{
iData = id;
dData = dd;
}
// ------------------------------------------------------------public void displayLink() // display ourself
{
System.out.print({ + iData + , + dData + } );
}
} // end class Link
////////////////////////////////////////////////////////////////
class LinkList
{
private Link first;
// ref to first link on list
// -------------------------------------------------------------

public LinkList()
// constructor
{
first = null;
// no links on list yet
}
// ------------------------------------------------------------public void insertFirst(int id, double dd)
{
// make new link
Link newLink = new Link(id, dd);
newLink.next = first;
// it points to old first link
first = newLink;
// now first points to this
}
// ------------------------------------------------------------public Link find(int key)
// find link with given key
{
// (assumes non-empty list)
Link current = first;
// start at first
while(current.iData != key)
// while no match,
{
if(current.next == null)
// if end of list,
return null;
// didnt find it
else
// not end of list,
current = current.next;
// go to next link
}
return current; // found it
}
// ------------------------------------------------------------public Link delete(int key)
// delete link with given key
{
// (assumes non-empty list)
Link current = first;
// search for link
Link previous = first;
while(current.iData != key)
{
if(current.next == null)
return null;
// didnt find it
else
{
previous = current;
// go to next link
current = current.next;
}
}
// found it
if(current == first)
// if first link,
first = first.next;
// change first
else
// otherwise,
previous.next = current.next; // bypass it
return current;
}
// ------------------------------------------------------------public void displayList()
// display the list
{
System.out.print(List (first-->last): );
Link current = first;
// start at beginning of list
while(current != null)
// until end of list,

{
current.displayLink();
// print data
current = current.next;
// move to next link
}
System.out.println();
}
// ------------------------------------------------------------} // end class LinkList
////////////////////////////////////////////////////////////////
class LinkList2App
{
public static void main(String[] args)
{
LinkList theList = new LinkList(); // make list
theList.insertFirst(22, 2.99);
theList.insertFirst(44, 4.99);
theList.insertFirst(66, 6.99);
theList.insertFirst(88, 8.99);

// insert 4 items

theList.displayList();

// display list

Link f = theList.find(44);
// find item
if( f != null)
System.out.println(Found link with key + f.iData);
else
System.out.println(Cant find link);
Link d = theList.delete(66);
// delete item
if( d != null )
System.out.println(Deleted link with key + d.iData);
else
System.out.println(Cant delete link);
theList.displayList();
// display list
} // end main()
} // end class LinkList2App
////////////////////////////////////////////////////////////////
main() , .
44, 66
. :
List (first-->last): {88, 8.99} {66, 6.99} {44, 4.99} {22, 2.99}
Found link with key 44
Deleted link with key 66
List (first-->last): {88, 8.99} {44, 4.99} {22, 2.99}
find() displayList() LinkList.java.
current first
current.next.

.
. null.
delete() find()
. (current)
current (previous).
, 3.6.

3.6
while current current.next previous
current. previous current.
current next previous
next.link.
. first first.next.
:

if(current == first)
first = first.next;
else
previous.next = current.next;

// found it
// if first link,
// change first
// otherwise,
// bypass link

,
. . , insertAfter()
.
3.4 (Double-Ended Lists)
, :
.
.

, .


. .
firstLastList.java .
// firstLastList.java
// demonstrates list with first and last references
// to run this program: C>java FirstLastApp
////////////////////////////////////////////////////////////////
class Link
{
public long dData;
// data item
public Link next;
// next link in list
// ------------------------------------------------------------public Link(long d)
// constructor
{ dData = d; }
// ------------------------------------------------------------public void displayLink()
// display this link
System.out.print(dData + ); }
// ------------------------------------------------------------} // end class Link
////////////////////////////////////////////////////////////////
class FirstLastList
{
private Link first;
// ref to first link
private Link last;
// ref to last link
// ------------------------------------------------------------public FirstLastList()
// constructor
{
first = null;
// no links on list yet
last = null;
}
// ------------------------------------------------------------public boolean isEmpty()
// true if no links
{ return first==null; }
// ------------------------------------------------------------public void insertFirst(long dd)
// insert at front of list
{
Link newLink = new Link(dd); // make new link
if( isEmpty() )
// if empty list,
last = newLink;
// newLink <-- last
newLink.next = first;
// newLink --> old first
first = newLink;
// first --> newLink
}
// ------------------------------------------------------------public void insertLast(long dd)
// insert at end of list
{
Link newLink = new Link(dd);
// make new link
if( isEmpty() )
// if empty list,
first = newLink;
// first --> newLink
else
last.next = newLink;
// old last --> newLink

last = newLink;
// newLink <-- last
}
// ------------------------------------------------------------public long deleteFirst()
// delete first link
{
// (assumes non-empty list)
long temp = first.dData;
if(first.next == null) // if only one item
last = null; // null <-- last
first = first.next; // first --> old next
return temp;
}
// ------------------------------------------------------------public void displayList()
{
System.out.print(List (first-->last): );
Link current = first;
// start at beginning
while(current != null)
// until end of list,
{
current.displayLink();
// print data
current = current.next;
// move to next link
}
System.out.println();
}
// ------------------------------------------------------------} // end class FirstLastList
////////////////////////////////////////////////////////////////
class FirstLastApp
{
public static void main(String[] args)
{
// make a new list
FirstLastList theList = new FirstLastList();
theList.insertFirst(22);
// insert at front
theList.insertFirst(44);
theList.insertFirst(66);
theList.insertLast(11);
theList.insertLast(33);
theList.insertLast(55);

// insert at rear

theList.displayList();

// display the list

theList.deleteFirst();
theList.deleteFirst();

// delete first two items

theList.displayList();
} // end main()
} // end class FirstLastApp
////////////////////////////////////////////////////////////////

// display again

.
. ,

.
. :
List (first-->last): 66 44 22 11 33 55
List (first-->last): 22 11 33 55
FirstLastList.
first last .
first last ,
null.
insertLast() .
last.next last
, 3.7.

3.7:
.

. isEmpty() true, insertFirst() last
insertLast() first .
insertFirst() first ,
insertLast() last .

. last null.

last next null

.
-. - . ,
.
3.4.1
.
, (1) .
, ,
. O(N) .
O(N)
.
, .

.

.
,
( ).
.
3.5 (Abstract Data Types - ADT)
()
.
.
.
.
: .
3.5.1
.
push() pop() ,
arr[++top] = data;

data = arr[top--];
.
, .
push() pop()
theList.insertFirst(data)

data = theList.deleteFirst()
push() pop()
.
LinkStack LinkList .

// linkStack.java
// demonstrates a stack implemented as a list
// to run this program: C>java LinkStackApp
////////////////////////////////////////////////////////////////
class Link
{
public long dData;
// data item
public Link next;
// next link in list
// ------------------------------------------------------------public Link(long dd)
// constructor
{ dData = dd; }
// ------------------------------------------------------------public void displayLink()
// display ourself
{ System.out.print(dData + ); }
} // end class Link
////////////////////////////////////////////////////////////////
class LinkList
{
private Link first;
// ref to first item on list
// ------------------------------------------------------------public LinkList()
// constructor
{ first = null; }
// no items on list yet
// ------------------------------------------------------------public boolean isEmpty()
// true if list is empty
{ return (first==null); }
// ------------------------------------------------------------public void insertFirst(long dd)
// insert at start of list
{
// make new link
Link newLink = new Link(dd);
newLink.next = first;
// newLink --> old first
first = newLink;
// first --> newLink
}
// ------------------------------------------------------------public long deleteFirst()
// delete first item
{
// (assumes list not empty)
Link temp = first;
// save reference to link
first = first.next;
// delete it: first-->old next
return temp.dData;
// return deleted link
}
// ------------------------------------------------------------public void displayList()
{
Link current = first;
// start at beginning of list
while(current != null)
// until end of list,
{
current.displayLink();
// print data
current = current.next;
// move to next link
}
System.out.println();
}
// -------------------------------------------------------------

} // end class LinkList


////////////////////////////////////////////////////////////////
class LinkStack
{
private LinkList theList;
//-------------------------------------------------------------public LinkStack() // constructor
{
theList = new LinkList();
}
//-------------------------------------------------------------public void push(long j)
// put item on top of stack
{
theList.insertFirst(j);
}
//-------------------------------------------------------------public long pop()
// take item from top of stack
{
return theList.deleteFirst();
}
//-------------------------------------------------------------public boolean isEmpty()
// true if stack is empty
{
return ( theList.isEmpty() );
}
//-------------------------------------------------------------public void displayStack()
{
System.out.print(Stack (top-->bottom): );
theList.displayList();
}
//-------------------------------------------------------------} // end class LinkStack
////////////////////////////////////////////////////////////////
class LinkStackApp
{
public static void main(String[] args)
{
LinkStack theStack = new LinkStack(); // make stack
theStack.push(20);
// push items
theStack.push(40);
theStack.displayStack();
// display stack
theStack.push(60);
// push items
theStack.push(80);
theStack.displayStack();
// display stack
theStack.pop();
// pop items
theStack.pop();
theStack.displayStack();
// display stack
} // end main()
} // end class LinkStackApp
////////////////////////////////////////////////////////////////

main() , ,
, . ,
.
Stack (top-->bottom): 40 20
Stack (top-->bottom): 80 60 40 20
Stack (top-->bottom): 40 20
. main()
LinkStackApp LinkStack . LinkStack
LinkList . main() LinkList .
main() push() LinkStack
insertFirst() LinkList . , pop() deleteFirst()
displayStack() displayList() .
main() LinkStack
stack.java.
3.5.2
.
.
// linkQueue.java
// demonstrates queue implemented as double-ended list
// to run this program: C>java LinkQueueApp
////////////////////////////////////////////////////////////////
class Link
{
public long dData;
// data item
public Link next;
// next link in list
// ------------------------------------------------------------public Link(long d)
// constructor
{ dData = d; }
// ------------------------------------------------------------public void displayLink()
// display this link
{ System.out.print(dData + ); }
// ------------------------------------------------------------} // end class Link
////////////////////////////////////////////////////////////////
class FirstLastList
{
private Link first;
// ref to first item
private Link last;
// ref to last item
// ------------------------------------------------------------public FirstLastList()
// constructor
{
first = null;
// no items on list yet
last = null;
}
// ------------------------------------------------------------public boolean isEmpty()
// true if no links
{ return first==null; }

// ------------------------------------------------------------public void insertLast(long dd)


// insert at end of list
{
Link newLink = new Link(dd); // make new link
if( isEmpty() )
// if empty list,
first = newLink;
// first --> newLink
else
last.next = newLink;
// old last --> newLink
last = newLink;
// newLink <-- last
}
// ------------------------------------------------------------public long deleteFirst()
// delete first link
{
// (assumes non-empty list)
long temp = first.dData;
if(first.next == null)
// if only one item
last = null;
// null <-- last
first = first.next; // first --> old next
return temp;
}
// ------------------------------------------------------------public void displayList()
{
Link current = first;
// start at beginning
while(current != null)
// until end of list,
{
current.displayLink();
// print data
current = current.next;
// move to next link
}
System.out.println();
}
// ------------------------------------------------------------} // end class FirstLastList
////////////////////////////////////////////////////////////////
class LinkQueue
{
private FirstLastList theList;
//-------------------------------------------------------------public LinkQueue()
// constructor
{ theList = new FirstLastList(); } // make a 2-ended list
//-------------------------------------------------------------public boolean isEmpty()
// true if queue is empty
{ return theList.isEmpty(); }
//-------------------------------------------------------------public void insert(long j)
// insert, rear of queue
{ theList.insertLast(j); }
//-------------------------------------------------------------public long remove()
// remove, front of queue
{ return theList.deleteFirst(); }
//-------------------------------------------------------------public void displayQueue()
{

System.out.print(Queue (front-->rear): );
theList.displayList();
}
//-------------------------------------------------------------} // end class LinkQueue
////////////////////////////////////////////////////////////////
class LinkQueueApp
{
public static void main(String[] args)
{
LinkQueue theQueue = new LinkQueue();
theQueue.insert(20);
// insert items
theQueue.insert(40);
theQueue.displayQueue();
// display queue
theQueue.insert(60);
// insert items
theQueue.insert(80);
theQueue.displayQueue();
// display queue
theQueue.remove();
// remove items
theQueue.remove();
theQueue.displayQueue();
// display queue
} // end main()
////////////////////////////////////////////////////////////////
, ,
. .
Queue (front-->rear): 20 40
Queue (front-->rear): 20 40 60 80
Queue (front-->rear): 60 80
insert() remove() LinkQueue
insertLast() deleteFirst() FirstLastList. queue.java
.
linkStack.java linkQueue.java
, .
.

.
3.6
.
.
int double.
:
. , int
-2,147483,648 +2,147,483,647 +,-,*,/ .
.
-
. , ( ,

), ( )
( ).
int double,
add() sub() + -.
.
.
()
().
, ,
. int
.
.
.
.
.
-
. () ,
() .
.
,
.

. ,
.

.
, push() pop() ( )
.
, .
.
public . push(),
pop() .
3.6.1
.
, . ( )
, .
, . ,
.
.
, .
, .
.
3.6.2
.

. ,
, , .

.
.

.
. ,

. .
3.7
.
.
.
() ,
. find() delete()
.
.
(
)
, .
.
,
heap.
3.7.1

:

, next
, next
. , :
.
:
public void insert(long key)
{
Link newLink = new Link(key);
Link previous = null; // start at first
Link current = first;

// insert in order
// make new link

// until end of list,


while(current != null && key > current.dData)
{
previous = current;
current = current.next;
}
if(previous==null)
first = newLink;
else
previous.next = newLink;
newLink.next = current;
} // end insert()

// or key > current,


// go to next item
// at beginning of list
// first --> newLink
// not at beginning
// old prev --> newLink
// newLink --> old current

previous next
previous .
. current first
previous null. previous null null
.
while
.
(current.dData) (key). While
current null. ( next
null) (first null).
while , ,
. previous null,
first . ,
previous.next .
next current. current null, next
.
sortedList.java SortedList insert(),
remove() displayList(). insert()
.
// sortedList.java
// demonstrates sorted list
// to run this program: C>java SortedListApp
////////////////////////////////////////////////////////////////
class Link
{
public long dData;
public Link next;
// ------------------------------------------------------------public Link(long dd)
{ dData = dd; }
// ------------------------------------------------------------public void displayLink()
{ System.out.print(dData + ); }
} // end class Link
////////////////////////////////////////////////////////////////
class SortedList
{
private Link first;
// ------------------------------------------------------------public SortedList()
{ first = null; }
// ------------------------------------------------------------public boolean isEmpty()
{ return (first==null); }
// ------------------------------------------------------------public void insert(long key)
{
Link newLink = new Link(key);
Link previous = null;
Link current = first;

// data item
// next link in list
// constructor

// display this link

// ref to first item on list


// constructor

// true if no links

// insert, in order
// make new link
// start at first

// until end of list,


while(current != null && key > current.dData)
{
previous = current;
current = current.next;
}
if(previous==null)
first = newLink;
else
previous.next = newLink;
newLink.next = current;
} // end insert()
// ------------------------------------------------------------public Link remove()
{
Link temp = first;
first = first.next;
return temp;
}
// ------------------------------------------------------------public void displayList()
{
System.out.print(List (first-->last): );
Link current = first;
while(current != null)
{
current.displayLink();
current = current.next;
}
System.out.println();
}
} // end class SortedList
////////////////////////////////////////////////////////////////
class SortedListApp
{
public static void main(String[] args)
{
SortedList theSortedList = new SortedList();
theSortedList.insert(20);
theSortedList.insert(40);
theSortedList.displayList();
theSortedList.insert(10);
theSortedList.insert(30);
theSortedList.insert(50);
theSortedList.displayList();
theSortedList.remove();
theSortedList.displayList();
} // end main()
} // end class SortedListApp
////////////////////////////////////////////////////////////////

// or key > current,


// go to next item
// at beginning of list
// first --> newLink
// not at beginning
// old prev --> newLink
// newLink --> old current

// return & delete first link


// (assumes non-empty list)
// save first
// delete first
// return value

// start at beginning of list


// until end of list,
// print data
// move to next link

// create new list


// insert 2 items
// display list
// insert 3 more items

// display list
// remove an item
// display list

main() 20 40. ,
10,30 50. ,
, insert() . ,
, .
, .
List (first-->last): 20 40
List (first-->last): 10 20 30 40 50
List (first-->last): 20 30 40 50

O(N) (N/2 )
. (1)
.
.
.
3.7.2 (List Insertion Sort)
.
.
.
, .
,
. O(N2)

, N , N2/4
. :
. N*2 N2
. ListInsertionSort.java
, (
) .
// listInsertionSort.java
// demonstrates sorted list used for sorting
// to run this program: C>java ListInsertionSortApp
////////////////////////////////////////////////////////////////
class Link
{
public long dData;
public Link next;
// ------------------------------------------------------------public Link(long dd)
{ dData = dd; }
// ------------------------------------------------------------} // end class Link
////////////////////////////////////////////////////////////////
class SortedList
{
private Link first;
// -------------------------------------------------------------

// data item
// next link in list
// constructor

// ref to first item on list

public SortedList()
{ first = null; }
// ------------------------------------------------------------public SortedList(Link[] linkArr)
{
first = null;
for(int j=0; j<linkArr.length; j++)
insert( linkArr[j] );
}
// ------------------------------------------------------------public void insert(Link k)
{
Link previous = null;
Link current = first;

// constructor (no args)


// initialize list
// constructor (array
// as argument)
// initialize list
// copy array
// to list

// insert (in order)


// start at first

// until end of list,


while(current != null && k.dData > current.dData)
{
// or key > current,
previous = current;
current = current.next;
// go to next item
}
if(previous==null)
// at beginning of list
first = k;
// first --> k
else
// not at beginning
previous.next = k;
// old prev --> k
k.next = current;
// k --> old current
} // end insert()
// ------------------------------------------------------------public Link remove()
// return & delete first link
{
// (assumes non-empty list)
Link temp = first;
// save first
first = first.next;
// delete first
return temp;
// return value
}
// ------------------------------------------------------------} // end class SortedList
////////////////////////////////////////////////////////////////
class ListInsertionSortApp
{
public static void main(String[] args)
{
int size = 10;
// create array of links
Link[] linkArray = new Link[size];
for(int j=0; j<size; j++)
// fill array with links
{
// random number
int n = (int)(java.lang.Math.random()*99);
Link newLink = new Link(n);
// make link
linkArray[j] = newLink;
// put in array
}
// display array contents
System.out.print(Unsorted array: );

for(int j=0; j<size; j++)


System.out.print( linkArray[j].dData + );
System.out.println();
// create new list
// initialized with array
SortedList theSortedList = new SortedList(linkArray);
for(int j=0; j<size; j++)
// links from list to array
linkArray[j] = theSortedList.remove();
// display array contents
System.out.print(Sorted Array: );
for(int j=0; j<size; j++)
System.out.print(linkArray[j].dData + );
System.out.println();
} // end main()
} // end class ListInsertionSortApp
////////////////////////////////////////////////////////////////
.
Unsorted array: 59 69 41 56 84 15 86 81 37 35
Sorted array: 15 35 37 41 56 59 69 81 84 86

.
SortedList Link
.
main().
, insert() .
Link, long. Link
. sortedList.java insert()
Link , long .

:
.
3.8
.
.
current=current.next
,
. .
, .
String .
.
current ,
. .
.
3.8 .

3,8:
Link
class Link
{
public long dData; // data item
public Link next; // next link in list
public link previous; // previous link in list
...
}

:
. .
(
) , .
doublyLinkedList
doublyLinked.java.
38.1
.
displayForward() displayList() .
displayBackward()
previous.

Link current = last;


// start at end
while(current != null)
// until start of list,
current = current.previous; // move to previous link
38.2
DoublyLinkedList . insertFirst()
, insertLast() insertAfter()
.
insetFirst() previous
next first .
first . 3.9.

3.9:
last first.previous.
if( isEmpty() )
last = newLink;
else
first.previous = newLink;
newLink.next = first;
first = newLink;

// if empty list,
// newLink <-- last
// newLink <-- old first
// newLink --> old first
// first --> newLink

insertLast() insertFirst().
insertAfter() .
.
.
find() linkList2.java.
current .
3.10.
next
null, last . insertAfter()
:
if(current==last)
// if last link,
{
newLink.next = null;
// newLink --> null
last = newLink;
// newLink <-- last
}
else
// not last link,
{
newLink.next = current.next;
// newLink --> old next
// newLink <-- old next
current.next.previous = newLink;

}
newLink.previous = current;
current.next = newLink;

// old current <-- newLink


// old current --> newLink

current.next.previous previous
next current.

3.10:

3.8.3
: deleteFirst(), deleteLast() deleteKey().
. deleteKey()
current. ,
next current.previous ( )
current.next ( ). previous
current.next current.previous.
current . 3.11 ,

current.previous.next = current.next;
current.next.previous = current.previous;

3.11:

first last
. deleteKey()
if(current==first)
first = current.next;
else

// first item?
// first --> old next
// not first
// old previous --> old next

current.previous.next = current.next;
if(current==last)
last = current.previous;
else

// last item?
// old previous <-- last
// not last
// old previous <-- old next

current.next.previous = current.previous;
doublyLinked.java
.
// doublyLinked.java
// demonstrates doubly-linked list
// to run this program: C>java DoublyLinkedApp
////////////////////////////////////////////////////////////////
class Link
{
public long dData;
// data item
public Link next;
// next link in list
public Link previous;
// previous link in list
// ------------------------------------------------------------public Link(long d)
// constructor
{ dData = d; }
// ------------------------------------------------------------public void displayLink()
// display this link
{ System.out.print(dData + ); }
// ------------------------------------------------------------} // end class Link
////////////////////////////////////////////////////////////////
class DoublyLinkedList
{
private Link first;
// ref to first item
private Link last;
// ref to last item
// ------------------------------------------------------------public DoublyLinkedList()
// constructor
{
first = null;
// no items on list yet
last = null;
}
// ------------------------------------------------------------public boolean isEmpty()
// true if no links
{ return first==null; }

// ------------------------------------------------------------public void insertFirst(long dd)


// insert at front of list
{
Link newLink = new Link(dd);
// make new link
if( isEmpty() )
last = newLink;
else
first.previous = newLink;
newLink.next = first;
first = newLink;

// if empty list,
// newLink <-- last
// newLink <-- old first
// newLink --> old first
// first --> newLink

}
// ------------------------------------------------------------public void insertLast(long dd)
// insert at end of list
{
Link newLink = new Link(dd);
// make new link
if( isEmpty() )
// if empty list,
first = newLink;
// first --> newLink
else
{
last.next = newLink;
// old last --> newLink
newLink.previous = last;
// old last <-- newLink
}
last = newLink; // newLink <-- last
}
// ------------------------------------------------------------public Link deleteFirst()
// delete first link
{
// (assumes non-empty list)
Link temp = first;
if(first.next == null)
// if only one item
last = null;
// null <-- last
else
first.next.previous = null;
// null <-- old next
first = first.next;
// first --> old next
return temp;
}
// ------------------------------------------------------------public Link deleteLast()
// delete last link
{
// (assumes non-empty list)
Link temp = last;
if(first.next == null)
// if only one item
first = null;
// first --> null
else
last.previous.next = null;
// old previous --> null
last = last.previous;
// old previous <-- last
return temp;
}
// ------------------------------------------------------------// insert dd just after key
public boolean insertAfter(long key, long dd)
{
// (assumes non-empty list)

Link current = first;


while(current.dData != key)
{
current = current.next;
if(current == null)
return false;
}
Link newLink = new Link(dd);
if(current==last) // if last link,
{
newLink.next = null;
last = newLink;
}
else
{
newLink.next = current.next;

// start at beginning
// until match is found,
// move to next link
// didnt find it
// make new link

// newLink --> null


// newLink <-- last
// not last link,
// newLink --> old next
// newLink <-- old next

current.next.previous = newLink;
}
newLink.previous = current;
// old current <-- newLink
current.next = newLink;
// old current --> newLink
return true;
// found it, did insertion
}
// ------------------------------------------------------------public Link deleteKey(long key)
// delete item w/ given key
{
// (assumes non-empty list)
Link current = first;
// start at beginning
while(current.dData != key)
// until match is found,
{
current = current.next;
// move to next link
if(current == null)
return null;
// didnt find it
}
if(current==first)
// found it; first item?
first = current.next;
// first --> old next
else
// not first
// old previous --> old next
current.previous.next = current.next;
if(current==last)
// last item?
last = current.previous;
// old previous <-- last
else
// not last
// old previous <-- old next
current.next.previous = current.previous;
return current;
// return value
}
// ------------------------------------------------------------public void displayForward()
{
System.out.print(List (first-->last): );
Link current = first;
// start at beginning
while(current != null)
// until end of list,

{
current.displayLink();
// display data
current = current.next;
// move to next link
}
System.out.println();
}
// ------------------------------------------------------------public void displayBackward()
{
System.out.print(List (last-->first): );
Link current = last;
// start at end
while(current != null)
// until start of list,
{
current.displayLink();
// display data
current = current.previous;
// move to previous link
}
System.out.println();
}
// ------------------------------------------------------------} // end class DoublyLinkedList
////////////////////////////////////////////////////////////////
class DoublyLinkedApp
{
public static void main(String[] args)
{
// make a new list
DoublyLinkedList theList = new DoublyLinkedList();
theList.insertFirst(22);
// insert at front
theList.insertFirst(44);
theList.insertFirst(66);
theList.insertLast(11);
// insert at rear
theList.insertLast(33);
theList.insertLast(55);
theList.displayForward();
theList.displayBackward();

// display list forward


// display list backward

theList.deleteFirst();
theList.deleteLast();
theList.deleteKey(11);

// delete first item


// delete last item
// delete item with key 11

theList.displayForward();
theList.insertAfter(22, 77);
theList.insertAfter(33, 88);
theList.displayForward();
} // end main()
} // end class DoublyLinkedApp
////////////////////////////////////////////////////////////////

// display list forward


// insert 77 after 22
// insert 88 after 33
// display list forward

main() ,
,

11. ( ),
insertAfter() .
List (first-->last): 66 44 22 11 33 55
List (last-->first): 55 33 11 22 44 66
List (first-->last): 44 22 33
List (first-->last): 44 22 77 33 88
insertAfter() .
main() ,
isEmpty() .
3.8.4 deque
deque .
deque ,
.
3.9
find().

.
, .
.
,
.

.

. . find()
.

.
.
.
current .

.
. .
.
3.9.1

(
-enumerators).
class ListIterator()
{
private Link current; ...
}

current . (
C++ .)

. getIterator()
. main()
:
public static void main(...)
{
LinkList theList = new LinkList();
ListIterator iter1 = theList.getIterator();
Link aLink = iter1.getCurrent();
iter1.nextLink();
}

// make list
// make iter
// access link at iterator
// move iter to next link


. iter1
(iter2 ) .
.
. 3.12 .

3.12
previous
.
.
first
.
first .

.
.
public
first. LinkList getFirst() setFirst().
first.
reset() nextLink()
class ListIterator()
{
private Link current;
private Link previous;
private LinkList ourList;

// reference to current link


// reference to previous link
// reference to parent list

public void reset()


// set to start of list
{
current = ourList.getFirst();
// current --> first
previous = null;
// previous --> null
}
public void nextLink()
// go to next link
{
previous = current;
// set previous to this
current = current.next;
// set this to next
}
...
}
C++
(friend) . .
.
insertAfter() .
:
reset()
nextLink()
getCurrent()
atEnd() true
insertAfter()
insertBefore()
deleteCurrent()
reset() nextLink(),
atEnd() .
.
insertBefore() , insertFirst()
.
displayList() getCurrent()
nextLink() .
interIterator.java
.
:
s
r -
n

g -
b
a
d
interIterator.java
// interIterator.java
// demonstrates iterators on a linked listListIterator
// to run this program: C>java InterIterApp
import java.io.*; // for I/O
////////////////////////////////////////////////////////////////
class Link
{
public long dData;
// data item
public Link next;
// next link in list
// ------------------------------------------------------------public Link(long dd)
// constructor
{ dData = dd; }
// ------------------------------------------------------------public void displayLink()
// display ourself
{ System.out.print(dData + ); }
} // end class Link
////////////////////////////////////////////////////////////////
class LinkList
{
private Link first;
// ref to first item on list
// ------------------------------------------------------------public LinkList()
// constructor
{ first = null; }
// no items on list yet
// ------------------------------------------------------------public Link getFirst()
// get value of first
{ return first; }
// ------------------------------------------------------------public void setFirst(Link f)
// set first to new link
{ first = f; }
// ------------------------------------------------------------public boolean isEmpty()
// true if list is empty
{ return first==null; }
// ------------------------------------------------------------public ListIterator getIterator()
// return iterator
{
return new ListIterator(this);
// initialized with
} // this list
// ------------------------------------------------------------public void displayList()
{
Link current = first;
// start at beginning of list
while(current != null)
// until end of list,
{
current.displayLink();
// print data

current = current.next;
// move to next link
}
System.out.println();
}
// ------------------------------------------------------------} // end class LinkList
////////////////////////////////////////////////////////////////
class ListIterator
{
private Link current;
// current link
private Link previous;
// previous link
private LinkList ourList;
// our linked list
//-------------------------------------------------------------public ListIterator(LinkList list)
// constructor
{
ourList = list;
reset();
}
//-------------------------------------------------------------public void reset()
// start at first
{
current = ourList.getFirst();
previous = null;
}
//-------------------------------------------------------------public boolean atEnd()
// true if last link
{ return (current.next==null); }
//-------------------------------------------------------------public void nextLink()
// go to next link
{
previous = current;
current = current.next;
}
//-------------------------------------------------------------public Link getCurrent()
// get current link
{ return current; }
//-------------------------------------------------------------public void insertAfter(long dd)
// insert after
{
// current link
Link newLink = new Link(dd);
if( ourList.isEmpty() )
// empty list
{
ourList.setFirst(newLink);
current = newLink;
}
else
// not empty
{
newLink.next = current.next;
current.next = newLink;
nextLink();
// point to new link
}

}
//-------------------------------------------------------------public void insertBefore(long dd)
// insert before
{
// current link
Link newLink = new Link(dd);
if(previous == null)
// beginning of list
{
// (or empty list)
newLink.next = ourList.getFirst();
ourList.setFirst(newLink);
reset();
}
else
// not beginning
{
newLink.next = previous.next;
previous.next = newLink;
current = newLink;
}
}
//-------------------------------------------------------------public long deleteCurrent()
// delete item at current
{
long value = current.dData;
if(previous == null)
// beginning of list
{
ourList.setFirst(current.next);
reset();
}
else
// not beginning
{
previous.next = current.next;
if( atEnd() )
reset();
else
current = current.next;
}
return value;
}
//-------------------------------------------------------------} // end class ListIterator
////////////////////////////////////////////////////////////////
class InterIterApp
{
public static void main(String[] args) throws IOException
{
LinkList theList = new LinkList();
// new list
ListIterator iter1 = theList.getIterator(); // new iter
long value;
iter1.insertAfter(20);
// insert items
iter1.insertAfter(40);
iter1.insertAfter(80);
iter1.insertBefore(60);

while(true)
{
System.out.print(Enter first letter of show, reset, );
System.out.print(next, get, before, after, delete: );
System.out.flush();
int choice = getChar();
// get users option
switch(choice)
{
case s:
// show list
if( !theList.isEmpty() )
theList.displayList();
else
System.out.println(List is empty);
break;
case r:
// reset (to first)
iter1.reset();
break;
case n:
// advance to next item
if( !theList.isEmpty() && !iter1.atEnd() )
iter1.nextLink();
else
System.out.println(Cant go to next link);
break;
case g:
// get current item
if( !theList.isEmpty() )
{
value = iter1.getCurrent().dData;
System.out.println(Returned + value);
}
else
System.out.println(List is empty);
break;
case b:
// insert before current
System.out.print(Enter value to insert: );
System.out.flush();
value = getInt();
iter1.insertBefore(value);
break;
case a:
// insert after current
System.out.print(Enter value to insert: );
System.out.flush();
value = getInt();
iter1.insertAfter(value);
break;
case d:
// delete current item
if( !theList.isEmpty() )
{
value = iter1.deleteCurrent();
System.out.println(Deleted + value);
}
else

System.out.println(Cant delete);
break;
default:
System.out.println(Invalid entry);
} // end switch
} // end while
} // end main()
//-------------------------------------------------------------public static String getString() throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
//------------------------------------------------------------public static char getChar() throws IOException
{
String s = getString();
return s.charAt(0);
}
//------------------------------------------------------------public static int getInt() throws IOException
{
String s = getString();
return Integer.parseInt(s);
}
//------------------------------------------------------------} // end class InterIterApp
////////////////////////////////////////////////////////////////
main()
insertAfter(). .
, , ,
, ( 60), 100
, 7 100 .
Enter first letter of
show, reset, next, get, before, after, delete: s
20 40 60 80
Enter first letter of
show, reset, next, get, before, after, delete: r
Enter first letter of
show, reset, next, get, before, after, delete: n
Enter first letter of
show, reset, next, get, before, after, delete: n
Enter first letter of
show, reset, next, get, before, after, delete: g
Returned 60
Enter first letter of
show, reset, next, get, before, after, delete: b

Enter value to insert: 100


Enter first letter of
show, reset, next, get, before, after, delete: a
Enter value to insert: 7
Enter first letter of
show, reset, next, get, before, after, delete: s
20 40 100 7 60 80
deleteCurrent()
.
.
.
insertBefore() insertAfter() current .
attend() . true
.

(
).

. (
.) atEend() true
.
.

.
displayList():
iter1.reset();
long value = iter1.getCurrent().dData;
System.out.println(value + );
while( !iter1.atEnd() )
{
iter1.nextLink();
long value = iter1.getCurrent().dData;
System.out.println(value + );
}

// start at first
// display link
// until end,
// go to next link,
// display it

isEmpty()
getCurrent().
3.
main() interIterator.java.
class InterIterApp
{
public static void main(String[] args) throws IOException
{
LinkList theList = new LinkList();
// new list
ListIterator iter1 = theList.getIterator();
// new iter
iter1.insertAfter(21);
// insert links
iter1.insertAfter(40);
iter1.insertAfter(30);
iter1.insertAfter(7);

iter1.insertAfter(45);
theList.displayList();
iter1.reset(); // start at first link
Link aLink = iter1.getCurrent();
if(aLink.dData % 3 == 0)
iter1.deleteCurrent();
while( !iter1.atEnd() )
{
iter1.nextLink();
aLink = iter1.getCurrent();
if(aLink.dData % 3 == 0)
iter1.deleteCurrent();
}
theList.displayList();
} // end main()
} // end class InterIterApp

// display list
// get it
// if divisible by 3,
// delete it
// until end of list,
// go to next link
// get link
// if divisible by 3,
// delete it
// display list

.
3 .
21 40 30 7 45
40 7
,
deleteCurrent().
, find()
. replace()
.

.
.

You might also like