Create a structure definition for a node with two members: an integer 'num' and a pointer to the next node 'nextptr'
Declare a global variable 'stnode' of type 'struct node*' and initialize it to NULL
Define a function ClListcreation that takes an integer parameter 'n':
Declare local variables: 'i', 'num'
If 'n' is greater than or equal to 1, then do the following:
Allocate memory for 'stnode' using 'malloc' and assign it to 'stnode'
Prompt the user to input data for the first node and store it in 'num'
Set the 'num' of 'stnode' to the input value
Set 'nextptr' of 'stnode' to NULL
Set 'preptr' to 'stnode'
Iterate from 'i' = 2 to 'n':
Allocate memory for a new node using 'malloc' and assign it to 'newnode'
Prompt the user to input data for the 'i'th node and store it in 'num'
Set the 'num' of 'newnode' to the input value
Set 'nextptr' of 'newnode' to NULL
Set 'nextptr' of 'preptr' to 'newnode'
Set 'preptr' to 'newnode'
Set 'nextptr' of 'preptr' to 'stnode'
Define a function displayClList:
Declare a local variable 'tmp' of type 'struct node*'
Declare a local variable 'n' and initialize it to 1
If 'stnode' is NULL, then do the following:
Print "No data found in the List yet."
Else, do the following:
Set 'tmp' to 'stnode'
Print "Data entered in the list are :"
Do the following:
Print "Data 'n' = 'tmp->num'"
Set 'tmp' to 'tmp->nextptr'
Increment 'n' by 1
While 'tmp' is not equal to 'stnode'
In the main function:
Declare a local variable 'n'
Set 'stnode' to NULL
Print "Circular Linked List : Create and display a circular linked list :"
Prompt the user to input the number of nodes and store it in 'n'
Call the function ClListcreation with 'n' as the argument
Call the function displayClList
Return 0
#include <stdio.h>
#include <stdlib.h>
struct node {
int num;
struct node * nextptr;
}*stnode;
void ClListcreation(int n);
void displayClList();
int main()
{
int n;
stnode = NULL;
printf("\n\n Circular Linked List : Create and display a circular linked list :\n");
printf("\n");
printf(" Input the number of nodes : ");
scanf("%d", &n);
ClListcreation(n);
displayClList();
return 0;
}
void ClListcreation(int n)
{
int i, num;
struct node *preptr, *newnode;
if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));
printf(" Input data for node 1 : ");
scanf("%d", &num);
stnode->num = num;
stnode->nextptr = NULL;
preptr = stnode;
for(i=2; i<=n; i++)
{
newnode = (struct node *)malloc(sizeof(struct node));
printf(" Input data for node %d : ", i);
scanf("%d", &num);
newnode->num = num;
newnode->nextptr = NULL; // next address of new node set as NULL
preptr->nextptr = newnode; // previous node is linking with new node
preptr = newnode; // previous node is advanced
}
preptr->nextptr = stnode; //last node is linking with first node
}
}
void displayClList()
{
struct node *tmp;
int n = 1;
if(stnode == NULL)
{
printf(" No data found in the List yet.");
}
else
{
tmp = stnode;
printf("\n\n Data entered in the list are :\n");
do {
printf(" Data %d = %d\n", n, tmp->num);
tmp = tmp->nextptr;
n++;
}while(tmp != stnode);
}
}
Circular Linked List : Create and display a circular linked list :
Input the number of nodes : 3
Input data for node 1 : 2
Input data for node 2 : 5
Input data for node 3 : 8
Data entered in the list are :
Data 1 = 2
Data 2 = 5
Data 3 = 8
//Create a new node
allocate memory for newNode
assign the data to newNode.
//Set prev and next pointers of new node
point next of newNode to the first node of the doubly linked list
point prev to null
//Make new node as head node
Point prev of the first node to newNode (now the previous head is the second node)
Point head to newNode
//Insertion in between two nodes
allocate memory for newNode
assign the data to newNode.
//Set the next pointer of new node and previous node
assign the value of next from previous node to the next of newNode
assign the address of newNode to the next of previous node
//Set the prev pointer of new node and the next node
assign the value of prev of next node to the prev of newNode
assign the address of newNode to the prev of next node
#include <stdio.h>
#include <stdlib.h>
// node creation
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
// insert node at the front
void insertFront(struct Node** head, int data) {
// allocate memory for newNode
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
// assign data to newNode
newNode->data = data;
// make newNode as a head
newNode->next = (*head);
// assign null to prev
newNode->prev = NULL;
// previous of head (now head is the second node) is newNode
if ((*head) != NULL)
(*head)->prev = newNode;
// head points to newNode
(*head) = newNode;
}
// insert a node after a specific node
void insertAfter(struct Node* prev_node, int data) {
// check if previous node is null
if (prev_node == NULL) {
printf("previous node cannot be null");
return;
}
// allocate memory for newNode
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
// assign data to newNode
newNode->data = data;
// set next of newNode to next of prev node
newNode->next = prev_node->next;
// set next of prev node to newNode
prev_node->next = newNode;
// set prev of newNode to the previous node
newNode->prev = prev_node;
// set prev of newNode's next to newNode
if (newNode->next != NULL)
newNode->next->prev = newNode;
}
// insert a newNode at the end of the list
void insertEnd(struct Node** head, int data) {
// allocate memory for node
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
// assign data to newNode
newNode->data = data;
// assign null to next of newNode
newNode->next = NULL;
// store the head node temporarily (for later use)
struct Node* temp = *head;
// if the linked list is empty, make the newNode as head node
if (*head == NULL) {
newNode->prev = NULL;
*head = newNode;
return;
}
// if the linked list is not empty, traverse to the end of the linked list
while (temp->next != NULL)
temp = temp->next;
// now, the last node of the linked list is temp
// assign next of the last node (temp) to newNode
temp->next = newNode;
// assign prev of newNode to temp
newNode->prev = temp;
}
// delete a node from the doubly linked list
void deleteNode(struct Node** head, struct Node* del_node) {
// if head or del is null, deletion is not possible
if (*head == NULL || del_node == NULL)
return;
// if del_node is the head node, point the head pointer to the next of del_node
if (*head == del_node)
*head = del_node->next;
// if del_node is not at the last node, point the prev of node next to del_node to the previous of del_node
if (del_node->next != NULL)
del_node->next->prev = del_node->prev;
// if del_node is not the first node, point the next of the previous node to the next node of del_node
if (del_node->prev != NULL)
del_node->prev->next = del_node->next;
// free the memory of del_node
free(del_node);
}
// print the doubly linked list
void displayList(struct Node* node) {
struct Node* last;
while (node != NULL) {
printf("%d->", node->data);
last = node;
node = node->next;
}
if (node == NULL)
printf("NULL\n");
}
int main() {
// initialize an empty node
struct Node* head = NULL;
insertEnd(&head, 5);
insertFront(&head, 1);
insertFront(&head, 6);
insertEnd(&head, 9);
// insert 11 after head
insertAfter(head, 11);
// insert 15 after the seond node
insertAfter(head->next, 15);
displayList(head);
// delete the last node
deleteNode(&head, head->next->next->next->next->next);
displayList(head);
}