Assignment DS PDF
Assignment DS PDF
Assignment DS PDF
#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;
insert(&root, arr[i]);
return root;
// Driver code
int main()
int arr[] = { 1, 2, 3, 4, 5 };
display(root);
return 0;
}
3)
list1 = 5->2->3->8
list2 = 1->7->4->5
#include <iostream>
// Representation of node
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;
// Driver code
int main()
insert(&root1, 5);
insert(&root1, 2);
insert(&root1, 3);
insert(&root1, 8);
display(root1);
// Second linked list
insert(&root2, 1);
insert(&root2, 7);
insert(&root2, 4);
insert(&root2, 5);
display(root2);
display(root);
return 0;
4)
#include <bits/stdc++.h>
struct Node {
int data;
};
head = head->next;
head->next = start;
return start;
(sizeof(struct Node));
newNode->data = data;
// head node.
newNode->next = (*head);
// newNode become the headNode.
(*head) = newNode;
node = node->next;
// linked list.
int main()
push(&head, 15);
push(&head, 14);
push(&head, 13);
push(&head, 22);
push(&head, 17);
// linked list.
circular(head);
displayList(head);
return 0;
5)
// 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;
p_curr = p_next;
q_curr = q_next;
// Driver code
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;
6)
#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;
// Driver code
int main()
print(head);
return 0;
#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;
// This is checked so that we can skip
if (n % 2 == 0 || n % 3 == 0)
return false;
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
if (isPrime(ptr->data)) {
// Update minimum
// Update maximum
ptr = ptr->next;
// Driver program
int main()
push(&head, 17);
push(&head, 7);
push(&head, 6);
push(&head, 16);
push(&head, 15);
minmaxPrimeNodes(&head);
return 0;
9)
class Node
public:
int data;
Node* next;
};
Node* current;
new_node->next = *head_ref;
*head_ref = new_node;
else
current = *head_ref;
current = current->next;
new_node->next = current->next;
current->next = new_node;
/* allocate node */
new_node->data = new_data;
new_node->next = NULL;
return new_node;
while(temp != NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
int main()
sortedInsert(&head, new_node);
new_node = newNode(10);
sortedInsert(&head, new_node);
new_node = newNode(7);
sortedInsert(&head, new_node);
new_node = newNode(3);
sortedInsert(&head, new_node);
new_node = newNode(1);
sortedInsert(&head, new_node);
new_node = newNode(9);
sortedInsert(&head, new_node);
printList(head);
return 0;
}
10)
// a linked list
#include <iostream>
struct Node {
int data;
Node(int data)
this->data = data;
next = NULL;
};
struct LinkedList {
Node* head;
LinkedList()
head = NULL;
void reverse()
// next pointers
Node* current = head;
// Store next
next = current->next;
current->next = prev;
prev = current;
current = next;
head = prev;
void print()
temp = temp->next;
{
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);
ll.print();
ll.reverse();
ll.print();
return 0;
11)
class Node
public:
int data;
Node* next;
};
is returned. */
Node dummy;
while (1)
if (a == NULL)
other list */
tail->next = b;
break;
else if (b == NULL)
tail->next = a;
break;
MoveNode(&(tail->next), &a);
else
MoveNode(&(tail->next), &b);
tail = tail->next;
return(dummy.next);
/* UTILITY FUNCTIONS */
source == {1, 2, 3}
dest == {1, 2, 3}
source == {2, 3}
dest == {1, 1, 2, 3} */
assert(newNode != NULL);
*sourceRef = newNode->next;
newNode->next = *destRef;
*destRef = newNode;
/* allocate node */
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
while (node!=NULL)
cout<<node->data<<" ";
node = node->next;
/* Driver code*/
int main()
Node* a = NULL;
Node* b = NULL;
push(&a, 15);
push(&a, 10);
push(&a, 5);
push(&b, 20);
push(&b, 3);
push(&b, 2);
printList(res);
return 0;
12)
// by swapping nodes
#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;
while (n != NULL)
{
n = n->next;
ptr1->data = data;
ptr1->next = *start_ref;
*start_ref = ptr1;
// Driver Code
int main()
int list_size, i;
insertAtTheBegin(&start, arr[i]);
printList(start);
bubbleSort(&start, list_size);
printList(start);
return 0;
14)
#include <bits/stdc++.h>
// Structure of a Node
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;
}
// If this is the last node to be deleted
if (q->front == q->rear)
value = q->front->data;
free(q->front);
q->front = NULL;
q->rear = NULL;
value = temp->data;
q->front = q->front->link;
q->rear->link= q->front;
free(temp);
return value ;
{
printf("%d ", temp->data);
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;
16)
#include <bits/stdc++.h>
class Node
public:
int data;
Node* next;
};
of the list. */
/* allocate node */
new_node->next = (*head_ref);
(*head_ref) = new_node;
count++;
current = current->next;
return count;
int main()
push(&head, 1);
push(&head, 3);
push(&head, 1);
push(&head, 2);
push(&head, 1);
return 0;
18)
#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);
// recursively.
else
return head;
if (head == NULL)
return;
traverse(head->next);
}
// Driver code
int main()
traverse(head);
19)
#include <bits/stdc++.h>
struct Node {
Node* next;
};
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;
}
// Function to add coefficients of
ptr1 = start;
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;
Node* poly3)
ptr1 = poly1;
ptr2 = poly2;
ptr2 = ptr2->next;
ptr2 = poly2;
ptr1 = ptr1->next;
removeDuplicates(poly3);
return poly3;
// Driver Code
int main()
printList(poly1);
printList(poly2);
printList(poly3);
return 0;