3.3 Elementary Data Structures: Linked Lists Flashcards

1
Q

What are Linked Lists?

A

A linked list is a set of items where each item is a part of a node that also contains a link to a node. In other words, each item contains the information we need to get to the next item.

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

What advantage do linked lists have over arrays?

A

Linked lists provide us with the ability to easily rearrange the items efficiently. Eg. to add an element in the front of an array: O(N), the same for a linked list: O(1).

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

What disadvantage do linked lists have over arrays?

A

The ease of modification/rearrangement that linked lists provide comes with a cost: quick access to any arbitrary item in the list, because the only way to get to an item is to follow links from the start. LINKED LISTS: SEQUENTIAL ACCESS. ARRAYS: RANDOM ACCESS.

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

Why are linked lists called SELF-REFERENT structures?

A

Because nodes in a linked lists are defined in terms of references to nodes.

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

Why are linked lists called CYCLIC STRUTURES?

A

Because although the nodes generally contain references to other nodes, they can also contain a reference to themselves.

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

What are the three conventions to represent the link for the last item in a linked list?

A
  1. It could be a NULL LINK that points to no node.
  2. It could be a DUMMY LINK that points to a dummy node that contains no item.
  3. It could be a refer back to the first node, making a list a CIRCULAR LIST.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are one-dimensional lists?

A

One-dimensional lists are lists in which all nodes, except perhaps the first and last one have only one link referring TO them.

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

Give a typedef declaration for a structure representing a linked list, and explain all its components.

A

typedef struct node *link;
struct node {Item item; link next; };

Links are references to nodes, and nodes consist of items and links. Items are of an unspecified datatype Item. This Item can be made to fit any datatype using a typedef statement.

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

How to allocate memory for a node/link? Give a C statement.

A

link x = malloc(sizeof(*x));

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

How to refer to item and link parts of a node x?

A

(x).item and (x).link
or
x->item and x->link

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

Write code to insert a node t after a node x.

A
t->next = x->next;
x->next = t;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Write code to delete a node which follows a node x.

A

x->next = x->next->next;

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

Why is insertion and deletion easier in linked lists than in arrays?

A

Insertion and deletion is difficult in arrays because we need to move all the elements following the affected element in an array. So, insertion and deletion is a O(N) time operation.
In linked lists, we simply need to rearrange the pointers, so it is a O(!) time operation.

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

Why is finding the kth (finding an element given its index) a difficult operation in linked lists, compared to arrays.

A

To reach an element with the index k in a linked list, due to sequential access, we need to follow k links. This makes accessing elements a O(N) time operation.

In arrays, we can randomly access an element with its index in O(1) time, like a[k].

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

Why is finding an element before a given item an unnatural operation on singly linked lists?

A

Because there is no way to access an element which comes before an element, ie, there is no pointer which points backward from a given element.

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

Why do sometimes not define functions for insertion, deletion etc. (manipulation) of linked lists?

A

Because these operations only take a few lines.

17
Q

Why is using a linked list not very efficient in the implementation of the Sieve of Erastosthenes program? And why is an array not an efficient data structure for the implementation of the Josephus Program?

A

In the sieve of erastosthenes program, lists are not useful because we need to access the elements via their indices quickly and efficiently.

In the Josephus problem, arrays are not efficient because we need to be able to delete elements quickly and efficiently.

18
Q

How to implement linked lists using a single array?

A

The indices could serve as info and the content could hold the index of the element to which this element points to.