Fundamentals of Computer Science II (CSC-152 97F)

[News] [Basics] [Syllabus] [Outlines] [Assignments] [Examples] [Readings] [Bailey Docs]

# Outline of Class 27: Lists, concluded

## Implementing Lists with Arrays

• In the previous class session, we started to talk about how to implement lists with arrays. In today's session, we'll look more at those topics.
• We may begin with an example, using the folks in the class.
• Note that our implementation will be for lists of integers, using a pair of arrays for contents and links.

### Initialization

• To initialize an array-based list implementation, we need to
• allocate the array(s)
• set up the free list
• note that the list is currently empty
• In Java code,
```/** Initialize a list with a maximum size */
public IntList(int maxsize) throws Exception {
// Create the two arrays
contents = new int[maxsize];
if (contents == null) {
throw new Exception("Insufficient memory");
}
next = new int[maxsize];
if (next == null) {
contents = null;	// So it can be reclaimed
throw new Exception("Insufficient memory");
}
// Set up the free list
for(int i = 0; i < maxsize-2; ++i) {
next[i] = i+1;
}
next[maxsize-1] = NULL_INDEX;
for(int i = 1; i < maxsize-1; ++i) {
prev[i] = i-1;
}
prev[0] = NULL_INDEX;
// Set up the actual list
front = NULL_INDEX;
back = NULL_INDEX;
} // IntList(int)
```

### Insertion

• Insertion in array-based lists is similar to insertion in singly-linked lists. The main difference is in how we allocate the new element.
• We'll look at insertion at the head of a list
```public void insertAtHead(int i) throws ListException {
// Sanity check
if (free == NULL_INDEX) {
throw new ListException("Insufficient room");
}
// Grab the front of the free list
int new_front = free;
free = next[free];
if (free != NULL_INDEX {
prev[free] = NULL_INDEX;
}
// Update the contents and next pointer of the new element
contents[new_front] = i;
next[new_front] = front;
prev[new_front] = NULL_INDEX;
// Update the prev pointer of the old front
if (front != NULL_INDEX) { prev[front] = new_front; }
// Update the front of the lsit
front = new_front;
```

### Deletion

• A few of you asked how the positions of elements in the list can differ from their positions in the array. The answer is that as we delete elements, we begin to lost some of that equivalence (particularly as we want to guarantee constant-time deletion). More importantly, as we then allocate new elements, we may be allocating from different locations.
• Here is a delete from end of list routine. The delete from head is similar.
```public int deleteLast() throws ListException {
// Sanity check
if (front == NULL_INDEX) {
throw new ListException("Can't delete from empty list");
}
// The index of the element we're deleting
int deleting = last;
// Update the last element
last = prev[last];
if (last != NULL_INDEX]) { next[last] = NULL_INDEX; }
// Update the front if necessary
if (front == deleting) { front = NULL_INDEX; }
// Update the free list by placing deleted element at front
next[deleting] = free;
prev[deleting] = NULL_INDEX;
if (free != NULL_INDEX) { prev[free] = deleting; }
free = deleting;
// We're done
return contents[deleting];
} // deleteLast
```

## Representing Fundamental Types

• We normally speak of primitive types like integers, floats, and such.
• These types are built into the language.
• But they need to be implemented at some level (in compiler, virtual machine, or hardware).
• In most machines, values are stored as fixed-length sequences of bits (0/1 values).
• Bits are grouped into bytes and words.
• Typically, a byte is eight bits
• Typically, a word is some multiple of bytes, usually two or four bytes.
• The bits in a group are numbered from right to left, starting with 0.
• The rightmost bit in an eight-bit byte is the 0th bit.
• The leftmost bit in an eight-bit byte is the 7th bit.
• Observe that there are (at least) three versions of each number:
• The pure, mathematical entity.
• The representation in ASCII text (or which there may be many)
• The representation in binary (of which there is at most one)

Outlines: prev next

[News] [Basics] [Syllabus] [Outlines] [Assignments] [Examples] [Readings] [Bailey Docs]

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.