Untitled
unknown
plain_text
2 years ago
1.4 kB
4
Indexable
#include <stdio.h> #define MAX_NODES 100 struct Node { int data; int next; // Index of the next node in the array }; int main() { struct Node nodes[MAX_NODES]; int node_count = 0; int head = -1; // Insert nodes at the end of the linked list nodes[node_count].data = 1; nodes[node_count].next = -1; head = node_count; node_count++; nodes[node_count].data = 2; nodes[node_count].next = -1; nodes[head].next = node_count; node_count++; nodes[node_count].data = 3; nodes[node_count].next = -1; nodes[head].next = node_count; node_count++; // Print the linked list int current = head; printf("Linked list: "); while (current != -1) { printf("%d ", nodes[current].data); current = nodes[current].next; } printf("\n"); // Delete the last node current = head; int prev = -1; while (current != -1 && nodes[current].next != -1) { prev = current; current = nodes[current].next; } if (prev != -1) { nodes[prev].next = -1; } else { head = -1; } // Print the linked list after deletion current = head; printf("Linked list after deletion: "); while (current != -1) { printf("%d ", nodes[current].data); current = nodes[current].next; } printf("\n"); return 0; }
Editor is loading...