You are on page 1of 7

============DOUBLE LINKED LIST==================================================

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:

struct node *head = NULL;


struct node *last = NULL;
struct node *current = NULL;
● head: Points to the first node in the list.
● last: Points to the last node in the list.
● current: Temporary pointer for navigation.

isEmpty Function:

bool isEmpty() { return head == NULL;


Returns true if the list is empty (head is NULL), otherwise returns false.

Let's break down the displayForward function line by line:

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.

printf("\n[ "); while(ptr != NULL) {

● Prints a newline and an opening square bracket to start the display.


● Enters a while loop that continues as long as ptr is not NULL (indicating there are nodes to traverse).

printf("(%d,%d) ", ptr->key, ptr->data);

● printf is a function in C used for formatted output to the console.


● ("(%d,%d) ", ptr->key, ptr->data) is the format string, which specifies how the data should be printed. Here:
○ (%d,%d) is a format specifier indicating that two integer values will be inserted into the string.
○ ptr->key is the key value of the current node, and ptr->data is the data value of the current node.
So, during each iteration of the loop, this line prints the key and data values of the current node pointed to by ptr in the format
"(key, data)". The %d is a placeholder for an integer value, and the actual values are obtained from ptr->key and
ptr->data.

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:

void insertFirst(int key, int data) { //create a link


struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key; link->data = data;
● Declares a function insertFirst that takes two parameters (key and data) and returns void.
● Allocates memory for a new node using malloc(sizeof(struct node)).
The link pointer is then assigned the address of the newly allocated memory.
● Sets the key and data values of the new node to the provided key and data parameters.

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.

Let's break down the insertLast function line by line:

void insertLast(int key, int data) {


struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
● Declares a function insertLast that takes two parameters (key and data) and returns void.
● Allocates memory for a new node using malloc(sizeof(struct node)). The link pointer is then assigned the
address of the newly allocated memory.
● Sets the key and data values of the new node to the provided key and data parameters.

if (isEmpty()) { //make it the last link


last = link;
}
else { //make link a new last link
last->next = link;
link->prev = last;
}
● 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). This establishes the new node as the only node in the
list.
● If the list is not empty, it means there are already nodes in the list, so:
● Sets the next pointer of the current last node (last->next) to point to the new node (link). This step
links the current last node to the new node.
● Sets the prev pointer of the new node (link->prev) to point to the current last node (last). This step
maintains the doubly-linked list by establishing the previous node of the new node.

//point last to new last node


last = link; }
● Updates the last pointer to point to the new node (link). This step effectively makes the new node the last
node in the list.

Let's break down the deleteFirst function line by line:

struct node* deleteFirst() {


//save reference to first link
struct node *tempLink = head;
● Declares a function deleteFirst that takes no parameters and returns a pointer to a node (struct node*).
● Declares a pointer tempLink and assigns it the value of the current head node (head). This pointer will
be used to store the reference to the node being deleted.
if only one link
if (head->next == NULL) {
last = NULL;
} else
{
head->next->prev = NULL;
}
● Checks if there is only one node in the list by examining the next pointer of the current head node
(head->next). If it is NULL, it means there's only one node in the list.
● If true, sets last to NULL since there is no longer any node in the list after the deletion.
● If false, update the prev pointer of the second node (the new head node after deletion) to
NULL. This step maintains the doubly-linked list structure.

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.

Let's break down the deleteLast function line by line:

struct node* deleteLast() { //save reference to last link


struct node *tempLink = last;
● Declares a function deleteLast that takes no parameters and returns a pointer to a node (struct node*).
● Declares a pointer tempLink and assigns it the value of the current last node (last). This pointer will be
used to store the reference to the node being deleted.

//if only one link


if (head->next == NULL)
{
head = NULL;
} else {
last->prev->next = NULL;
}
● Checks if there is only one node in the list by examining the next pointer of the current head node
(head->next). If it is NULL, it means there's only one node in the list.
● If true, sets head to NULL since there is no longer any node in the list after the deletion.
● If false, update the next pointer of the second-to-last node (the new last node after deletion) to
NULL. This step maintains the doubly-linked list structure.

last = last->prev;
● Moves the last pointer to the previous node in the list, effectively removing the current last node.

//return the deleted link


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.
====================CIRCULAR LINKED LIST ========================================
let's go through the code line by line with explanations:

#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.

int main() { int n;


stnode = NULL;
● The main function begins. It declares an integer variable n and initializes the global variable stnode to NULL.

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.

insertEndCircular(); printf("After inserting at the end: \n"); displayClList();


● Calls insertEndCircular to insert a node at the end of the circular linked list and displays the updated list.

deleteFirstCircular(); printf("After deleting the first node: \n"); displayClList();


● Calls deleteFirstCircular to delete the first node of the circular linked list and displays the updated list.

deleteLastCircular(); printf("After deleting the last node: \n"); displayClList();


● Calls deleteLastCircular to delete the last node of the circular linked list and displays the updated list.

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,

You might also like