Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an algorithm or code segment for searching a circular linked list for a gi

ID: 3644079 • Letter: W

Question

Write an algorithm or code segment for searching a circular linked list for a given item.

Explanation / Answer

typedef struct node { int data; struct node *left; struct node *right; } NODE; NODE *alloc_node() { return calloc(sizeof(NODE), 1); } NODE *makelist(int data) { NODE *root = alloc_node(); root->data = data; root->left = root; root->right = root; return root; } /* Insert data *AFTER* the positionth node. */ NODE *add(NODE *position, int data) { NODE *new = alloc_node(); new->data = data; new->left = position; new->right = position->right; (position->right)->left = new; position->right = new; return new; } void print_list(NODE *start) { NODE *this = start->right; printf("[%d, ", start->data); while ((this != NULL) && (this != start)) { printf("%d, ", this->data); this = this->right; } printf("] "); } void delete_list(NODE *start) { NODE *next = start; NODE *this; NODE *end = start->left->right = NULL; while (next != NULL) { this = next; if ((next = next->right) != NULL) { /* Sever links if they exist. */ next->left = NULL; } free(this); } } int main() { NODE *root = makelist(1); NODE *next = add(root, 2); next = add(next, 3); next = add(next, 4); /* 1 * 4 2 * 3 */ printf("%d ", root->right->data); printf("%d ", root->left->data); print_list(root); delete_list(root); return 0; }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote