ADTs Flashcards

1
Q

How do you implement the code for a function that counts the elements in a linkedlist?

A
  1. public driver - checks to make sure there are nodes
  2. private helper - if next isn’t null, add one and count the next
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. Why use dummy nodes?
  2. How is it implemented?
A
  1. The first node, last node, only node or empty list lead to special coding cases:
    1. lots of ifs
    2. lots of potential bugs
    3. less readable
    4. less easily and reliable to modify
  2. Add a “dummy” or “header” or “trailer” node to one end or the other end or both ends of a linked list, to ensure that the nodes containing real data are never the first, last or only nodes, and that top is neve null.
    1. Dummy nodes may contain undefined data, but in ordered lists should have appropriate extreme data values
    2. with back linking you can tell if a node is a dummy node if one of its links is null.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

For trailer and header Nodes, where do the variables exist?

How do you code the trailer and header?

A

Inside the constructor only

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the code to insert an item into a header/footer ordered linkedlist?

How does it differ from a list without headers?

A

without a header we have to check if curr and prev are null etc..

With headers, that additional code is no longer necessary

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you delete a value by key with a linkedlist with header files?

How does this differe for an ordered/unordered list?

A

For unordered-check if curr is not the dummy trailer as well

add to while loop: prev.next.next != null

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a circular linked list and why use it?

A
  • The last node points to the first node (not top).

How it could be useful:

  • This would allow you to search the list starting at any node
  • An easy way to find both ends, have top pointer point to the last node
    • Last node: last
    • First node: last.next
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Stack - where are new items added?

A

New elements are added at the top

LIFO - Last in first out

FILO - First in last out

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Queue - where are new items added

A

Added to the back

FIFO - first in, first out

LILO - last in, last out

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the stack main operations?

A
  • push - add something to the top
  • pop - remove something from the top
  • top - only look at the top
  • isEmpty
  • constructor
  • destructor - in languages without garbage collection
  • isFull - if space is limited
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the text representation of a stack?

A

[5 7 3 >,

[ represents the bottom

> represents the top

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

In general how do you implement a stack with an array?

A
  • The bottom of the stack is always index-0
  • The stack class needs an int variable called “top” that contains the index o the top of the stackin the array
  • limitation - fixed size
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the code for the class implementation of a Stack using an array?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Draw the ?variable record? diagram for a stack using an array

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

In general, how would you implement a stack using a linkedlist

A
  • each node contains an item in the stack
  • the front of the list (the top pointer) is the top of the stack
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the code for the class implementation of a stack using a linkedlist

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Draw the ?variable record? for stack using a linkedlist

A
17
Q

What is the pseudo code for implementing the bracket matching example

A
18
Q

What is the text representation of a queue

A

< 7 3 4 <

the 7 would be at the front, the 4 would be at the end

angle brackets point in the direction that items move through the queue

19
Q

What are queue basic operations?

A
  • Enter (or enqueue) - add item
  • Leave (or dequeue) - remove item
  • Front - loop only at the front of the queue(without removing it)
  • isEmpty
20
Q

ADT Queue - LinkedList

Draw a general linkedlist style 1 using both a front and end pointer

A
21
Q

What is the class code for a queue linked list style 1 (front and end pointer)

A
22
Q

Queue - Linkedlist style 1 - front and end pointer

Draw a ?varaible record? of what the implementation looks like

A
23
Q

Queue - LinkedList Style 2 (circular)

Draw the implementation of a queue using a circular linkedlist

A
24
Q

Queue - LinkedList Style 2 (circular)

Draw the general implementation of a queue using a circular linkedlist to Enqueue(enter) an item.

A
25
Q

Queue - LinkedList Style 2 (circular)

Draw the general implementation of a queue using a circular linkedlist to dequeue(leave) and element

A
26
Q

In general, what would a array queue implementation look like

A
27
Q

What are the bad implmentations of array queue?

A
  1. leave from index-0 and shift all elements over over (O(n)).
  2. Let the front move position (array moves right continually)
28
Q

How do you implement a circular array with Mod?

A

Implement a front and end for the array.

end = (end+1) % queueArray.length;

(The result is an integer in the range 0..queueArray.length-1)

29
Q

How does the mod operation table work/look?

A
30
Q

What are the solutions to using a circular array and distinguise between a full and empty array?

A
  1. Keep a third variable in addition to front and end.
    • positiblities
      • empty or full, a boolean flag
      • count(an int), the number of elements.
  2. Simply do not allow the nth element to be used.
    • Example: If the array has space for 8 elements, then the queue can only hold 7
    • Always leave at least one empty space
  3. Make end point to the first empty slot(the next ont to be filled by an entering element)
    • Use with solution 2
31
Q

How fast should all queue operations be and how do you ensure that is achieved?

A

O(1)

Before you program, think about which end you should do insertions and deletions at to make sure that NO loops are needed