Assignment: 1.wap Using C++ To Create A Linked List From A Given Array
Assignment: 1.wap Using C++ To Create A Linked List From A Given Array
#include <iostream>
struct Node {
int data;
Node* next;
};
Node* ptr;
temp->data = item;
temp->next = NULL;
if (*root == NULL)
*root = temp;
else {
ptr = *root;
ptr = ptr->next;
ptr->next = temp;
root = root->next;
}
Node *arrayToList(int arr[], int n)
insert(&root, arr[i]);
return root;
int main()
int arr[] = { 1, 2, 3, 4, 5 };
display(root);
return 0;
2.
3.WAP USING C++ TO CREATE NEW LINKED LIST FROM TWO GIVEN LINKED LIST WITH GREATER
ELEMENT AT EACH NODE.
#include <iostream>
struct Node {
int data;
Node* next;
};
temp->data = item;
temp->next = NULL;
if (*root == NULL)
*root = temp;
else {
ptr = *root;
ptr = ptr->next;
ptr->next = temp;
temp->next = NULL;
temp->data = ptr2->data;
else
temp->data = ptr1->data;
if (root == NULL)
root = temp;
else {
ptr = root;
ptr = ptr->next;
ptr->next = temp;
ptr1 = ptr1->next;
ptr2 = ptr2->next;
return root;
root = root->next;
int main()
insert(&root1, 5);
insert(&root1, 2);
insert(&root1, 3);
insert(&root1, 8);
cout << "First List: ";
display(root1);
insert(&root2, 1);
insert(&root2, 7);
insert(&root2, 4);
insert(&root2, 5);
display(root2);
display(root);
return 0;
4.WAP USING C++ TO CONVERT SINGLY LINKED LIST INTO CIRCULAR LINKED LIST.
#include <bits/stdc++.h>
struct Node {
int data;
};
head = head->next;
head->next = start;
return start;
(sizeof(struct Node));
newNode->data = data;
newNode->next = (*head);
(*head) = newNode;
node = node->next;
int main()
push(&head, 15);
push(&head, 14);
push(&head, 13);
push(&head, 22);
push(&head, 17);
circular(head);
displayList(head);
return 0;
5.WAP USING C++ TO MERGE A LINKED LIST INTO CIRCULAR ANOTHER LINKED LIST AT ALTERNATE
POSITIONS.
#include <bits/stdc++.h>
class Node
public:
int data;
Node *next;
};
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
cout<<temp->data<<" ";
temp = temp->next;
cout<<endl;
p_next = p_curr->next;
q_next = q_curr->next;
q_curr->next = p_next;
p_curr->next = q_curr;
p_curr = p_next;
q_curr = q_next;
*q = q_curr;
int main()
push(&p, 3);
push(&p, 2);
push(&p, 1);
printList(p);
push(&q, 8);
push(&q, 7);
push(&q, 6);
push(&q, 5);
push(&q, 4);
printList(q);
merge(p, &q);
printList(p);
printList(q);
return 0;
#include <iostream>
struct node {
char data;
node* next;
};
newnode->data = data;
newnode->next = NULL;
return newnode;
head = add(text[0]);
curr->next = add(text[i]);
curr = curr->next;
return head;
curr = curr->next;
int main()
return 0;
7. WAP USING C++ TO FIND MINIMUM AND MAXIMUM PRIME NUMBERS OF A SINGLY LINKED
LIST.
#include <bits/stdc++.h>
struct Node {
int data;
Node* next;
};
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
bool isPrime(int n)
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
if (isPrime(ptr->data)) {
ptr = ptr->next;
int main()
push(&head, 17);
push(&head, 7);
push(&head, 6);
push(&head, 16);
push(&head, 15);
minmaxPrimeNodes(&head);
return 0;
8.
9.WAP USING C++ TO ADD A NEW NODE TO THE ASENDING ORDER LINKED LIST.
#include <iostream>
struct Node {
int data;
Node(int data)
this->data = data;
next = NULL;
};
struct LinkedList {
Node* head;
LinkedList()
head = NULL;
}
Node* reverse(Node* head)
return head;
head->next->next = head;
head->next = NULL;
return rest;
void print()
temp = temp->next;
temp->next = head;
head = temp;
};
int main()
LinkedList ll;
ll.push(20);
ll.push(4);
ll.push(15);
ll.push(85);
ll.print();
ll.head = ll.reverse(ll.head);
ll.print();
return 0;
11.
#include <iostream>
struct Node
int data;
} Node;
ptr2->next = ptr1;
ptr1->next = tmp;
return ptr2;
}
struct Node** h;
int i, j, swapped;
h = head;
swapped = 0;
*h = swap(p1, p2);
swapped = 1;
h = &(*h)->next;
if (swapped == 0)
break;
}
void printList(struct Node* n)
while (n != NULL)
n = n->next;
ptr1->data = data;
ptr1->next = *start_ref;
*start_ref = ptr1;
int main()
int list_size, i;
insertAtTheBegin(&start, arr[i]);
printList(start);
bubbleSort(&start, list_size);
printList(start);
return 0;
13. WAP USING C++ TO SORT A LINKED LIST BY READJUSTING THE LINKS.
#include <bits/stdc++.h>
struct Node
int data;
};
struct Queue
};
temp->data = value;
if (q->front == NULL)
q->front = temp;
else
q->rear->link = temp;
q->rear = temp;
q->rear->link = q->front;
if (q->front == NULL)
return INT_MIN;
int value;
if (q->front == q->rear)
value = q->front->data;
free(q->front);
q->front = NULL;
q->rear = NULL;
else
value = temp->data;
q->front = q->front->link;
q->rear->link= q->front;
free(temp);
}
return value ;
temp = temp->link;
printf("%d", temp->data);
int main()
enQueue(q, 14);
enQueue(q, 22);
enQueue(q, 6);
displayQueue(q);
displayQueue(q);
enQueue(q, 9);
enQueue(q, 20);
displayQueue(q);
return 0;
15. WAP USING C++TO CONCATENATE ONE LINKED LIST AT THE END OF ANOTHER AND THEN TO
ERACE ALL NODES PRESENT IN THE LINKED LIST.
18.WAP USING C++ TO ADD A NEW NODE AT THE END OF LINKED LIST USING RECURSION.
#include <bits/stdc++.h>
struct Node {
int data;
Node* next;
};
new_node->data = data;
new_node->next = NULL;
return new_node;
if (head == NULL)
return newNode(data);
else
if (head == NULL)
return;
traverse(head->next);
int main()
traverse(head);
#include <bits/stdc++.h>
struct Node {
Node* next;
};
Node* addnode(Node* start, int coeff, int power)
newnode->coeff = coeff;
newnode->power = power;
newnode->next = NULL;
if (start == NULL)
return newnode;
ptr = ptr->next;
ptr->next = newnode;
return start;
cout << ptr->coeff << "x^" << ptr->power << " + ";
ptr = ptr->next;
ptr1 = start;
while (ptr1 != NULL && ptr1->next != NULL) {
ptr2 = ptr1;
if (ptr1->power == ptr2->next->power) {
dup = ptr2->next;
ptr2->next = ptr2->next->next;
delete (dup);
else
ptr2 = ptr2->next;
ptr1 = ptr1->next;
ptr1 = poly1;
ptr2 = poly2;
ptr2 = ptr2->next;
ptr2 = poly2;
ptr1 = ptr1->next;
removeDuplicates(poly3);
return poly3;
int main()
printList(poly1);
printList(poly2);
printList(poly3);
return 0;