(STL) & (CLL) & (DLL) - 1 230225 220537
(STL) & (CLL) & (DLL) - 1 230225 220537
Functions
Container
Iterators
Implementation:
head = firstNode;
lastNode->next = firstNode;
Implementation in C
#include<stdio.h>
#include<stdlib.h>
//DECLARATION
struct node
{
int data;
struct node *next;
};
// This function prints contents of linked list from given node
void printLinkedList(struct node *list)
{
struct node *currentNode = list;
do
{
printf(" %d ", list->data);
list = list->next; // accessing and assigning the next element of list
}while(list != currentNode);
}
// Main method to initialize the program
int main()
{
//INITIALIZATION
//Declare and allocate 3 nodes in the heap
struct node* head;
struct node* first = (struct node*)malloc(sizeof(struct node));
struct node* second = (struct node*)malloc(sizeof(struct node));
struct node* third = (struct node*)malloc(sizeof(struct node));
struct node* fourth = (struct node*)malloc(sizeof(struct node));
//ASSIGNINMENT
first->data = 10; //assign data in first node
first->next = second; // Link first node with second
second->data = 20; //assign data to second node
second->next = third;
third->data = 30; //assign data to third node
third->next = fourth;
fourth->data = 50; //assign data to fourth node
fourth->next = first; // last node pointing to the first node
head = first; //head pointing to the linked list
printLinkedList(head);
return 0;
}
Output:-
10 20 30 50
struct node {
int data;
struct node *next;
struct node *prev;
}
Implementation in C language
#include<stdio.h>
#include<stdlib.h>
//DECLARATION
struct node
{
int data;
struct node *next;
struct node *prev;
};
//This function prints the content of linked list--forward direction
void printLLForward(struct node *list)
{
while (list)
{
printf(" %d ", list->data);
list = list->next; // accessing and assigning the next node of list
}
}
// This function prints the linked list--backward direction
void printLLBackward(struct node *list)
{
while (list->next != NULL)
{
list = list->next;
}
while (list)
{
printf(" %d ", list->data);
list = list->prev; // accessing and assigning the prev node of list
}
}
// Main method to initialize the program
int main()
{
//INITIALIZATION
//Declare and allocate 3 nodes in the heap
struct node* head;
struct node* first = (struct node*)malloc(sizeof(struct node));
struct node* second = (struct node*)malloc(sizeof(struct node));
struct node* third = (struct node*)malloc(sizeof(struct node));
//ASSIGNINMENT
first->data = 10; //assign data in first node
first->next = second; // Link first node with second
first->prev = NULL;
second->data = 20; //assign data to second node
second->next = third;
second->prev = first;
third->data = 30; //assign data to third node
third->next = NULL; // last node pointing to the first node
third->prev = second;
head = first; //head pointing to the linked list
printf("Printing the linked list in forward direction :\t");
printLLForward(head);
printf("\nPrinting the linked list in backward direction:\t");
printLLBackward(head);
return 0;
}
Output:-
Printing the linked list in forward direction : 10 20 30
Printing the linked list in backward direction: 30 20 10
Advantages:
Disadvantages:
Note:-
Prepared by