Вы находитесь на странице: 1из 31

Lists

List: finite sequence of data elements


all elements have the same data type

The operations depend on the type of


the list and not on the data type
List: the most general type
Ordered: element is ascending order

Stacks, Queues: not all operations are


allowed
E.G.M. Petrakis

lists

list or
head

rear or
tail

current
info

list

next

Insertion after current


position

new element
E.G.M. Petrakis

lists

list or
head

list

current

rear or
tail

Insertion at current
position

new element
E.G.M. Petrakis

lists

list or
head

list or
head

rear or
tail

current

Deletion after current


position

deleted
element
E.G.M. Petrakis

lists

list or
head

list or
head

rear or
tail

current

Deletion at current
position

deleted
element
E.G.M. Petrakis

lists

Terminology
empty: contains no elements
length: number of elements in list
head, or list: pointer to the beginning
of the list
tail: pointer to the last element
current: pointer to current element
ordered: elements in ascending or
descending order
E.G.M. Petrakis

lists

Operations

setFirst: set current to head


setPos(i): sets current to the i-th element
currValue: returns value of current element
next/prev: current points to next/previous element
clear: delete all elements
insert: inserts an element at/after current position
append: inserts an element at tail
remove: delete the element at/after current position
isInList: true/false if current position is in list
isEmpty: true/false if list is empty

E.G.M. Petrakis

lists

ADT List
interface List {
public void clear();
public void insert(Object item);
public void append(Object item);
public Object remove();
public void setFirst();
public void next();
public void prev();
public int length();
public void setPos(int pos);
public void setValue(Object val);
public Object currValue();
public boolean isEmpty();
public boolean isInList();
}

E.G.M. Petrakis

lists

// List ADT in Java


// Remove all Objects
// Insert at curr pos
// Insert at tail
// Remove/return curr
// Set to first pos
// Move to next pos
// Move to prev pos
// Return curr length
// Set curr position
// Set current value
// Return curr value
// True if empty list
// True if in list
// interface List

Iteration
Iterate through the whole list : MyList
for (MyList.first( ); MyList.isInList( ); MyList.next( ))
DoSomething(MyList.currValue( ));

If MyList: (12 32 15) and current points to


32 then MyList.insert(90) changes the list
to be (12 32 90 15)

E.G.M. Petrakis

lists

List Implementations
Array-based: very fast
the elements are stored in array
static: actual number of elements less than size
allocated

Dynamic Memory: slower, more efficient

allocates memory for new elements


dynamic: no restriction on number of elements
(except memory size)

E.G.M. Petrakis

lists

10

Array Implementation (1)


Elements in continuous array positions
Head of list at pos 0
Insertion and Deletion cause shifting of
elements

E.G.M. Petrakis

lists

11

class AList implements List { // Array-based list class


private static final int defaultSize = 10;
private int msize;
private int numInList;
private int curr;
private Object[] listArray;

// Maximum size of list


// Actual list size
// Position of curr
// Array holding list

AList() { setup(defaultSize); }
AList(int sz) { setup(sz); }

// Constructor
// Constructor

private void setup(int sz) {


msize = sz;
numInList = curr = 0;
ListArray = new Object[sz];
}

// Do initializations

public void clear()


{ numInList = curr = 0; }

// Remove all Objects from list


// Simply reinitialize values

E.G.M. Petrakis

// Create listArray

lists

12

public void insert(Object it) {


// Insert at curr pos
Assert.notFalse(numInList < msize, "List is full");
Assert.notFalse((curr >=0) && (curr <= numInList), "Bad value for curr");
for (int i=numInList; i>curr; i--)
// Shift up
listArray[i] = listArray[i-1];
listArray[curr] = it;
numInList++;
// Increment list size
}
public void append(Object it) {
// Insert at tail
Assert.notFalse(numInList < msize, "List is full");
listArray[numInList++] = it;
// Increment list size
}
public Object remove() {
// Remove and return Object
Assert.notFalse(!isEmpty(), "No delete: list empty");
Assert.notFalse(isInList(), "No current element");
Object it = listArray[curr];
// Hold removed Object
for (int i=curr; i<numInList-1; i++)
// Shift down
listArray[i] = listArray[i+1];
numInList--;
// Decrement list size
return it;
}

E.G.M. Petrakis

lists

13

public
public
public
public
public
public

void setFirst() { curr = 0; }


// Set to first
void prev() { curr--; }
// Move curr to prev
void next() { curr++; }
// Move curr to next
int length() { return numInList; }
void setPos(int pos) { curr = pos; }
boolean isEmpty() { return numInList == 0; }

public void setValue(Object it) {


// Set current value
Assert.notFalse(isInList(), "No current element");
listArray[curr] = it;
}
public boolean isInList()
// True if curr within list
{ return (curr >= 0) && (curr < numInList); }

} // Array-based list implementation

E.G.M. Petrakis

lists

14

Array Implementation (2)


list

1
8
9

11

26

11

10 11 12

nodes

5 10
12 26 0
11

NULL

13
E.G.M. Petrakis

lists

15

List 1

31

List 2

17

26

List 3

31

19

32

List 4

18

13

E.G.M. Petrakis

14

lists

37

11

12

15
16

List 4
List 2

26

11

10

16

25

17

13

19

19

14

13

10

22

12

31

13

16

37

24

17

12

32

21

22

15

List 3

11

14
15

List 1

18
19
20

23

E.G.M. Petrakis

24

12

25

18

lists

0
6

17

class AList implements List { // Array-based list class


private static final int defaultSize = 10;
private int msize;
// Maximum size of list
private int numInList;
// Actual list size
private int curr;
// Position of curr
private int avail;
// next available position
private Object[] listArray;
// Array holding list
private int[] listarray_next;
// array holding pointers to next Object
private void setup(int sz) {
// Do initializations
msize = sz;
numinlist = curr = 0;
listarray = new Object[sz];
listarray_next = new int[sz];
avail = 0;
// the first available element
for (i=0; i < msize; i++)
listarray_next[i] = i+1;
// each elem points to its successor
listarray_next[msize-1] = nothing;
// the last elem has no next
}

E.G.M. Petrakis

lists

18

private int get_node( ) {


if (avail == nothing)
error(list overflow)
else {
int pos = avail;
avail = listarray_next[avail];
return pos;
}
}

// Get next available node


// from stack

private void free_node (int p) {


listarray_next[p] = avail;
avail =p;
}

// make node available


// push node back to stack

private void insert(int p, Object x) { // insert after node pointed to by p


if (p < 0 || p >= msize)
error (void insertion)
else {
int q = get_node( );
listarray[q] = x;
listarray_next[q] = listarray_next[p];
listarray_next[p] = q;
}
}

E.G.M. Petrakis

lists

19

private void delete(int p, Object x) {


if ( p > 0 || p >= msize)
// deletes elem after elem
error (void deletion);
// pointed to by p
else {
int q = listarray_next[p];
x = listarray[q];
listarray_next[p] = listarray_next[q];
free_node(q);
}
}
public AList() { setup(defaultSize); } // Constructor
public AList(int sz) { setup(sz); }
// Constructor
public
public
public
public
public
public
public
public

void clear( ) {}
void insert(Object it) {}
void append(Object it) {}
Object remove( ) {}
void setFirst( ) {}
void prev( ) {}
void next( ) {}
int length( ) {}

// remove all ELEMs from list


// insert ELEM at current position
// insert ELEM at tail of list
// remove and return current ELEM
// set curr to first position;
// move curr to previous position;
// move curr to next position;
// return current length of list

} // Array-based list implementation


E.G.M. Petrakis

lists

20

Dynamic Memory
Allocates memory for new elements as needed
Each node is a distinct object

The node class

// A linked-list node
// Object for this node
// Pointer to next node
// Constructor

class Link {

private Object element;


private Link next;
Link(Object it, Link nextval)
{ element = it; next = nextval; }
Link(Link nextval) { next = nextval; } // Constructor
Link next() { return next; }
Link setNext(Link nextval) { return next = nextval; }
Object element() { return element; }
Object setElement(Object it) { return element = it; }

E.G.M. Petrakis

lists

21

class LList implements List { // Linked list class


private Link head;
private Link tail;
protected Link curr;

// Pointer to list header


// Pointer to last Object in list
// Position of current Object

LList(int sz) { setup(); }


// Constructor
LList() { setup(); }
// Constructor
private void setup()
{ tail = head = curr = new Link(null); }
public void setFirst() { curr = head; }
public void next() { if (curr != null) curr = curr.next(); }
public void prev() {
// Move to previous position
Link temp = head;
if ((curr == null) || (curr == head)) // No prev
{ curr = null; return; }
// so return
while ((temp != null) && (temp.next() != curr))
temp = temp.next();
curr = temp;
}
E.G.M. Petrakis

lists

22

public Object currValue() {


if (!isInList()) return null;
return curr.next().element();
}

// Return current Object

public boolean isEmpty()


{ return head.next() == null; }

// True if list is empty

public void insert(Object it) {


// Insert Object at current position
Assert.notNull(curr, "No current element");
curr.setNext(new Link(it, curr.next()));
if (tail == curr)
// Appended new Object
tail = curr.next();
}
public Object remove() {
if (!isInList()) return null;
Object it = curr.next().element();
if (tail == curr.next()) tail = curr;
curr.setNext(curr.next().next());
return it;
}
E.G.M. Petrakis

// Remove/return curr Object


// Remember value
// Set tail
// Cut from list
// Return value
lists

23

public void append(Object it)


// insert Elem at tail of list
{ tail.setNext(new Link(it, NULL)); }
public int length( ) {
// return current length of list
int cnt = 0;
for (Link temp = head.next(); temp != NULL; temp = temp.next())
cnt++;
// count Elems
return cnt;
}
public void setPos(int pos) {
// set curr to position
curr = head;
for (int i = 0; (curr != NULL) && (i < pos) i++) curr = curr.next();
}
public void setValue(Object val) {
// set current Elem's value
Assert.notFalse(isInList(), "No current element");
curr.next().setElement(val);
}
public bool isInList( )
// TRUE if curr is within list
{ return (curr != NULL) && (curr.next() != NULL); }

} // Linked list class implementation

E.G.M. Petrakis

lists

24

Comparison
Array-Based Lists:

insertion and deletion are (n)


prev and direct access are (1)
fixed space allocated in advance
space reorganization if the array is full
faster in most cases

insertion and deletion are (1)


prev and direct access are (n)
space grows with number of elements
every element requires overhead
slower

Linked Lists:

E.G.M. Petrakis

lists

25

Doubly Linked List


Allows for direct access to both next
and previous elements of the current
pointer
insert (delete) operations update both
next and prev pointers
easy implementation

E.G.M. Petrakis

lists

26

Doubly linked list node


class DLink { // A doubly-linked list node
private Object element;
// Object for this node private
DLink next;
// Ptr to next node
DLink prev;
// Ptr to previous node
DLink(Object it, DLink n, DLink p)
// Constr. 1
{ element = it; next = n; prev = p; }
DLink(DLink n, DLink p) { next = n; prev = p; }
// Constr. 2
DLink next() { return next; }
DLink setNext(DLink nextval) { return next = nextval; }
DLink prev() { return prev; }
DLink setPrev(DLink prevval) { return prev = prevval; }
Object element() { return element; }
Object setElement(Object it) { return element = it; }
}
E.G.M. Petrakis

lists

27

Insertion in Doubly Linked List


current

current

E.G.M. Petrakis

lists

28

Deletion in Doubly Linked List


current

current

E.G.M. Petrakis

lists

29

Insertion
curr.setNext(new DLink(it, curr.next(), curr));
if (curr.next().next() != null)
curr.next().next().setPrev(curr.next());
if (tail == curr)
// Appended new Object
tail = curr.next();

E.G.M. Petrakis

lists

30

Circular Linked Lists


The next pointer of the last element points
to the first element

E.G.M. Petrakis

lists

31

Вам также может понравиться