Linked Lists Flashcards

1
Q

What is a linked list?

A

A form of sequential collection and it dow not have to be in order. A linked list is made up of independent nodes that may contain any type of data and each node has a reference to the next node in the link.

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

What are the parts of the linked list

A

Head: this is the first node of the linked-list
nodes: they are the indepentent data store of the value
links: this connects the nodes together
tail: tail is the last node of the links. does not link to anything

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

What is the structure of the node

A

Data and Reference
The data stores the information and the reference stores the link to the next node.

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

Why use a linked list over an array

A
  1. each elements of a linked list is independent allowing it to delete the node without getting rid of the list
  2. We do not need to specify the size of the linked list like we do with an array.
  3. arrays cannot insert new arrays
    4 insertion and removals in linked list are very efficient
  4. arrays are very good at finding values since it is indexed. linked lists you need to iterate over to find the linked list.
  5. random access - accessing an element is very efficient in arrays
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What types of linked lists are there

A
  • Singly linked list
  • circular singly linked list
    -Doubly linked list
  • Circular Doubly Linked List
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Singly Linked list

A

The most basic of linked lists. The nodes have 2 parts. one for data and one for reference to the next node.

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

What is a Circular Singly/Double Linked List

A

It is the same thing as a single/double linked list but the last node references the first node creating a circle. This allows us to have the option to go back to the first node

When to use: Games are a good example where you need to move from one person to the next continuously

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

What is a Doubly Linked List

A

A doubly linked list has two references and data. One reference is to the previous node and the second reference is to the next node. This way you can move back and forth between each other.

ex: Say you are using spotify. You can move to the next song but also reference the previous song.

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

how linked list are stored in Memory

A

Linked list are stored randomly. When a reference is open it will then select the location.

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

Example of a linked list Node

A

class Node:
def __init__(self, value=None):
self.value = value
self.next = None

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

example of a simple linked list class

A

class SingleLinkedList:
def __init__(self):
self.head = None
self.tail = None

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

Method example to iterate over the nodes in the linked list

A

You can use the special variable __iter__ to define how to iterate.
def __iter__(self):
node = self.head
while node:
yield node
node = node.next

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

method example of inserting new node

A

def insert(self, value, location):
new_node = Node(value)
if self.head is None:
self.head = new_node
self.tail = new_node
else:
if location == 0:
new_node.next = self.head
self.head = new_node
elif location == -1:
self.tail.next = new_node
self.tail = new_node
else:
temp_node = self.head
index = 0
while index < location - 1 and temp_node.next:
temp_node = temp_node.next
index += 1
if temp_node.next == self.tail:
self.tail.next = new_node
self.tail = new_node
else:
next_node = temp_node.next
temp_node.next = new_node
new_node.next = next_node

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

deletion method example

A

def deletion(self, location):
if self.head is None:
raise Exception(“The list does not exist”)
if location < -1:
raise Exception(“The location must be greater than or equal to -1”)
if self.head == self.tail:
if location > 0:
raise Exception(“The location is out of bounds”)
else:
self.head = None
self.tail = None
elif location == 0:
self.head = self.head.next
elif location == -1:
if self.head == self.tail:
self.head = None
self.tail = None
else:
node = self.head
while node.next != self.tail:
node = node.next
node.next = None
self.tail = node
else:
index = 0
prev_node = None
node = self.head
while index < location - 1 and node.next != self.tail:
prev_node = node
node = node.next
index += 1
if node.next == self.tail:
if location > index + 2:
raise Exception(‘The location is out of bounds’)
else:
node.next = None
self.tail = node
else:
prev_node.next = node.next

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