Universidad Carlos III de Madrid

Ingeniería de Telecomunicación

Enero-Mayo 2010 / January-May 2010

Linked Lists, Stacks and Queues

Objectives Section Learning Objectives

Learning Objectives for Stacks and Queues

  • Define the concept of queue (Bloom 1).

  • Describe the public interface of a queue (Bloom 1).

  • Enumerate some applications of queues (Bloom 1).

  • Define the concept of stack (Bloom 1).

  • Describe the interface of a stack (Bloom 1).

  • Enumerate some applications of stacks (Bloom 1).

  • Define the concept of linked list (Bloom 1).

  • Define the concept of double-ended queue (deque) (Bloom 1).

  • Describe the interface of a double-ended queue (deque) (Bloom 1).

  • Define the concept of priority queue (Bloom 1).

  • Describe the interface of a priority queue (Bloom 1).

  • Explain how the state of a queue changes during several consecutive insertion and extraction operations (Bloom 2*).

  • Explain how the state of a stack changes during several consecutive insertion and extraction operations (Bloom 2*).

  • Explain how the state of a double-ended queue changes during several consecutive insertion and extraction operations in any of its ends (Bloom 2).

  • Explain why the implementation of a queue using a linked list requires insertion and extraction operations to be performed in different ends of the list (Bloom 2).

  • Explain why, to implement a queue using a linked list, it is recommended to keep a reference to the last node (tail) of the list (Bloom 2).

  • Explain why, to implement a queue using a linked list, it is recommended to extract nodes from the top of the list and insert nodes at the tail of the list (Bloom 2).

  • Explain why, to implement a queue using a linked list, push and pop operations have to be performed at the same end of the list. Explain why this end is the top of the list instead of its tail (Bloom 2).

  • Develop a queue using an array. (Bloom 3*)

  • Develop a stack using an array. (Bloom 3*)

  • Develop a queue using a linked list. (Bloom 3*)

  • Develop a stack using a linked list. (Bloom 3*)

  • Develop a queue using a double-ended queue (Bloom 3).

  • Develop a stack using a double-ended queue (Bloom 3).

  • Develop a double-ended queue using a doubly-linked list (Bloom 3).

  • Develop simple programs that use queues. (Bloom 3*)

  • Develop simple programs that use stacks. (Bloom 3*)

  • Solve basic theory questions about how a queue works. (Bloom 3*)

  • Solve basic theory questions about how a stack works. (Bloom 3*)

  • Solve basic theory questions about how a double-ended queue works (Bloom 3).

  • Develop a method to remove any node from a linked list (Bloom 3).

  • Develop a method to insert a new node in any position of a linked list (Bloom 3).

  • Compare pros and cons of arrays and linked lists to implement stacks and queues (Bloom 4*).

  • Given a simple programming problem, identify the adequacy of a queue or stack to solve it (Bloom 4).

  • Explain how a programming language can support internally the concept of subroutine (method) by means of a stack (Bloom 4).

  • Explain how a programming language can support the concept of recursion by means of a stack (Bloom 4).

  • Given a programming problem that proposes a design for its solution, including the data structures to be used (doubly-linked lists, circular linked lists and the like), develop the solution to the problem (Bloom 5).

Lecture Section Session 1 (lecture): Linked lists

Material

Lab Section Session 2 (lab): Linked lists

Material

Material (with solutions)

Lecture Section Session 3 (lecture): Stacks and Queues

Material

Lab Section Session 4 (lab): Stacks and Queues

Material

Material (with solutions)

Learning Pills Section Learning Pills

¿DID YOU KNOW THAT...?

Java uses a stack (the execution stack) to store the calls of a program in a certain moment.

¿DID YOU KNOW THAT...?

Java allows to control the execution stack size of a program.

¿DID YOU KNOW THAT...?

The Java API provides a Stack class which is not recommended to use.

¿DID YOU KNOW THAT...?

To implement a stack, we can use the basic Array class. The fundamental property of an array requires us to declare a size so that the compiler can allocate the correct amount of memory. If we have no idea how many items to expect, then it is difficult to make a reasonable choice for the array size. For that, and instead of using a Vector class, we can use the basic array with the dynamic array expansion technique. This way, if we have a 10-items array

int [] arr = new int[10];
					
and we want to make it larger as the program runs, we can use this technique.

¿DID YOU KNOW THAT...?

The Java library already contains a class with built-in functionality to mimic the dynamic-array-expansion technique.

¿DID YOU KNOW THAT...?

Among the two ways of implementing an stack, with arrays (Array or ArrayList) or with linked lists (LinkedList), the array versions of these data structures are likely to be faster than their linked list counterparts, especially if an accurate estimation of the capacity is available.

¿DID YOU KNOW THAT...?

Among the two ways of implementing a queue, with arrays (Array or ArrayList) or with linked lists (LinkedList), the list versions of these data structures are likely to be faster than their array counterparts.

¿DID YOU KNOW THAT...?

A queue does not allow random access to any of its elements.

¿DID YOU KNOW THAT...?

You can use an auxiliary stack to invert the order of the elements of a queue.

¿DID YOU KNOW THAT...?

Basic push and pop operations in the stack, and enqueue and dequeue in a queue have a complexity of O(1).

¿DID YOU KNOW THAT...?

For the Graphical User Interfaces in Java, a queue is used to store the events launched by the human-computer interactions.

¿DID YOU KNOW THAT...?

A double-ended queue (deque) can be used to implement an stack.

¿DID YOU KNOW THAT...?

A double-ended queue (deque) can be used to implement a queue.

¿DID YOU KNOW THAT...?

In a queue based in linked lists (LinkedQueue), the enqueue() method can be of high cost if the adequate measures are not taken.

¿DID YOU KNOW THAT...?

An array-based stack implementation (ArrayStack) is as efficient as an array-based queue implementation (ArrayQueue) if it uses a circular array.

¿DID YOU KNOW THAT...?

Java offers an object (Iterator) to iterate a list; however, this object does not exist either for a Stack or for the Queue.