Merging two Singly linked lists in alternate order.

#include<bits/stdc++.h>
using namespace std;
struct node
{
    int data;
    struct node * next;
};
struct node * newnode, *temp, *temp1, *temp2;
struct node * create()
{
    struct node *head=0;
    int choice=1;
    while(choice==1)
    {
        newnode=(struct node*)malloc(sizeof(struct node));
        cout<<"Enter data ";
        cin>>newnode->data;
        newnode->next=0;
        if(head==0)
        head=temp=newnode;
        else
        {
            temp->next=newnode;
            temp=newnode;
        }
        cout<<"Want another node to be inserted : ";
        cin>>choice;
    }
    return head;
}

void link(struct node*head, struct node*temp, struct node*newnode)
{

}

struct node* alter_merge(struct node*head1, struct node* head2)
{
    struct node*head=0;
    temp1=head1,temp2=head2;
    while(temp1!=0 && temp2!=0)
    {
        newnode=(struct node*)malloc(sizeof(struct node));
        newnode->data=temp1->data;
        newnode->next=0;
        if(head==0)
            temp=head=newnode;
        else
            {temp->next=newnode;temp=newnode;}
        newnode=(struct node*)malloc(sizeof(struct node));
        newnode->data=temp2->data;
        newnode->next=0;
        temp->next=newnode;
        temp=newnode;
        temp1=temp1->next;
        temp2=temp2->next;
    }
    while(temp1!=0)
    {
        newnode=(struct node*)malloc(sizeof(struct node));
        newnode->data=temp1->data;
        newnode->next=0;
        if(head==0)
            temp=head=newnode;
        else
            {temp->next=newnode;temp=newnode;}
        temp1=temp1->next;
    }
    while(temp2!=0)
    {
        newnode=(struct node*)malloc(sizeof(struct node));
        newnode->data=temp2->data;
        newnode->next=0;
        if(head==0)
            temp=head=newnode;
        else
            {temp->next=newnode;temp=newnode;}
        temp2=temp2->next;
    }
    return head;
}

void listprint(struct node *head)
{
    if(head==0)
        cout<<"\nList Empty!\n";
    else
    {
        temp=head;
        while(temp!=0)
        {
            cout<<temp->data<<" ";
            temp=temp->next;
        }
    }
}
int main()
{
    int choice=1;
    struct node*head1=0, *head2=0, *head3=0;
    cout<<"Enter List 1st : \n";
    head1=create();
    cout<<"The list is : ";
    listprint(head1);
    cout<<"\nEnter List 2nd : \n";
    head2=create();
    cout<<"The list is : ";
    listprint(head2);
    head3=alter_merge(head1,head2);
    cout<<"\nTHE NEW LIST IS: \n";
    listprint(head3);
}

Comments

Popular posts from this blog

First_Come_First_Serve CPU Scheduling

Reversing stack Method 2 !! (One Helper Stack only)

Populating Next Right Pointers in Each Node in O(1) space (without queue and level order)

Calculate factorial of large numbers !! (Using Arrays)

Multiplication of large numbers (Given in string format)

Left View of Binary Tree (Method 1 using recursion)

Check Bracket Sequence

Image Multiplication

Boundary Traversal of binary tree

BST to greater sum tree