You are on page 1of 10

ASSIGNMENT #2

…………………………………………..

Question 1:
#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;

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;

}
Question 2:
: Write a Program to remove duplicates
#include <bits/stdc++.h>
using namespace std;
 
class Node
{
    public:
    int data;
    Node* next;
};
 

void removeDuplicates(Node* head)


{
    Node* current = head;
 
        Node* next_next;
     
   
    if (current == NULL)
    return;
 
    
    while (current->next != NULL)
    {
    
    if (current->data == current->next->data)
    {
        
        next_next = current->next->next;
        free(current->next);
        current->next = next_next;
    }
    else
    {
        current = current->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 *node)
{
    while (node!=NULL)
    {
        cout<<" "<<node->data;
        node = node->next;
    }
}
 
int main()
{
        Node* head = NULL;
     
    /* Let us create a sorted linked list to test the functions
    Created linked list will be 11->11->11->13->13->20 */
    push(&head, 20);
    push(&head, 13);
    push(&head, 13);
    push(&head, 11);
    push(&head, 11);
    push(&head, 11);                                    
 
    cout<<"Linked list before duplicate removal ";
    printList(head);
 
    /* Remove duplicates from linked list */
    removeDuplicates(head);
 
    cout<<"\nLinked list after duplicate removal ";    
    printList(head);            
     
    return 0;
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>

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;
}
 

bool search(Node* head, int x)


{
    Node* current = head; // Initialize current
    while (current != NULL)
    {
        if (current->key == x)
            return true;
        current = current->next;
    }
    return false;
}
 
/* Driver program to test count function*/
int main()
{
    /* Start with the empty list */
    Node* head = NULL;
    int x = 21;
 
    /* Use push() to construct below list
    14->21->11->30->10 */
    push(&head, 10);
    push(&head, 30);
    push(&head, 11);
    push(&head, 21);
    push(&head, 14);
 
    search(head, 21)? cout<<"Yes" : cout<<"No";
    return 0;
}

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;
    }
}
 

void moveToFront(struct Node* head,


                struct Node* p, int m)
{

    if (head == NULL)


        return;
 
    p = head;
    head = head->next;
    m++;
 
    if (length == m)
    {
 
        p->next = NULL;
 
        
        last->next = first;
 
        first = head;
    }
    else
        moveToFront(head, p, m);
}
 
 
void push(struct Node** head_ref,
        int new_data)
{
    struct Node* new_node = (struct Node*)
        malloc(sizeof(struct Node));
 
    new_node->data = new_data;
 
    
    new_node->next = (*head_ref);
 
    (*head_ref) = new_node;
 
    if (length == 0)
        last = *head_ref;
    else
        first = *head_ref;
 
    length++;
}
 
int main()
{
    struct Node* start = NULL;
 
    push(&start, 5);
    push(&start, 4);
    push(&start, 3);
    push(&start, 2);
    push(&start, 1);
    push(&start, 0);
 
    cout << "Initial Linked list\n";
    printList(start);
    int m = 4; // no.of nodes to change
    struct Node* temp;
    moveToFront(start, temp, m);
 
    cout << "\n Final Linked list\n";
    start = first;
    printList(start);
 
    return 0;
}

Question 6:
Output:
Created Linked list is: 1 7 8 6 4

You might also like