answersLogoWhite

0

Can you Write algorithm for circularly linked list?

Updated: 8/16/2019
User Avatar

Wiki User

15y ago

Best Answer

C code. This has been tested. Example usage in main(). # include # include 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("]\n"); } 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\n", root->right->data); printf("%d\n", root->left->data); print_list(root); delete_list(root); return 0; }

User Avatar

Wiki User

15y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can you Write algorithm for circularly linked list?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a algorithm for doubly linked list in c?

sorry


What is pseudo code algorithm for create a linked list with a header and insert a four numbers to the list?

pseudo code algorithm to create a linked list


3 Write the algorithm of inserting an element at middle of a linked?

theory part to how u can insert element at middle of link list ?


How to write an actual algorithm for inserting an element in a linked list?

Insert newNode into a linked list after targetNode Node currentNode = root while currentNode != targetNode currentNode = currentNode.next newNode.next = currentNode.next currentNode.next = newNode


What are the differences between singly-linked doubly-linked and circularly-linked lists?

The difference is how many pointers each node has, and what they are pointing to. A linked list is comprised of "Nodes" each node contains data as well as 1 or more pointers. A singly linked list has one pointer per node, and a doubly linked list has 2 pointers per node. Some programs use several pointers per node. The purpose of these pointers is to hold the list together. In a singly linked list, you can view a node and can then move on to the next node that it is pointing to until you've passed through them all. A doubly-linked list would have a pointer to the next node as well as to the previous node. Thus you can move forward and backward through the list. A circularly-linked list doesn't necessarily have a set number of pointers because it simply means that the last node points to the first node creating a big circle. A non-circularly-linked list would not contain this last to first pointer and thus you would eventually reach the end of the list and stop.


Write an algorithm to search an element in linked list?

To search for an element in a linked list, you iterate the list, looking for the element, and either return the element or an indication that it was not found. for (ptr = first; ptr != null; ptr = ptr.next) if (ptr.value == searchvalue) break; This will either leave ptr with the address of the found element, or null, if not found.


How do you write a Java program to implement weighted queue using circular doubly linked list?

Add weights to the elements of the queue and use an algorithm to sort the queue every time an element is added.


Write an algorithm for the implementation of a circular doubly linked list?

Create a new node, making sure it is not allocated locally in the function and thus will not be destroyed when the function execution finishesFill in dataUse the "last node" pointer in the list and copy the "next" pointer location (pointing to the first node) into the new nodes "next" pointerSet the "last node" "next" pointer to point to the new nodeChange the list's "last node" pointer to point to the new nodeFor an example of implementation see: How_you_insert_a_newnode_in_singly_circular_link_list


What is the worst case running time of algorithm to delete each element from the linked list?

Linear time. O(n).


Explain Pseudo code for circular linked list?

write pseudocode for link list


An algorithm of deleting linked list?

To delete a linked list walk through the list and delete the memory allocated to each element, remembering the next element address, and then iterating or recursing the process using the next element address, until the next element address is null.


Write an algorithm and draw a corresponding flowchart to search a number in the given list of numbers and also display its position?

please give me an algorithm and a corresponding flow chart that displays list of numbers from 1 to 20.