Skip to main content

Posts

Showing posts with the label Circular Link List

Link List

In computer science, a linked list is a linear collection of elements, called nodes, each node have info field and next field pointing to the next node by means of a pointer. It is a data structure consisting of a group of nodes in sequence and connected with links. In simplest form of this data structure, each node is consist of data and a reference (in other words, a link) to the next node in the sequence. This structure allows for efficient insertion or removal of elements from any position in the sequence during iteration. Types of Link List Singly Link List: Insert node at first position in singly link list Insert node at last position in singly link list Insert node at specific position in singly link list Delete node at first position in singly link list Delete node at last position in singly link list Delete node at specific position in singly link list Find number of nodes in singly link list Dublication of singly link list Concatenation of two singly link list So...

Deletion of last node in circular link list

Now we will see how to delete node at last position in circular link list. Algorithm to delete node at last position in circular link list: Set TEMP1 = START If START != NULL If START->NEXT == NULL START = START->NEXT FREE (TEMP1) Else Traverse the list so that TEMP1 points to last node and TEMP2 points second last node TEMP2->NEXT = NULL FREE (TEMP1) End If; End If; Function to delete node at last position in circular link list:   int deleteLast(NODE **tail){ NODE *temp; int info = -1; if(*tail == NULL){ printf("\nError: List have no node"); } else{ if(*tail == (*tail)->next){ temp = *tail; *tail = NULL; info = temp->info; free(temp); } else{ temp = (*tail)->next; //temp points to first node while(temp->next != *tail){ temp = temp->next; } temp->next = (*tail)->next; //points to first node info = (*tail)->info; // store the info free(*tail); // free last node *tail = temp; //...

Deletion of node at specific position N in circular link list

Now we will see how to delete node at position 'N' in circular link list. Algorithm to delete node at position 'N' in circular link list: In this algorithm, TAIL points to last node of the link list and POS is position where the new node to be deleted. The steps are as follows: Initialize TEMP and TEMP2 If list have no node or position is less than one  (TAIL == NULL or POS < 1) Node cannot be deleted Else If (POS == 1) If list have one node   (TAIL->NEXT == TAIL) Delete TAIL   (TAIL = NULL) Else Delete the first node   (TAIL->NEXT = TAIL->NEXT->NEXT) End If; Else Set TEMP = TAIL->NEXT   (TEMP now points to first node) Traverse the list for (POS-1)th node using TEMP If node to be deleted is last node i.e. (TEMP->NEXT == TAIL) Set TAIL = TEMP End If; Set TEMP->NEXT = TEMP->NEXT->NEXT End If; End If;     Function to delete node at position 'N' in circular link list: int deleteAtPosition(NODE **tail, ...

Insert node at last position in circular link list

Insertion of new node at last position in circular link list requires creation of new node and setting it's info field. After creation of new node, we make last node points to newly created node and newly created node point to first node of the list. After that the pointer pointing to last node is updated to point to new created node. Algorithm for insertion at last position in circular link list: The algorithm uses TAIL as pointer to last node of list and PTR is the newly created node that is to be inserted at last position. The steps are as follows: Create new node PTR Set the INFO field of PTR If list have no node i.e. [TAIL == NULL] TAIL = PTR PTR->NEXT = PTR [PTR will point to itself] Else PTR->NEXT = TAIL->NEXT   [PTR will point to first node] TAIL->NEXT = PTR   [last node will point to PTR] TAIL = PTR   [TAIL will point to PTR which is now last node] End If; Function to insert node at last position in circular link list: void insertA...

Insert node at specific position N in circular link list

Now we will see how to insert a new node at position N in circular link list. Algorithm for insertion at position N in circular link list: In this algorithm, TAIL is pointer to the last node of the list and PTR is node to be inserted. The step are as below : Create new node PTR Set the info field of PTR If list have no nodes i.e. (TAIL == NULL) If (POS == 1) Set PTR->NEXT = PTR Set TAIL = PTR Else Invalid POS. Node can't be inserted End If; Else if (POS == 1) (Now list is not empty also) Set PTR->NEXT = TAIL->NEXT Set TAIL->NEXT = PTR Else Set TEMP = TAIL->NEXT Traverse the list for (POS-1)th node of list into TEMP If TEMP == TAIL Set PTR->NEXT = TAIL->NEXT Set TAIL->NEXT = PTR Set TAIL = PTR Else Set PTR->NEXT = TEMP->NEXT Set TEMP->NEXT = PTR End If; End If; Function to insert node at position N in circular link list: void insertAtPosition(NODE **tail, int info, int pos){ NODE *ptr = (NODE*) malloc(sizeof(NODE...

Deletion of first node in circular link list

Deletion of first node from the circular link list requires that the list has at least one node. It is error to delete a node doesn't exist. In this process, last node's next pointer is updated to point to second node of the list. Now we will see the algorithm to delete node at first position in circular link list. Algorithm to delete first node in circular link list: The algorithm uses TAIL as pointer to the last node of circular list and TEMP is temporary pointer that help in deletion process. The steps are as follows: Declare TEMP If list have only one node FREE (TAIL) TAIL = NULL Else TEMP = TAIL->NEXT  [TEMP will point to first node] TAIL->NEXT = TAIL->NEXT->NEXT [TAIL's next will now point to second node] Free (TEMP) [free the first node] End If; Function to delete node at first position in circular link list: The function takes pointer to last node as input and return info part of the list if no error occur, otherwise returns -1 after printi...

Insert node at first position in circular link list

Insertion of new node at first position in circular link list requires creation of new node and setting it's info field. After creation of new node, we make last node points to newly created node and newly created node to the first node of the list. Let us see the algorithm for insertion : Algorithm for insertion at first position in circular link list: In this algorithm, TAIL is pointer to last node and PTR is node to be inserted at first. The step are as follows: Create new node PTR Set the INFO field of PTR If list have no node i.e. [TAIL == NULL] TAIL = PTR PTR->NEXT = PTR [PTR will point to itself] Else PTR->NEXT = TAIL->NEXT [PTR's next will point to first node] TAIL->NEXT = PTR [TAIL's next pointer will point to PTR] End If Function to insert node at first position in circular link list: void insertAtFirst(NODE **tail, int info){ NODE *ptr = (NODE*) malloc(sizeof(NODE)); ptr->info = info; if(*tail == NULL){ //no nodes in list ptr-...

Circular Link List

Circular Link List is data structure in which all nodes are connected into the continuous circle. In this list, the last node points to the first node of the list. The last node reference is stored in variable and its next node is first node of the list. Before going to discuss the operation on circular link list we will first see the basic structure of the data structure and see how it can be represented in c programming. First we will see the how the node of circular link list is represented. See the image below: The component of singly list node: info : It contains the actual/satelite information next : This field points to the next node in the list Here is how the circular link list is represented: The components of circular link list are: TAIL  is pointer points to the last node of the list NODE : Each node have info field and next pointer to point next NODE in the list Last NODE of the list will point to first node of the list The structur...