L.L. Queues & L.L. Traversal

Basic L.L. Queue

Value Next
a pointer
b pointer
g pointer
h nullptr

as a stack, this L.L. would be:

abha \rightarrow b \rightarrow \dots \rightarrow h

Pseudo-code:

class Node{
  Type data
  Node * next
}

Enqueue + Dequeue – First Versions

Variables:

Dequeue:

Pseudo-code:

dequeue(){
  temp = front
  val = front->data
  front = front->next
  delete temp
  size = size-1
  return val
}

Diagram:

Value Next
a pointer
b pointer
g pointer
h nullptr

Unqueue

Pseudo-code:

enqueue(x){
  n = new node containing x
  back->next = n
  back = back->next
  size = size+1
}

Diagram:

Value Next
a pointer
b pointer
g pointer
h nullptr pointer
x nullptr

Empty Queue

A stack with a

frontaback\text{front} \rightarrow a \leftarrow \text{back}

After dequeue

front,back\text{front}, \text{back}

After enqueue(b)

frontbback\text{front} \rightarrow b \leftarrow \text{back}

Enqueue & Dequeue are different from empty/non-empty queues.

Enqueue

Example 1: Empty queue

front,back\text{front}, \text{back}

Enqueue b

frontbback\text{front} \rightarrow b \leftarrow \text{back}

Example 2: Starter queue

frontxback\text{front} \rightarrow \dots \rightarrow x \leftarrow \text{back}

enqueue b

frontxbback\text{front} \rightarrow \dots \rightarrow x \rightarrow b \leftarrow \text{back}

Pseudo code:

enqueue(x){
  if size > 0{
    back->next = new Node(x)
    back = back->next
  } else {
    back = new Node(x)
    front = back
  }
  size = size+1
}

Dequeue

Pseudo code:

front = front->next
return value

That code has the following effect upon a queue.

frontabcdback\text{front} \rightarrow a \rightarrow b \rightarrow c \rightarrow d \leftarrow \text{back}

becomes

frontbcdback\text{front} \rightarrow b \rightarrow c \rightarrow d \leftarrow \text{back}

while returning a.

Example 1:

frontbback\text{front} \rightarrow b \leftarrow \text{back}

dequeue (b)

front,back\text{front}, \text{back}

Example 2:

frontbcdback\text{front} \rightarrow b \rightarrow c \rightarrow d \leftarrow \text{back}

dequeue

frontcdback\text{front} \rightarrow c \rightarrow d \leftarrow \text{back}

Return b

Pseudo code

dequeue(){
  temp = front
  val = front->data
  front = front->next
  delete temp
  size = size-1
  return val
}

Traversing the List

Pseudo code:

displayList(){
  Node * cur = front;
  while(cur!=nullptr){
    output cur->data
    cur = cur->next
  }
}

A diagram which shows the cursor going from c, to d to be (in order from front to back).

Linked List Ends

Transcriber’s note: this is the same as in 06. The left/right arrows denote a double linked list, even though the current set of slides is talking about only singly linked lists.

For [4, 5, 6]:

headbeginning node456ending nodetail \text{head} \leftrightarrow \text{beginning node} \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6 \leftrightarrow \text{ending node} \leftrightarrow \text{tail}

versus

head456tail \text{head} \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6 \leftrightarrow \text{tail}

For [5]:

headbeginning node5ending nodetail \text{head} \leftrightarrow \text{beginning node} \leftrightarrow 5 \leftrightarrow \text{ending node} \leftrightarrow \text{tail}

versus

head5tail\text{head} \leftrightarrow 5 \leftrightarrow \text{tail}

For []:

headbeginning nodeending nodetail\text{head} \leftrightarrow \text{beginning node} \leftrightarrow \text{ending node} \leftrightarrow \text{tail}

versus

head,tail\text{head} , \text{tail}

List Class: Traversing the List

Pseudo code:

displayList(){
  Node * cur = head->next
  while(cur!=tail){
    output cur->data
    cur = cur->next
  }
}

Graphic of cur moving through each (empty) element of the queue.

End