0% found this document useful (0 votes)
14 views

Circular Linked List

Ds notes on circular linked list

Uploaded by

jiyadubla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Circular Linked List

Ds notes on circular linked list

Uploaded by

jiyadubla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Implement Circular Linked List ADT.

In a Circular linked list, every element has a link to its next element in the
sequence, and the last element has a link to the first element. A circular
linked list is similar to the singly linked list except that the last node points
to the first node. Below is the image to illustrate the same:

1. Insertion at the beginning:


Insert a new node as the first node. The next pointer of last will point to
this node and this new node will point to the previous first node.
Below is the implementation of the above operation:

// C program for the above operation

#include <stdio.h>

#include <stdlib.h>

// Structure of a linked list node

struct node {

int info;

struct node* next;


};

// Pointer to last node in the list

struct node* last = NULL;

// Function to insert a node in the

// starting of the list

void insertAtFront(int data)

// Initialize a new node

struct node* temp;

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

// If the new node is the only

// node in the list

if (last == NULL) {

temp->info = data;

temp->next = temp;

last = temp;

// Else last node contains the


// reference of the new node and

// new node contains the reference

// of the previous first node

else {

temp->info = data;

temp->next = last->next;

// last node now has reference

// of the new node temp

last->next = temp;

// Function to print the list

void viewList()

// If list is empty

if (last == NULL)

printf("\nList is empty\n");

// Else print the list

else {
struct node* temp;

temp = last->next;

// While first node is not

// reached again, print,

// since the list is circular

do {

printf("\nData = %d", temp->info);

temp = temp->next;

} while (temp != last->next);

// Driver Code

int main()

// Function Call

insertAtFront(10);

insertAtFront(20);

insertAtFront(30);

// Print list
viewList();

return 0;

Output
Data = 30
Data = 20
Data = 10
The time complexity of the insertAtFront function is O(1) as it performs a
constant amount of work to insert a node at the front of the linked list.
The time complexity of the viewList function is O(n) as it has to traverse
the entire linked list to print its elements.
2. Insertion at the end:
Inserting a new node as the last node. The next pointer of last will point to
this node and this new node will point to the first node.
Below is the implementation of the above operation:

// C program for the above operation

#include <stdio.h>

#include <stdlib.h>

// Structure of a linked list node

struct node {

int info;

struct node* next;


};

// Pointer to last node in the list

struct node* last = NULL;

// Function to add a new node at the

// end of the list

void addatlast(int data)

// Initialize a new node

struct node* temp;

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

// If the new node is the

// only node in the list

if (last == NULL) {

temp->info = data;

temp->next = temp;

last = temp;

// Else the new node will be the


// last node and will contain

// the reference of head node

else {

temp->info = data;

temp->next = last->next;

last->next = temp;

last = temp;

// Function to print the list

void viewList()

// If list is empty

if (last == NULL)

printf("\nList is empty\n");

// Else print the list

else {

struct node* temp;

temp = last->next;

do {
printf("\nData = %d", temp->info);

temp = temp->next;

} while (temp != last->next);

// Driver Code

int main()

// Function Call

addatlast(10);

addatlast(20);

addatlast(30);

// Print list

viewList();

return 0;

Output
Data = 10
Data = 20
Data = 30
This C program defines a circular linked list, where each node points to the next node in the
list, and the last node points back to the first node, creating a circular structure. Let’s break
down the key components of the code:

Structure Definition
struct node {
int info;
struct node* next;
};

 struct node: This structure defines a node in the linked list. It contains:
o info: An integer to store the data.
o next: A pointer to the next node in the list.

Pointer to Last Node


struct node* last = NULL;

 last: A pointer that keeps track of the last node in the list. It is initialized to NULL, indicating
that the list is initially empty.

Function to Add Node at the End


void addatlast(int data)

This function adds a new node containing data at the end of the circular linked list.

Steps Inside addatlast:

1. Memory Allocation:

struct node* temp;


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

o A new node (temp) is created using malloc, which allocates memory for a new
node.
2. Check if List is Empty:

if (last == NULL) {
temp->info = data;
temp->next = temp;
last = temp;
}

o If last is NULL, this indicates that the list is empty.


o The new node (temp) becomes the only node in the list:
 info is set to data.
 next points to itself (making it circular).
 last is updated to point to this new node.
3. Add to the End of the Existing List:

else {
temp->info = data;
temp->next = last->next;
last->next = temp;
last = temp;
}

o If the list is not empty, the new node needs to be added after the current last node.
o temp->info is set to data.
o temp->next is set to last->next, which is the first node in the list.
o The next pointer of the current last node is updated to point to the new node (temp).
o Finally, last is updated to point to temp, making it the new last node.

You might also like