0% found this document useful (0 votes)
20 views11 pages

DS - Unit - 1 - Circular LL Notes

Uploaded by

akshit23153075
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views11 pages

DS - Unit - 1 - Circular LL Notes

Uploaded by

akshit23153075
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Circular Linked List

In single linked list, every node points to its next node in the sequence and the last node points
NULL. But in circular linked list, every node points to its next node in the sequence but the last
node points to the first node in the list.

Circular linked list is a sequence of elements in which every element has link to its next element
in the sequence and the last element has a link to the first element in the sequence. That means
circular linked list is similar to the single linked list except that the last node points to the first
node in the list Example.

Note that there are no NULL values in the NEXT part of any of the nodes of list.

Program - Create and Display a Circular Linked List

#include <stdio.h>

#include <stdlib.h>

struct node {

int data;

struct node * next;

};

struct node *head;

void createList(int n);

void displayList();
int main()

int numnodes;

printf("enter the number of nodes");

scanf("%d", &numnodes);

createList(numnodes);

displayList();

return 0;

/**

* Creates a circular linked list of n nodes.

* @n Number of nodes to be created

*/

void createList(int n)

int i, data;

struct node *firstnode,*lastnode, *newNode;

firstnode = (struct node *)malloc(sizeof(struct node));

head=firstnode;

printf("Enter data of 1 node: ");

scanf("%d", &data);

firstnode->data = data;
firstnode->next = NULL;

lastnode = firstnode;

/ * Creates and links rest of the n-1 node */

for(i=2; i<=n; i++)

newNode = (struct node *)malloc(sizeof(struct node)) ;

printf("Enter data of %d node: ", i);

scanf("%d", &data);

newNode->data = data;

newNode->next = NULL;

// Link the previous node with newly created node

lastnode->next = newNode;

// Move the previous node ahead

lastnode = newNode;

// when Linked List is created with n nodes then to make it circular Link // the
last node with first node

lastnode->next=head;

printf("\nCIRCULAR LINKED LIST CREATED SUCCESSFULLY\n");

/**

* Display the content of the list

*/
void displayList()

struct node *temp;

if(head == NULL)

printf("List is empty.\n");

else

temp = head;

printf("Linked List is\n");

do {

printf("%d\t", temp ->data);

temp = temp->next;

}while(temp!= head);

OUTPUT

enter the number of nodes

Enter data of 1 node: 21

Enter data of 2 node: 22


Enter data of 3 node: 23

Enter data of 4 node: 24

CIRCULAR LINKED LIST CREATED SUCCESSFULLY

Linked List is

21 22 23 24

Insert Operation in Circular Linked List

Case 1 – Insert newnode at the beginning of circular Linked List

Step1 – Create a New Node.

Step 2 – Allocate memory to newly Created Node.

Step 3 – Assign data field value to newnode.

Step 4. To add new node in the beginning set the next pointer of newnode as

head--->next and next pointer of lastnode with new node.

Step 5 – Set head=newnode.

Pseudo code or function to insert newnode in the start of circular linked list is
given here
void insertAtBeginning(int value)

struct node * newNode ;

newNode = (struct node *)malloc(sizeof(struct node));

newNode->data = value;

newNode->next = head; // Point to next node which is currently head

lastnode->next =newNode; //Connect last node with newNode which is now Firstnode

head=newNode

}
Case 2- Insert newnode at the end of circular Linked List

Step1 – Create a New Node.

Step 2 – Allocate memory to newly Created Node.

Step 3 – Assign data field value to newnode.

Step 4. To add new node in the end of circular linked list set the next pointer of
lastnode with newnode and set next pointer of newnode with head to connect it
with first node of linked list and make the newnode as last node
// Pseudocode or Function to insert newnode at the End of Circular Linked List is given
//below

void insertAtEnd(int value)

struct node* newnode;

newnode = (struct node*)malloc(sizeof(struct node));

newnode->data = value;

newnode->next=firstnode;

lastnode->next = newnode;

lastnode = newnode;

Case 3- Insert newnode after a Given Node in Circular Linked List

Step1 – Create a New Node.

Step 2 – Allocate memory to newly Created Node.

Step 3 – Assign data field value to newnode.

Step 4 – Enter the data value of the node after which you want to insert the
newnode.
Step 5 - Define two node pointer temp1 and temp2 and initialize with them with
head and move temp1 and temp2 until the temp2 represents that node after which
you have to insert new node and temp1 refers to node next to temp2.

Step 6 – Set the next pointer of temp2 as newnode and next pointer of nenode with
temp1.
/* Pseudo Code or Function to insert new node after a given node in CLL is given below

void Insertafter(int num1, int num)

struct node *newnode, *temp1, * temp2;

newnode = (struct node*)malloc(sizeof(struct node));

newnode->data = num; //Links the data part

newnode->next = NULL;

temp1 = head;

temp2 = head;

/*traverse the linkedlist till the temp2 refer to the node after which you want to insert new //
node*/

do

temp2=temp1;

temp1 = temp1->next;

}while(temp2->data!=num1);

temp2->next = newnode;

newnode->next=temp1 ;

}
Deletion Operation in Circular Linked List

Case 1 – Deleting First node or from beginning of Circular Linked List

Step 1 – Check whether Linked List is empty or not.

Step 2 – If Linked List is empty the display the message linked list is empty
deletion is not possible

Step 3- If Linked List is not empty the to delete the first node of Circular Linked
list do the following

3.1 – Declare a node pointer temp and initialize it with head ( first node)

3.2 – Set the head pointer as head->next and set next field of lastnode as head.

3.3 – Delete temp it means free(temp)


// Pseudo Code or Function to delete first node in CLL is given here

void deletefrombegining()

if(head==NULL)

printf("linked list is empty deletion is not possible") ;

else

struct node*temp=head;

head=head->next;

lastnode->next=head;

free(temp);

}
Case 2 – Deleting Last node or from end of Circular Linked List

Step 1 – Check whether Linked List is empty or not.

Step 2 – If Linked List is empty the display the message linked list is empty
deletion is not possible

Step 3- If Linked List is not empty the to delete the last node of Circular Linked
list do the following

3.1 -- Define two node pointer temp1 and temp2 and initialize with them with
head and move temp1 and temp2 until the temp2 refers to the second last node and
temp1 refers to the last node of circular linked list.

3.2 – Set the next pointer of temp2 with head.

3.3 – Delete temp1 it means free(temp1)

// Pseudo Code or Function to delete last node is given here


void deletefromend()

if(head==NULL)

printf("linked list is empty deletion is not possible");

else

struct node*temp1,*temp2;

temp1=head;

temp2=head;

do
{

temp2=temp1;

temp1=temp1->next;

}while(temp1->next!=head);

temp2-> next= head;

free(temp1);

Case 3 – Deleting node after a given node of Circular Linked List

Step 1 – Check whether Linked List is empty or not.

Step 2 – If Linked List is empty the display the message linked list is empty
deletion is not possible

Step 3- If Linked List is not empty the to delete a node after a given node in
Circular Linked list do the following

3.1 – Enter the data filed value of that node after which you have to delete the node
in Circular Linked List

3.2-- Define two node pointer temp1 and temp2 and initialize with them with head
and move temp1 and temp2 until the temp1 refers to the node to be deleted and
temp2 refers to that node after which we have to delete the bode.

3.3 – Change the next filed pointer of temp 2.

3.4 – Delete temp1 it means free(temp1)


// Pseudo Code or Function to delete a node after a given node is given below
void Deleteafter(int num)

if(head==NULL)

printf("Linked List is empty deletion is not possible");

else

struct node *temp1, * temp2;

temp1 = head;

temp2 = head;

// traverse the linkedlist till the temp2 refer to the node after which you want to insert new
node

while (temp2->data!=num)

temp2=temp1;

temp1 = temp1->next;

temp2->next = temp1->next;

free(temp1);

You might also like