0% found this document useful (0 votes)
86 views26 pages

Assignment: 1.wap Using C++ To Create A Linked List From A Given Array

The document contains C++ code to perform various operations on linked lists: 1. Create a linked list from an array 2. Create a new linked list from two given linked lists with the greater element at each node 3. Convert a singly linked list into a circular linked list 4. Merge a linked list into a circular another linked list at alternate positions 5. Convert a string to a singly linked list 6. Find the minimum and maximum prime numbers in a singly linked list 7. Add a new node to an ascending order linked list 8. Reverse a linked list

Uploaded by

Bhavey Malhotra
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)
86 views26 pages

Assignment: 1.wap Using C++ To Create A Linked List From A Given Array

The document contains C++ code to perform various operations on linked lists: 1. Create a linked list from an array 2. Create a new linked list from two given linked lists with the greater element at each node 3. Convert a singly linked list into a circular linked list 4. Merge a linked list into a circular another linked list at alternate positions 5. Convert a string to a singly linked list 6. Find the minimum and maximum prime numbers in a singly linked list 7. Add a new node to an ascending order linked list 8. Reverse a linked list

Uploaded by

Bhavey Malhotra
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/ 26

ASSIGNMENT

1.WAP USING C++ TO CREATE A LINKED LIST FROM A GIVEN ARRAY.

#include <iostream>

using namespace std;

struct Node {

int data;

Node* next;

};

void insert(Node** root, int item)

Node* temp = new Node;

Node* ptr;

temp->data = item;

temp->next = NULL;

if (*root == NULL)

*root = temp;

else {

ptr = *root;

while (ptr->next != NULL)

ptr = ptr->next;

ptr->next = temp;

void display(Node* root)

while (root != NULL) {

cout << root->data << " ";

root = root->next;

}
Node *arrayToList(int arr[], int n)

Node *root = NULL;

for (int i = 0; i < n; i++)

insert(&root, arr[i]);

return root;

int main()

int arr[] = { 1, 2, 3, 4, 5 };

int n = sizeof(arr) / sizeof(arr[0]);

Node* root = arrayToList(arr, n);

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>

using namespace std;

struct Node {

int data;

Node* next;

};

void insert(Node** root, int item)

Node *ptr, *temp;


temp = new Node;

temp->data = item;

temp->next = NULL;

if (*root == NULL)

*root = temp;

else {

ptr = *root;

while (ptr->next != NULL)

ptr = ptr->next;

ptr->next = temp;

Node* newList(Node* root1, Node* root2)

Node *ptr1 = root1, *ptr2 = root2, *ptr;

Node *root = NULL, *temp;

while (ptr1 != NULL) {

temp = new Node;

temp->next = NULL;

// Compare for greater node

if (ptr1->data < ptr2->data)

temp->data = ptr2->data;

else

temp->data = ptr1->data;

if (root == NULL)

root = temp;
else {

ptr = root;

while (ptr->next != NULL)

ptr = ptr->next;

ptr->next = temp;

ptr1 = ptr1->next;

ptr2 = ptr2->next;

return root;

void display(Node* root)

while (root != NULL) {

cout << root->data << "->";

root = root->next;

cout << endl;

int main()

Node *root1 = NULL, *root2 = NULL, *root = NULL;

// First linked list

insert(&root1, 5);

insert(&root1, 2);

insert(&root1, 3);

insert(&root1, 8);
cout << "First List: ";

display(root1);

// Second linked list

insert(&root2, 1);

insert(&root2, 7);

insert(&root2, 4);

insert(&root2, 5);

cout << "Second List: ";

display(root2);

root = newList(root1, root2);

cout << "New List: ";

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;

struct Node* next;

};

struct Node* circular(struct Node* head)

struct Node* start = head;

while (head->next != NULL)

head = head->next;
head->next = start;

return start;

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

struct Node* newNode = (struct Node*)malloc

(sizeof(struct Node));

newNode->data = data;

newNode->next = (*head);

// newNode become the headNode.

(*head) = newNode;

void displayList(struct Node* node)

struct Node* start = node;

while (node->next != start) {

printf("%d ", node->data);

node = node->next;

printf("%d ", node->data);

int main()

struct Node* head = NULL;

push(&head, 15);

push(&head, 14);
push(&head, 13);

push(&head, 22);

push(&head, 17);

circular(head);

printf("Display list: \n");

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>

using namespace std;

class Node

public:

int data;

Node *next;

};

void push(Node ** head_ref, int new_data)

Node* new_node = new Node();

new_node->data = new_data;

new_node->next = (*head_ref);

(*head_ref) = new_node;

void printList(Node *head)

Node *temp = head;


while (temp != NULL)

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

temp = temp->next;

cout<<endl;

void merge(Node *p, Node **q)

Node *p_curr = p, *q_curr = *q;

Node *p_next, *q_next;

while (p_curr != NULL && q_curr != NULL)

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()

Node *p = NULL, *q = NULL;

push(&p, 3);

push(&p, 2);

push(&p, 1);

cout<<"First Linked List:\n";

printList(p);
push(&q, 8);

push(&q, 7);

push(&q, 6);

push(&q, 5);

push(&q, 4);

cout<<"Second Linked List:\n";

printList(q);

merge(p, &q);

cout<<"Modified First Linked List:\n";

printList(p);

cout<<"Modified Second Linked List:\n";

printList(q);

return 0;

6.WAP USING C++ TO CONVERT A STRING TO A SINGLY LINKED LIST.

#include <iostream>

using namespace std;

struct node {

char data;

node* next;

};

node* add(char data)


{

node* newnode = new node;

newnode->data = data;

newnode->next = NULL;

return newnode;

node* string_to_SLL(string text, node* head)

head = add(text[0]);

node* curr = head;

for (int i = 1; i < text.size(); i++) {

curr->next = add(text[i]);

curr = curr->next;

return head;

void print(node* head)

node* curr = head;

while (curr != NULL) {

cout << curr->data << " -> ";

curr = curr->next;

int main()

string text = "GEEKS";

node* head = NULL;

head = string_to_SLL(text, head);


print(head);

return 0;

7. WAP USING C++ TO FIND MINIMUM AND MAXIMUM PRIME NUMBERS OF A SINGLY LINKED
LIST.

#include <bits/stdc++.h>

using namespace std;

struct Node {

int data;

Node* next;

};

void push(Node** head_ref, int new_data)

Node* new_node = new Node;

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;

void minmaxPrimeNodes(Node** head_ref)

int minimum = INT_MAX;

int maximum = INT_MIN;

Node* ptr = *head_ref;

while (ptr != NULL) {

if (isPrime(ptr->data)) {

minimum = min(minimum, ptr->data);

maximum = max(maximum, ptr->data);

ptr = ptr->next;

cout << "Minimum : " << minimum << endl;

cout << "Maximum : " << maximum << endl;

int main()

Node* head = NULL;

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.

10.WAP USING C++ TO REVERSE A LINKED LIST.

#include <iostream>

using namespace std;

struct Node {

int data;

struct Node* next;

Node(int data)

this->data = data;

next = NULL;

};

struct LinkedList {

Node* head;

LinkedList()

head = NULL;

}
Node* reverse(Node* head)

if (head == NULL || head->next == NULL)

return head;

Node* rest = reverse(head->next);

head->next->next = head;

head->next = NULL;

return rest;

void print()

struct Node* temp = head;

while (temp != NULL) {

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

temp = temp->next;

void push(int data)

Node* temp = new Node(data);

temp->next = head;

head = temp;

};

int main()

LinkedList ll;

ll.push(20);
ll.push(4);

ll.push(15);

ll.push(85);

cout << "Given linked list\n";

ll.print();

ll.head = ll.reverse(ll.head);

cout << "\nReversed Linked list \n";

ll.print();

return 0;

11.

12. WAP USING C++ TO SORT A LINKED LIST BY SWAPPING DATA.

#include <iostream>

using namespace std;

struct Node

int data;

struct Node* next;

} Node;

struct Node* swap(struct Node* ptr1, struct Node* ptr2)

struct Node* tmp = ptr2->next;

ptr2->next = ptr1;

ptr1->next = tmp;

return ptr2;
}

int bubbleSort(struct Node** head, int count)

struct Node** h;

int i, j, swapped;

for (i = 0; i <= count; i++)

h = head;

swapped = 0;

for (j = 0; j < count - i - 1; j++)

struct Node* p1 = *h;

struct Node* p2 = p1->next;

if (p1->data > p2->data)

*h = swap(p1, p2);

swapped = 1;

h = &(*h)->next;

if (swapped == 0)

break;

}
void printList(struct Node* n)

while (n != NULL)

cout << n->data << " -> ";

n = n->next;

cout << endl;

void insertAtTheBegin(struct Node** start_ref, int data)

struct Node* ptr1

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

ptr1->data = data;

ptr1->next = *start_ref;

*start_ref = ptr1;

int main()

int arr[] = { 78, 20, 10, 32, 1, 5 };

int list_size, i;

struct Node* start = NULL;

list_size = sizeof(arr) / sizeof(arr[0]);

for (i = 0; i < list_size; i++)

insertAtTheBegin(&start, arr[i]);

cout <<"Linked list before sorting\n";

printList(start);
bubbleSort(&start, list_size);

cout <<"Linked list after sorting\n";

printList(start);

return 0;

13. WAP USING C++ TO SORT A LINKED LIST BY READJUSTING THE LINKS.

14. WAP USING C++ TO IMPLEMENT A CIRCULAR QUEUE AS ALINKED LIST.

#include <bits/stdc++.h>

using namespace std;

struct Node

int data;

struct Node* link;

};

struct Queue

struct Node *front, *rear;

};

void enQueue(Queue *q, int value)

struct Node *temp = new Node;

temp->data = value;

if (q->front == NULL)

q->front = temp;
else

q->rear->link = temp;

q->rear = temp;

q->rear->link = q->front;

int deQueue(Queue *q)

if (q->front == NULL)

printf ("Queue is empty");

return INT_MIN;

int value;

if (q->front == q->rear)

value = q->front->data;

free(q->front);

q->front = NULL;

q->rear = NULL;

else

struct Node *temp = q->front;

value = temp->data;

q->front = q->front->link;

q->rear->link= q->front;

free(temp);

}
return value ;

void displayQueue(struct Queue *q)

struct Node *temp = q->front;

printf("\nElements in Circular Queue are: ");

while (temp->link != q->front)

printf("%d ", temp->data);

temp = temp->link;

printf("%d", temp->data);

int main()

Queue *q = new Queue;

q->front = q->rear = NULL;

enQueue(q, 14);

enQueue(q, 22);

enQueue(q, 6);

displayQueue(q);

printf("\nDeleted value = %d", deQueue(q));

printf("\nDeleted value = %d", deQueue(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>

using namespace std;

struct Node {

int data;

Node* next;

};

Node *newNode(int data)

Node *new_node = new Node;

new_node->data = data;

new_node->next = NULL;

return new_node;

Node* insertEnd(Node* head, int data)

if (head == NULL)

return newNode(data);

else

head->next = insertEnd(head->next, data);


return head;

void traverse(Node* head)

if (head == NULL)

return;

cout << head->data << " ";

traverse(head->next);

int main()

Node* head = NULL;

head = insertEnd(head, 6);

head = insertEnd(head, 8);

head = insertEnd(head, 10);

head = insertEnd(head, 12);

head = insertEnd(head, 14);

traverse(head);

19.WAP USING C++ TO MULTIPLY TWO POLYNOMIALS MAINTAINED AS LINKED LISTS.

#include <bits/stdc++.h>

using namespace std;

struct Node {

int coeff, power;

Node* next;

};
Node* addnode(Node* start, int coeff, int power)

Node* newnode = new Node;

newnode->coeff = coeff;

newnode->power = power;

newnode->next = NULL;

if (start == NULL)

return newnode;

Node* ptr = start;

while (ptr->next != NULL)

ptr = ptr->next;

ptr->next = newnode;

return start;

void printList(struct Node* ptr)

while (ptr->next != NULL) {

cout << ptr->coeff << "x^" << ptr->power << " + ";

ptr = ptr->next;

cout << ptr->coeff << "\n";

void removeDuplicates(Node* start)

Node *ptr1, *ptr2, *dup;

ptr1 = start;
while (ptr1 != NULL && ptr1->next != NULL) {

ptr2 = ptr1;

while (ptr2->next != NULL) {

if (ptr1->power == ptr2->next->power) {

ptr1->coeff = ptr1->coeff + ptr2->next->coeff;

dup = ptr2->next;

ptr2->next = ptr2->next->next;

delete (dup);

else

ptr2 = ptr2->next;

ptr1 = ptr1->next;

Node* multiply(Node* poly1, Node* poly2, Node* poly3)

Node *ptr1, *ptr2;

ptr1 = poly1;

ptr2 = poly2;

while (ptr1 != NULL) {

while (ptr2 != NULL) {

int coeff, power;

coeff = ptr1->coeff * ptr2->coeff;

power = ptr1->power + ptr2->power;


poly3 = addnode(poly3, coeff, power);

ptr2 = ptr2->next;

ptr2 = poly2;

ptr1 = ptr1->next;

removeDuplicates(poly3);

return poly3;

int main()

Node *poly1 = NULL, *poly2 = NULL, *poly3 = NULL;

poly1 = addnode(poly1, 3, 2);

poly1 = addnode(poly1, 5, 1);

poly1 = addnode(poly1, 6, 0);

poly2 = addnode(poly2, 6, 1);

poly2 = addnode(poly2, 8, 0);

cout << "1st Polynomial:- ";

printList(poly1);

cout << "2nd Polynomial:- ";

printList(poly2);

poly3 = multiply(poly1, poly2, poly3);


cout << "Resultant Polynomial:- ";

printList(poly3);

return 0;

You might also like