0% found this document useful (0 votes)
64 views13 pages

(STL) & (CLL) & (DLL) - 1 230225 220537

This document discusses circular linked lists and doubly linked lists. It defines circular linked lists as lists where the last node points back to the first node, forming a continuous loop. It provides code to implement a circular linked list in C with functions to print the list. Doubly linked lists are defined as lists where each node has pointers to both the next and previous nodes. Code is given to implement a doubly linked list in C with functions to print in the forward and backward directions. Advantages and disadvantages of each type of linked list are listed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views13 pages

(STL) & (CLL) & (DLL) - 1 230225 220537

This document discusses circular linked lists and doubly linked lists. It defines circular linked lists as lists where the last node points back to the first node, forming a continuous loop. It provides code to implement a circular linked list in C with functions to print the list. Doubly linked lists are defined as lists where each node has pointers to both the next and previous nodes. Code is given to implement a doubly linked list in C with functions to print in the forward and backward directions. Advantages and disadvantages of each type of linked list are listed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13


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

Advantages of Circular linked list over singly linked list:


Disadvantages
Structure and Declaration

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

You might also like