1.
Inserting a node into a sorted Doubly Linked List
DoublyLinkedListNode* sortedInsert(DoublyLinkedListNode* head, int data) {
DoublyLinkedListNode *New = create_doubly_linked_list_node(data);
if (!head)
head = New;
return head;
else if (data < (head->data))
New->next = head;
head->prev = New;
New->prev = NULL;
head = New;
return head;
else
DoublyLinkedListNode *temp = head;
while ( ((temp->next) != NULL) && ((temp->next->data) <= data))
temp = temp->next;
if (temp->next != NULL)
DoublyLinkedListNode *next = temp->next;
next->prev = New;
New->next = next;
else
New->next = NULL;
temp->next = New;
New->prev = temp;
return head;
2.Reverse a Doubly Linked List
DoublyLinkedListNode* reverse(DoublyLinkedListNode* llist) {
if(llist==NULL || llist->next==NULL){
return llist;
}
DoublyLinkedListNode* curr= llist;
while(curr!=NULL){
if (curr->next == NULL)
llist = curr;
DoublyLinkedListNode* temp=curr->next;
curr->next=curr->prev;
curr->prev=temp;
curr=curr->prev;
}
return llist;
}
1. Write a c program to find Sum of Elements in CLL
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
struct Node* insertEnd(struct Node* last, int data) {
if (last == NULL) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = newNode;
return newNode;
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = last->next;
last->next = newNode;
return newNode;
int sumOfElements(struct Node* last) {
if (last == NULL)
return 0;
struct Node* current = last->next;
int sum = 0;
do {
sum += current->data;
current = current->next;
} while (current != last->next);
return sum;
}
int main() {
struct Node* last = NULL;
int n, data;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &data);
last = insertEnd(last, data);
int sum = sumOfElements(last);
printf("%d\n", sum);
return 0;
Post lab
1. Node* removeDuplicates(Node* head) {
if (head == NULL || head->next == NULL) {
return head;
}
Node* current = head;
while (current->next != NULL) {
if (current->data == current->next->data) {
Node* temp = current->next;
current->next = current->next->next;
free(temp);
} else {
current = current->next;
}
}
return head;
}
};
2. CYCLE
bool has_cycle(SinglyLinkedListNode* head) {
SinglyLinkedListNode* fast=head;
SinglyLinkedListNode* slow=head;
while(fast->next!=NULL && fast->next->next!=NULL){
fast=fast->next->next;
slow=slow->next;
if(fast==slow)
return 1;
return 0;
SKILL
1. DESIGN LINKEDLIST
struct Node {
int val;
struct Node *next;
};
typedef struct MyLinkedList{
struct Node *head;
} MyLinkedList;
struct Node *createNode(int val)
struct Node *newNode = malloc(sizeof(struct Node));
newNode->next = NULL;
newNode->val = val;
return newNode;
MyLinkedList* myLinkedListCreate() {
MyLinkedList *obj = malloc(sizeof(MyLinkedList));
obj->head = NULL;
return obj;
int myLinkedListGet(MyLinkedList* obj, int index) {
struct Node *current = obj->head;
if(index == 0 && current)
return current->val;
else if(index == 0 && !current)
return -1;
else
for(int i = 0; i<index; i++)
if(current->next)
current = current->next;
else
return -1;
return current->val;
void myLinkedListAddAtHead(MyLinkedList* obj, int val) {
if(obj->head == NULL)
obj->head = createNode(val);
else
struct Node *temp = obj->head;
obj->head = createNode(val);
obj->head->next = temp;
void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
struct Node *current;
if(!obj->head)
myLinkedListAddAtHead(obj,val);
else
current = obj->head;
while(current->next != NULL)
current = current->next;
current->next = createNode(val);
void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) {
struct Node *current = obj->head;
struct Node *temp;
if(index == 0)
myLinkedListAddAtHead(obj,val);
else if(index > 0 && current)
for(int i = 1;i<index;i++)
{
if(current->next)
current = current->next;
else
return;
temp = current->next;
current->next = createNode(val);
current = current->next;
current->next = temp;
else
return;
void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
struct Node *current = obj->head;
struct Node *deleteNode;
if(index == 0 && current->next)
obj->head = current->next;
free(current);
else if(index == 0 && !current->next)
obj->head = NULL;
else if(current->next)
for(int i = 1;i < index; i++)
{
if(current->next->next)
current = current->next;
else
return;
deleteNode = current->next;
current->next = deleteNode->next;
//free the deleted node from the heap's memory
free(deleteNode);
void myLinkedListFree(MyLinkedList* obj) {
struct Node *temp = obj->head;
struct Node *next;
if(temp && temp->next)
next = temp->next;
free(temp);
while(next->next)
temp = next;
next = next->next;
free(temp);
free(next);
else if(temp)
{
free(temp);
else
return;
void display(MyLinkedList* obj)
struct Node * current = obj->head;
printf("Data in all linked list: ");
if(obj->head == NULL)
printf("NULL\n");
return;
while(current != NULL)
printf("%d ", current->val);
current = current->next;
printf("\n");
2. Flatten a Multilevel Doubly Linked List
Node* flatten(Node* head) {
for (Node* h = head; h; h = h->next){
if (h->child){
Node* next = h->next;
h->next = h->child;
h->next->prev = h;
h->child = NULL;
Node* temp = h->next;
while (temp->next) temp = temp->next;
temp->next = next;
if (next) next->prev = temp;
return head;
3.Find the Winner of the Circular Game
int findTheWinner(int n, int k) {
int last=1;
for(int i=2;i<=n;i++)
last=(last+k-1)%i+1;
return last;
3. Linked List Cycle II
struct ListNode *detectCycle(struct ListNode *head) {
struct ListNode* p1 = head, *p2 = head;
while (p2 != NULL && p2->next != NULL) {
p1 = p1->next;
p2 = p2->next->next;
if (p1 == p2) break;
if (p2 == NULL || p2->next == NULL) return NULL;
while (head != p1) {
head = head->next;
p1 = p1->next;
return head;
5.LARGEST ELEMENT IN A DOUBLY LINKED LIST
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct Node { int data; struct Node* next; struct Node* prev; };
void findMaxAndPredecessor(struct Node* head) { if (head == NULL) { printf("The list is empty.\n");
return; }
struct Node* maxNode = head;
struct Node* temp = head->next;
while (temp != NULL) {
if (temp->data > maxNode->data) {
maxNode = temp;
temp = temp->next;
if (maxNode->prev != NULL) {
printf("%d\n", maxNode->prev->data);
} else {
printf("%d", maxNode->data);
int main() { int n; scanf("%d",&n); struct Node* head = (struct Node*)malloc(sizeof(struct Node));
head->data = n; head->next = NULL; head->prev = NULL;
findMaxAndPredecessor(head);
return 0;
6. DELETE THE ELEMENT FROM A SPECIFIED POSITION IN A DOUBLY LINKED LIST
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct Node
int data;
struct Node *next;
struct Node *pre;
};
struct Node *head=NULL;
void create()
struct Node *New,*temp;
temp=head;
New=(struct Node *)malloc(sizeof(struct Node));
scanf("%d",&New->data);
New->next=NULL;
if(head==NULL){
head=New;
head->pre= NULL;
else
{
while(temp->next!=NULL)
temp=temp->next;
temp->next=New;
New-> pre=temp;
void display()
struct Node *temp;
temp=head;
while(temp!=NULL)
printf("%d deleted",temp->data);
temp=temp->next;
void del_pos()
struct Node *temp;
temp=head;
head=head->next;
head->pre=NULL;
free(temp);
int main() {
create();
//del_pos();
display();
return 0;
}