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

Double Linked List Code

Uploaded by

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

Double Linked List Code

Uploaded by

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

DOUBLE LINKED LIST

#include <stdio.h>
#include <stdlib.h>

 This line tells the program to include two basic libraries:


o stdio.h: This allows us to use input/output functions like printf (to
print) and scanf (to read input).
o stdlib.h: This allows us to use functions like malloc, which helps us
allocate memory for our nodes (the boxes).
c

struct node {
int data;
struct node *prev;
struct node *next;
};

 This defines what a node (a box in the chain) looks like:


o int data: Each node stores a number (the data).
o struct node *prev: Each node points to the previous node (or box)
in the chain.
o struct node *next: Each node points to the next node in the chain.

Think of a node as a person holding hands with two other people: one
on the left (prev) and one on the right (next).
c

struct node* createNode(int data) {


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

 createNode is a function that creates a new node (a new box in the


chain).
o malloc(sizeof(struct node)): This allocates space for a new node
in memory. Imagine reserving an empty box to store our data.
c

if (!newNode) {
printf("Memory allocation failed\n");
exit(1);
}

 This checks if memory allocation was successful. If not, it prints an


error message and stops the program. It’s like trying to grab an empty
box, but if there aren’t any boxes left, we stop.
c

newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}

 We fill the new node (box) with the number (data) and set both its
left (prev) and right (next) neighbors to NULL, meaning the new box
isn’t connected to anything yet.
 Finally, it sends back (return) the newly created box (node).
c

void printList1(struct node *head) {


struct node *temp = head;

 This function is used to print the list in forward order, starting from
the first node (head).
o struct node *temp = head: We use a temporary pointer (temp) that
starts at the first node (head).
c

while (temp != NULL) {


printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

 While loop: This loop moves through the chain of nodes, printing the
number (data) in each node until it reaches the end.
o temp = temp->next: After printing the data of the current node, we
move to the next node in the chain.
o The loop stops when we reach the last node, where temp becomes
NULL (no more nodes to print).

void printList2(struct node *tail) {


struct node *temp = tail;

 This function prints the list in reverse order, starting from the last
node (tail).
c

while (temp != NULL) {


printf("%d ", temp->data);
temp = temp->prev;
}
printf("\n");
}

 Similar to the forward print function, this loop prints the data from
the last node to the first by following the prev pointers (going
backwards through the chain).
c

int main() {
int n, data;
struct node *head = NULL, *temp = NULL, *tail = NULL;

 In the main function, we start by declaring variables:


o n will store the number of nodes the user wants to create.
o data is used to store the number the user inputs for each node.
o head, temp, and tail are pointers used to keep track of the first node
(head), current node (temp), and last node (tail).
c

printf("Enter the number of nodes: ");


scanf("%d", &n);

 The program asks the user how many nodes they want to create
(scanf reads the number and stores it in n).
c

for (int i = 0; i < n; i++) {


printf("Enter data for node %d: ", i + 1);
scanf("%d", &data);
struct node *newNode = createNode(data);

 For loop: This loop runs n times (once for each node the user wants
to create).
o For each node, it asks the user to enter a number (data).
o It calls the createNode function to make a new node (box) with that
number.
c

if (head == NULL) {
head = newNode;
} else {
temp->next = newNode;
newNode->prev = temp;
}
temp = newNode;

 If block:
o If the list is empty (i.e., head is NULL), the first node we create
becomes the head (the start of the list).
o Otherwise, we connect the new node to the current node (temp) by
setting:
 temp->next = newNode: The current node now points to the new
node.
 newNode->prev = temp: The new node points back to the current
node.
o Then, we move temp to the new node, so the next iteration can add
the next node after it.
c

tail = temp;

 After the loop ends, temp points to the last node. We assign this to
tail so we can print the list in reverse later.

printf("Doubly linked list in forward: ");


printList1(head);
printf("Doubly linked list in reverse: ");
printList2(tail);

 The program prints the list in forward order (starting from head) and
then in reverse order (starting from tail).
c

return 0;
}

 This signals the end of the program. The 0 indicates the program ran
successfully.
Summary:
 You create a chain of connected boxes (nodes).
 Each box holds a number and knows about its neighbors (the
previous and next boxes).
 You can print the numbers from the start to the end of the chain
(forward) or from the end to the start (reverse).

You might also like