ITSC 2214 Week 5

Ace your homework & exams now with Quizwiz!

The important thing to understand about linked nodes are:

1. They are not necessarily stored contiguously in memory, the way that arrays are 2. They don't provide random access through indexing. You typically have to traverse a set of nodes to get to a specific node 3. If you need to insert a node in the middle of a linked node structure (such as a linked list), it is much easier to do than in an array, because you don't need to shift all the other elements over

Assume you have a linked list data structure with n nodes. It is a singly-linked list that supports generics, so it can hold any type of object. How many references are in this data structure, including references that are null?

2n + 1

1) Given numQueue: 5, 9, 1 (front is 5) What are the queue contents after the following push operation? Type the queue as: 1, 2, 3 Push(numQueue, 4)

5, 9, 1, 4

Given the following code using a queue X: X.enqueue(new Integer(4)); X.enqueue(new Integer(3)); Integer y = X.dequeue(); X.enqueue(new Integer(7)); X.enqueue(new Integer(2)); X.enqueue(y); X.enqueue(new Integer(9)); y = X.dequeue(); X.enqueue(new Integer(3)); X.enqueue(new Integer(8)); What is the value returned by X.first()?

7

If you have a queue with 700 items in it, how many linear nodes would be in use?

700

A special node named ___ is created to ___ after which regular items can be inserted

> head > represent the front of the list

Which of the following are not true when working with linked list Queues?

Can view any element if you have the index Can add at any index point

A queue processes elements in a _____ manner.

FIFO

The top variable keeps track of the number of elements in a LinkedStack implementation

False

Given that parkingQueue has no items (i.e., is empty), what does GetLength(parkingQueue) return?

If 0 items exist, length is 0

Push(queue, x)

Inserts x at end of the queue Ex. Push(queue, 56). Queue: 43, 12, 77, 56

For the following code, what is returned? public String testQueue() throws EmptyCollectionException { String s; Queue<String> queue = new ArrayQueue<Integer>(); queue.add("a"); queue.add("b"); s = queue.remove(); s = queue.remove(); queue.add("c"); queue.add("d"); s = queue.remove(); s = queue.remove(); return s; }

Nothing, an exception is thrown.

For the following code, what is returned? public String testQueue() throws EmptyCollectionException { String s; Queue<String> queue = new ArrayQueue<Integer>(); queue.add("a"); queue.add("b"); s = queue.remove(); s = queue.remove(); queue.add("c"); queue.add("d"); s = queue.remove(); s = queue.remove(); return s; }

Nothing, the code won't compile.

Will of the following lines of code wll compile (assume all needed files have been imported)?

Queue strings = new LinkedList(); Queue strings = new LinkedList< />();

LinkedList creation code:

Queue<T> queue = new LinkedList<T>();

Pop(queue)

Returns and removes item at front of queue Ex. Pop(queue) returns: 43. Queue: 12, 77

Peek(queue)

Returns but does not remove item at the front of the queue Ex. Peek(queue) return 43. Queue: 43, 12, 77

GetLength(queue)

Returns the number of items in the queue Ex. GetLength(queue) returns 3

IsEmpty(queue)

Returns true if queue has no items Ex. IsEmpty(queue) returns false

What is the purpose of a list's head node?

The head refers to the first item's node, or refers to nothing if the list is empty.

After the above list has items inserted as above, if a fourth item was inserted at the front of the list, what would happen to the location of node1?

The object does not have to be moved; only a few reference values change.

StackPop points a local variable to the list's head node. (T/F)

True > Removing the head node from the list would otherwise "lose" the head node. The local variable ensure the head node is remembered and can thus be returned at the end of StackPop.

list node

a class defined to represent each list item

queue add()

add(newElement) > Adds newElement element to the tail of the queue. The queue's size increases by one.

Queue's add() method

adds an element to the tail of the queue and increases the queue's size by one

queue

an ADT in which items are inserted at the end of the queue and removed from the front of the queue

A queue is appropriate for which of the following domains:

call center call processing

node

comprised of the data to be stored in each list item

Because a linear node holds a reference to a generic object, we call it a:

container

Assume you have a stack implemented with single-linked nodes, that looks like this: 8 -> 17 -> 23 -> 93 -> 66 'top' is a reference variable that points to the node containing 8. 'count' is an internal int that holds the count of nodes (currently 5) If you want to do push operation with the value 19, what is the pseudocode to achieve this?

create single linked node called temp set temp's element pointer to 19 set temp's next pointer to be the same as top set top to point at temp increment count

queue element()

element() > Returns, but does not remove, the element at the head of the queue. Throws an exception if the queue is empty.

Single-linked nodes

have a reference pointer to an object as well as a pointer to another node

Double-linked nodes

have a reference pointer to an object, a pointer to the previous node and a pointer to the next node

When using a Queue implemented with Linked Nodes how would you gain access to the element of the second node in the list?

head.getNext().getElement();

LinkedList and Queue import code:

import java.util.LinkedList; import java.util.Queue;

null

indicates that a reference variable does not refer to any object

queue push

inserts an item at the end of the queue

Queue

interface defined within the Java Collections Framework defines a Collection of ordered elements that supports element insertion at the tail and element retrieval from the head

If the head pointer is null, the queue ___

is empty > A value of null indicates that the pointer points to nothing, so the queue is empty.

first-in first-out ADT

items are inserted at the end of the queue and removed from the front

Linked nodes have this advantage over arrays for implementing collections:

no capacity issues

An empty stack is indicated by a list head pointer value of ___

null

queue peek()

peek() > Returns, but does not remove, the element at the head of the queue if the queue is not empty. Otherwise, returns null.

Which of the following methods allows you to return the head from a Queue, removing it and not throw an exception if the Queue is empty?

poll()

queue poll()

poll() > Removes and returns the element at the head of the queue if the queue is not empty. Otherwise, returns null.

queue remove()

remove() > Removes and returns the element at the head of the queue. Throws an exception if the queue is empty.

Which Java queue operations throw exceptions?

remove() element()

Queue's remove() method

removes AND RETURNS the element at the head of the queue

queue pop

removes and returns the item at the front of the queue

There are two main types of linear nodes:

singly-linked and doubly-linked

linked nodes (linear nodes)

structures that just allow you to link objects together

Java supports automatic conversion of an object, e.g., LinkedList, to a reference variable of an interface type, e.g., Queue, as long as ___

the object implements the interface


Related study sets

MDA- Ch. 11 Overview of Dentition Test Q&A

View Set

Patho Check Your Understanding 1,2,4,7

View Set

Stress and its Effects on Health

View Set

Priority Setting Framework Beginner

View Set