0% found this document useful (0 votes)
36 views18 pages

Manas Dsa

Uploaded by

jappisardar1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views18 pages

Manas Dsa

Uploaded by

jappisardar1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

NAME: MANAS ANAND ENROL NO: 20102048 BATCH: A2

DSA LAB ASSIGNMENT 3


A1

CODE:
#include<iostream>
using namespace std;
class node
{
public:
int data;
node* next;
node(int val)
{
data=val;
next=NULL;
}
};
void insertAtHead(node* &head, int val)
{
node* n=new node(val);
if(head==NULL)
{
n->next=n;
head=n;
Charvi Gupta 20102031 1
}
node* temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=n;
n->next=head;
head=n;
}
void insertAtTail(node* &head, int val)
{
if(head==NULL)
{
insertAtHead(head,val);

}
node* n=new node(val);
node* temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=n;

Charvi Gupta 20102031 2


n->next=head;
}
void display(node* head)
{
node* temp=head;
do
{
cout<<temp->data<<" ";
temp=temp->next;
}
while(temp!=head);
cout<<endl;
}
bool search(node* head, int key)
{
node* temp=head;
while(temp->next!=head)
{
if(temp->data==key)
{
return true;
}
temp=temp->next;
}
return false;

Charvi Gupta 20102031 3


}
int main()
{
node* head=NULL;
insertAtTail(head,5);
insertAtTail(head,2);
insertAtTail(head,3);
insertAtTail(head,4);

display(head);
cout<<endl<<search(head,3);
return 0;
}

A2
CODE:
// C++ program for the above approach

#include <bits/stdc++.h>
using namespace std;

// Structure of a Node
struct Node {
int data;

Charvi Gupta 20102031 4


struct Node* next;
struct Node* prev;
};

// Function to insert a node at the


// end of the given Linked List
void insertEnd(struct Node** start,
int value)
{
// If the list is empty, then
// create a single node
// circular and doubly list
if (*start == NULL) {

// Create a new Node


struct Node* new_node = new Node();

// Add values and links to


// the next & the previous
new_node->data = value;
new_node->next = new_node;
new_node->prev = new_node;

// Update the start


*start = new_node;

Charvi Gupta 20102031 5


return;
}

// If list is not empty, then


// find last node
Node* last = (*start)->prev;

// Create Node dynamically


struct Node* new_node = new Node;
new_node->data = value;

// Start is the next of new_node


new_node->next = *start;

// Make new node previous of start


(*start)->prev = new_node;

// Make last previous of new node


new_node->prev = last;

// Make new node next of old last


last->next = new_node;
}

// Function to print the linked list

Charvi Gupta 20102031 6


void display(struct Node* start)
{
// Forward traversal
struct Node* temp = start;

cout << "Traversal in forward "


"direction \n";
while (temp->next != start) {
cout << temp->data << " ";
temp = temp->next;
}
cout << temp->data << " " ;

// Backward traversal
cout << "\nTraversal in reverse"
" direction \n";
Node* last = start->prev;
temp = last;

// Traverse the Linked List


while (temp->prev != last) {

// Print the data


cout << temp->data << " " ;
temp = temp->prev;

Charvi Gupta 20102031 7


}

// Print the data


cout << temp->data << " ";
}

// Function to find the sum of all


// nodes in the given Linked List
int findSum(Node*& start)
{
// Stores the sum of all the nodes
int sum = 0;

Node* temp = start;

// Traverse the linked list and


// update the sum
while (temp->next != start) {

// Update the sum


sum += temp->data;

// Update the temp


temp = temp->next;
}

Charvi Gupta 20102031 8


// Update the sum
sum += temp->data;

// Return the sum


return sum;
}

// Function to update the data of


// every node with the sum of data
// of all nodes except itself
void updateNodeValue(Node*& start)
{
// Stores the total sum
// of all the nodes
int sum = findSum(start);

Node* temp = start;

// Traverse the linked list


// and update each node's data
while (temp->next != start) {

// Update the temp->data


temp->data = sum - temp->data;

Charvi Gupta 20102031 9


// Update the temp
temp = temp->next;
}

// Update the temp->data


temp->data = sum - temp->data;
}

// Function to construct a
// circular doubly linked list
Node* formLinkedList(Node* start)
{
// Given linked list as:
// 4 <-> 5 <-> 6 <-> 7 <-> 8
insertEnd(&start, 4);
insertEnd(&start, 5);
insertEnd(&start, 6);
insertEnd(&start, 7);
insertEnd(&start, 8);

// Return the head of the


// constructed Linked List
return start;
}

Charvi Gupta 20102031 10


// Driver Code
int main()
{
// Linked list formation
struct Node* start = NULL;
start = formLinkedList(start);

// Display the linked list


display(start);

// Function Call
updateNodeValue(start);

// Display the linked list


// after updating nodes
cout << "\nAfter updating "
<< "the node values:\n";

// Print the Linked List


display(start);

return 0;
}
A3

Charvi Gupta 20102031 11


CODE:
// Charvi Gupta 20102031 Q3
//Input: 6 83 41 35 12 5
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};

void push(struct Node** head_ref, int data)


{

struct Node* ptr1


= (struct Node*)malloc(
sizeof(struct Node));

struct Node* temp = *head_ref;


ptr1->data = data;
ptr1->next = *head_ref;
if (*head_ref != NULL) {
while (temp->next != *head_ref)
temp = temp->next;
temp->next = ptr1;
}
else
ptr1->next = ptr1;
*head_ref = ptr1;
}
void deleteNode(Node* head_ref, Node* del)
{

Charvi Gupta 20102031 12


struct Node* temp = head_ref;
while (temp->next != del) {
temp = temp->next;
}
temp->next = del->next;
free(del);

return;
}
int digitSum(int num)
{
int sum = 0;
while (num) {
sum += (num % 10);
num /= 10;
}
return sum;
}
Node* deleteEvenDigitSumNodes(Node* head)
{
struct Node* ptr = head;

struct Node* temp;


while(digitSum(ptr->data)%2==0){
ptr=ptr->next;
if(ptr==head){
return NULL;
}
}
temp = ptr;
Node* temp2=ptr;
ptr=ptr->next;
Charvi Gupta 20102031 13
while(ptr!=head){
temp2=ptr;
ptr=ptr->next;
}
temp2->next=temp;
head=temp;
ptr=head;
do {
temp = ptr->next;
if (digitSum(ptr->data)%2==0)
deleteNode(head, ptr);
ptr = temp;
} while (ptr != head);
return head;
}
void printList(struct Node* head)
{
if(!head) printf("NULL");
struct Node* temp = head;
if (head != NULL) {
do {
printf("%d ", temp->data);
temp = temp->next;
} while (temp != head);
}
}
int main()
{
struct Node* head = NULL;
// Created linked list will be
// 6->83->41->35->12->5
push(&head, 6);
Charvi Gupta 20102031 14
push(&head, 83);
push(&head, 41);
push(&head, 35);
push(&head, 12);
push(&head, 5);
cout<<"\n The list after deleted elements is \n";
head = deleteEvenDigitSumNodes(head);
printList(head);
return 0;
}

A4
CODE:
// Charvi Gupta 20102031 Q4
#include <bits/stdc++.h>
using namespace std;
#define SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
class Node
{
public:
int data;
Node *next;
Node *child;
};
Node *createList(int *arr, int n)
{
Node *head = NULL;
Node *p;
int i;
for (i = 0; i < n; ++i)
{
if (head == NULL)

Charvi Gupta 20102031 15


head = p = new Node();
else
{
p->next = new Node();
p = p->next;
}
p->data = arr[i];
p->next = p->child = NULL;
}
return head;
}
void printList(Node *head)
{
while (head != NULL)
{
cout << head->data << " ";
head = head->next;
}
cout<<endl;
}
Node *createList(void)
{
int arr1[] = {10, 5, 12, 7, 11};
int arr2[] = {4, 20, 13};
int arr3[] = {17, 6};
int arr4[] = {9, 8};
int arr5[] = {19, 15};
int arr6[] = {2};
int arr7[] = {16};
int arr8[] = {3};
Node *head1 = createList(arr1, SIZE(arr1));
Node *head2 = createList(arr2, SIZE(arr2));
Charvi Gupta 20102031 16
Node *head3 = createList(arr3, SIZE(arr3));
Node *head4 = createList(arr4, SIZE(arr4));
Node *head5 = createList(arr5, SIZE(arr5));
Node *head6 = createList(arr6, SIZE(arr6));
Node *head7 = createList(arr7, SIZE(arr7));
Node *head8 = createList(arr8, SIZE(arr8));
head1->child = head2;
head1->next->next->next->child = head3;
head3->child = head4;
head4->child = head5;
head2->next->child = head6;
head2->next->next->child = head7;
head7->child = head8;
return head1;
}
void flattenList(Node *head)
{
if (head == NULL)
return;

Node *tmp;
Node *tail = head;
while (tail->next != NULL)
tail = tail->next;
Node *cur = head;
while (cur != tail)
{
if (cur->child)
{
tail->next = cur->child;
tmp = cur->child;
while (tmp->next)
Charvi Gupta 20102031 17
tmp = tmp->next;
tail = tmp;
}
cur = cur->next;
}
}
int main(void)
{
Node *head = NULL;
cout<<"\n The required output is : \n";
head = createList();
flattenList(head);
printList(head);
return 0;
}

Charvi Gupta 20102031 18

You might also like