0
0
DSA Cprogramming~10 mins

Adjacency List Representation in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare the adjacency list array for a graph with MAX_VERTICES vertices.

DSA C
struct Node* adjacencyList[[1]];
Drag options to blanks, or click blank then click option'
AMAX_VERTICES
BMAX_EDGES
CMAX_NODES
DMAX_GRAPH
Attempts:
3 left
💡 Hint
Common Mistakes
Using the number of edges instead of vertices for the array size.
Using an undefined constant like MAX_NODES.
2fill in blank
medium

Complete the code to create a new node for the adjacency list with the given vertex value.

DSA C
struct Node* newNode = (struct Node*)malloc(sizeof(struct [1]));
newNode->vertex = vertex;
newNode->next = NULL;
Drag options to blanks, or click blank then click option'
AList
BNode
CGraph
DEdge
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong struct name like Graph or Edge.
Forgetting to cast the malloc result.
3fill in blank
hard

Fix the error in the code that adds an edge from source to destination in the adjacency list.

DSA C
newNode->next = adjacencyList[[1]];
adjacencyList[source] = newNode;
Drag options to blanks, or click blank then click option'
Asource
Bdestination
Cvertex
Dnode
Attempts:
3 left
💡 Hint
Common Mistakes
Using destination as the index instead of source.
Using an undefined variable like vertex or node.
4fill in blank
hard

Fill both blanks to complete the function that prints the adjacency list of the graph.

DSA C
for (int i = 0; i < [1]; i++) {
    struct Node* temp = adjacencyList[i];
    printf("Vertex %d:", i);
    while (temp != [2]) {
        printf(" -> %d", temp->vertex);
        temp = temp->next;
    }
    printf("\n");
}
Drag options to blanks, or click blank then click option'
AMAX_VERTICES
BNULL
C0
DNULLPTR
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of MAX_VERTICES for the loop limit.
Using NULLPTR which is not standard in C.
5fill in blank
hard

Fill all three blanks to complete the function that adds an undirected edge between two vertices.

DSA C
void addEdge(int src, int dest) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct [1]));
    newNode->vertex = dest;
    newNode->next = adjacencyList[src];
    adjacencyList[src] = newNode;

    newNode = (struct Node*)malloc(sizeof(struct [2]));
    newNode->vertex = src;
    newNode->next = adjacencyList[[3]];
    adjacencyList[dest] = newNode;
}
Drag options to blanks, or click blank then click option'
ANode
BGraph
Csrc
Ddest
Attempts:
3 left
💡 Hint
Common Mistakes
Using Graph instead of Node for malloc size.
Using src instead of dest for the second adjacency list index.