Professional Documents
Culture Documents
…………………………………………..
Question 1:
#include <iostream>
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;
}
Question 2:
: Write a Program to remove duplicates
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node* next;
};
>>>>>>>>>>>>>>>>>>>>>>>>>>>
Question 3:
include <iostream>
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;
}
int getCount(Node* head)
{
int count = 0; // Initialize count
Node* current = head; // Initialize current
while (current != NULL)
{
count++;
current = current->next;
}
return count;
}
int main()
{
Node* head = NULL;
/* Use push() to construct below list
1->2->1->3->1 */
push(&head, 1);
push(&head, 3);
push(&head, 1);
push(&head, 2);
push(&head, 1);
cout<<"count of nodes is "<< getCount(head);
return 0;
}
//////////////////////////////////////
Question 4:
#include <iostream>
using namespace std;
class Node
{
public:
int key;
Node* next;
};
void push(Node** head_ref, int new_key)
{
Node* new_node = new Node();
new_node->key = new_key;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
Question 5:
#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node* next;
} * first, *last;
int length = 0;
void printList(struct Node* node)
{
while (node != NULL)
{
cout << node->data <<" ";
node = node->next;
}
}
Question 6:
Output:
Created Linked list is: 1 7 8 6 4