Linked List Flashcards

1
Q

What is a linked list?

A

a Linked list is a linear collection of data elements, whose order is not given by their physical placement in memory.

In its most basic form, each node contains: data, and a reference (in other words, a link) to the next node in the sequence.

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

Why is it used?

A

This structure allows for efficient insertion or removal of elements from any position in the sequence during iteration.

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

What is a drawback of it?

A

A drawback of linked lists is that access time is linear (and difficult to pipeline).

Faster access, such as random access, is not feasible. Arrays have better cache locality compared to linked lists.

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

What can they be used to implement?

A

They can be used to implement several other common abstract data types, including lists, stacks, queues, associative arrays, and S-expressions, though it is not uncommon to implement those data structures directly without using a linked list as the basis.

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

What is a principle benefit?

A

The principal benefit of a linked list over a conventional array is that the list elements can be easily inserted or removed without reallocation or reorganization of the entire structure because the data items need not be stored contiguously in memory or on disk, while restructuring an array at run-time is a much more expensive operation.

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

When are they less optimal, O(n)?

A

since simple linked lists by themselves do not allow random access to the data or any form of efficient indexing, many basic operations—such as obtaining the last node of the list, finding a node that contains a given datum, or locating the place where a new node should be inserted—may require iterating through most or all of the list elements.

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

What are the disadvantages?

A

They use more memory than arrays because of the storage used by their pointers.

Nodes in a linked list must be read in order from the beginning as linked lists are inherently sequential access.

Nodes are stored incontiguously, greatly increasing the time periods required to access individual elements within the list, especially with a CPU cache.

Difficulties arise in linked lists when it comes to reverse traversing. For instance, singly linked lists are cumbersome to navigate backwards[1] and while doubly linked lists are somewhat easier to read, memory is consumed in allocating space for a back-pointer.

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

What is the code to add a new node?

A
node addNode(node head, int value){
   node temp,p;// declare two nodes temp and p
   temp = createNode();// assume createNode creates a new node with data = 0 and next pointing to NULL.
   temp->data = value; // add element's value to data part of node
   if(head == NULL){
       head = temp;     //when linked list is empty
   }
   else{
       p  = head;//assign head to p 
       while(p->next != NULL){
           p = p->next;//traverse the list until p is the last node.The last node always points to NULL.
       }
       p->next = temp;//Point the previous last node to the new node created.
   }
   return head;

}

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

What are the fields?

A

The field of each node that contains the address of the next node is usually called the ‘next link’ or ‘next pointer’. The remaining fields are known as the ‘data’, ‘information’, ‘value’, ‘cargo’, or ‘payload’ fields.

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

Doubly linked-lists?

A

In a ‘doubly linked list’, each node contains, besides the next-node link, a second link field pointing to the ‘previous’ node in the sequence. The two links may be called ‘forward(‘s’) and ‘backwards’, or ‘next’ and ‘prev’(‘previous’).

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

What is the code for a linked list to spell out FISH?

A
Node1.data = "F"
Node1.next = Node2
Node2.data = "I"
Node2.next = Node3
Node3.data = "S"
Node3.next = Node4
Node4.data = "H"
Node4.next = null
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to insert data in a double-linked list?

A
Node6.data = "6"
Node6.previous = Node2
Node6.next = Node3
Node3.previous = Node6
Node2.next = Node6
How well did you know this?
1
Not at all
2
3
4
5
Perfectly