Professional Documents
Culture Documents
The #include statements at the beginning of the code are used to include various standard C libraries.
#include <stdio.h>:
● This library provides input and output functions. It is necessary for functions like printf() and
scanf().
#include <string.h>:
● This library provides functions for manipulating strings. It is commonly used for string-related
operations, such as strcpy(), strlen(), etc. Although the code you provided does not
currently use string functions, it's included in case future modifications require them.
#include <stdlib.h>:
● This library provides functions involving memory allocation, random number generation, and
other general-purpose functions. In the provided code, it is used for dynamic memory
allocation using malloc() to create new nodes for the linked list.
#include <stdbool.h>:
● This library introduces the bool type and the values true and false in C. It is used in the
code to define the boolean type for functions like isEmpty() that return true or false.
Including these libraries allows the program to use functions and types defined in these standard
libraries, making it easier to perform common tasks in C programming. The specific libraries included
depend on the functionalities required in the program. In this case, they are chosen to support
input/output operations, string manipulation, dynamic memory allocation, and boolean types.
You must remember if you are using above special data types ,include the libraries that i have mentioned
struct node {
int data;
int key;
struct node *next;
struct node *prev;
};
This structure defines a node for a doubly-linked list. Each node contains an integer data, an integer key, and two pointers:
next pointing to the next node in the list and prev pointing to the previous node.
Global Variables:
isEmpty Function:
void displayForward()
{
struct node *ptr = head;
● Declares a function displayForward that takes no parameters and returns void.
● Initializes a pointer ptr to the head of the list. This pointer will be used to traverse the list.
ptr = ptr->next;
Moves ptr to the next node in the list by updating it to point to the next pointer of the current node.
} printf(" ]");
● After the loop, prints a closing square bracket to mark the end of the display.
So, the displayForward function essentially traverses the linked list from the head to the end, printing the key and
data values of each node in the format "(key, data)" enclosed in square brackets. The loop ensures that the traversal
continues until the end of the list is reached.
Let's break down the insertFirst function line by line:
if (isEmpty())
{ //make it the last link
last = link;
}
else { //update first prev link
head->prev = link;
}
● Checks if the linked list is empty using the isEmpty() function. If it is empty, it means the new node will
be the only node in the list, so:
● Assigns last to point to the new node (link).
● If the list is not empty, it means there are already nodes in the list, so:
● Updates the prev pointer of the current head node (head->prev) to point to the new node (link).
This step ensures that the doubly-linked list consistency is maintained.
//point it to old first link
link->next = head; //point first to new first link head = link; }
● Sets the next pointer of the new node (link->next) to point to the current head node (head). This step links
the new node to the existing list.
● Updates the head pointer to point to the new node (link). This step effectively makes the new node the first
node in the list.
head = head->next;
● Moves the head pointer to the next node in the list, effectively removing the current head node.
return tempLink;
● Returns the pointer tempLink, which points to the node that was deleted. The calling function can use this
pointer for further processing or freeing the memory associated with the deleted node.
last = last->prev;
● Moves the last pointer to the previous node in the list, effectively removing the current last node.
#include <stdio.h>
#include <stdlib.h>
● These lines include standard input/output and standard library functions, necessary for input/output operations
and dynamic memory allocation.
●
struct node
{
int num;
struct node *nextptr;
} *stnode;
● This defines a structure node that represents a node in a linked list. It contains an integer (num) and a pointer to
the next node (nextptr).
● A global variable stnode is declared to represent the starting node of the circular linked list.
scanf("%d", &n);
● Print statements prompt the user for the number of nodes in the circular linked list and read the input.
ClListcreation(n); displayClList();
● Calls the function ClListcreation to create a circular linked list with n nodes and then displays the list using
displayClList.
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;
● The function ClListcreation begins. It takes an integer n as an argument.
● Checks if n is greater than or equal to 1.
● Allocates memory for the first node, takes input for its data, and initializes preptr to 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;
preptr->nextptr = newnode;
preptr = newnode;
}
preptr->nextptr = stnode;
}
}
● Uses a loop to create and link subsequent nodes to form a circular linked list.
● Takes input for each node's data, allocates memory, links nodes, and updates preptr.
● Links the last node to stnode to complete the circular structure.
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); }
}
● The displayClList function begins. It displays the circular linked list.
● Checks if the list is empty. If not, uses a loop to traverse and print each node's data until it reaches the starting
node again.
void insertEndCircular() {
int num;
struct node *newnode, *temp;
printf("Enter data for the new node at the end: ");
scanf("%d", &num);
newnode = (struct node *)malloc(sizeof(struct node));
newnode->num = num;
newnode->nextptr = stnode; temp = stnode;
while (temp->nextptr != stnode) {
temp = temp->nextptr;
}
temp->nextptr = newnode;
}
● insertEndCircular function begins. It inserts a new node at the end of the circular linked list.
● Takes input for the new node, allocates memory, and links it to the first node.
● Traverses the list to find the last node and links it to the new node.
void deleteFirstCircular() {
struct node *temp, *lastNode;
if (stnode == NULL) {
printf("Circular Linked List is empty, nothing to delete.\n");
return;
}
temp = stnode; lastNode = stnode;
while (lastNode->nextptr != stnode) {
lastNode = lastNode->nextptr;
}
stnode = temp->nextptr; lastNode->nextptr = stnode; free(temp); }
● deleteFirstCircular function begins. It deletes the first node in the circular linked list.
● Handles the case when the list is empty.
● Updates stnode to the next node and links the last node to the new starting node.
● Frees the memory of the deleted node.
void deleteLastCircular() {
struct node *temp, *lastNode;
if (stnode == NULL)
{
printf("Circular Linked List is empty, nothing to delete.\n");
return; } temp = stnode; lastNode = stnode;
while (lastNode->nextptr != stnode)
{ temp = lastNode; lastNode = lastNode->nextptr; } if (lastNode == stnode)
{ stnode = NULL;
}
else { temp->nextptr = stnode; } free(lastNode); }
● deleteLastCircular function begins. It deletes the last node in the circular linked list.
● Handles the case when the list is empty.
● Traverses to find the last and second last nodes.
● If only one node is present, sets stnode to NULL; otherwise,