Joshua_143

Commendable
Dec 7, 2016
2
0
1,510
So i wrote a program which take 2 linked lists and merges them but for some reason it does not work!!!!
it gets complied 0 ERRORS 0 WARNINGS
but for some reason (logical i think) it does not merge properly

C++:
#include <iostream>
#include <iomanip>
#include <conio.h>

using namespace std;

class llist
{
  struct node
   {
     int data;
     node *link;

   }*start1,*start2,*start3;
public:
  llist(){start1 = start2 = start3 = NULL ;}
  void create();
  void display();
  void display_second();
  void mergef();
  ~llist();
};

void llist :: create()
 {

   int n,i,m;
   cout << "How many nodes you want to create?" << endl;
   cin >> n;
   cout<<endl;
   node *first,*last,*temp;
   first = new node;
   cout<<"Enter the data part of node 1"<<endl;
   cin>>first->data;
   first->link = NULL;
   temp = first;

for (i = 1;i<n;i++){
  last = new node;
  cout << "Enter the data part of node "<<i+1<<endl;
  cin >> last ->data;
  last ->link = NULL;
  temp -> link = last;
  temp = last;
        }
start1 = first;

cout<<"-----------------------------------------------------------"<<endl;

     cout<<endl<<"How many nodes do you want to create in linked list 2?"<<endl;
     cin >> m;
     cout<<endl;
     node *one ,*sec,*t;
     one = new node;
     cout<<"Enter the data part of the 1 node"<<endl;
     cin>>one ->data;
     one -> link = NULL;
     t = one;

    for(i = 1; i < m ;i++)
       {
      sec = new node;
      cout << "Enter the data part of the "<<i+1<<" node"<<endl;
      cin >> sec -> data;
      sec -> link = NULL;
      t -> link = sec;
      t = sec;
       }
   start2 = one;
 }

void llist :: mergef()
 {
   node *p ,*temp1,*temp2,*last;

   p = new node;
   p->link = NULL;

  for (temp1 = start1,temp2 = start2;(temp1 != NULL) && (temp2 != NULL) ;)
    {
      if ((temp1->data) < (temp2->data))
      {
        p -> data = temp1 ->data;
        temp1 = temp1 -> link;
      }
        else
          {
        p -> data = temp2->data;
        temp2 = temp2->link;
          }
      if(start3 == NULL)
          start3 = p;
          else
             last->link = p;

      last = p;
    }

   for(;temp1!=NULL;temp1 = temp1->link)
    {
        p = new node;
        p -> data = temp1->data;
        p -> link = NULL;
        last ->link = p;
        last = p;
    }

   for(;temp2!=NULL;temp2 = temp2->link)
    {
        p = new node;
        p->data = temp2->data;
        p->link = NULL;
        last->link = p;
        last = p;

    }
 }

   void llist :: display_second()
    {
  cout<<"The Merged LinkList is as follows"<<endl;
    node *p = start3;
    while (p!=NULL)
    {
        cout << p->data<<"->";
        p = p->link;
    }
    cout<<"END"<<endl<<endl;
   }


 void llist :: display()
  {

      cout<<endl<< "The first Linked List is as follows"<<endl;
      node *p  = start1;
      node *p2 = start2;
      while(p!=NULL)
      {
          cout << p->data<<"->";
          p = p->link;
      }
      cout<<"END"<<endl<<endl;

      cout<<"The second Linked List is as follows"<<endl;

     while(p2!= NULL)
     {
         cout<<p2->data<<"->";
         p2 = p2->link;
     }
     cout<<"END"<<endl;
  }


  llist :: ~llist()
  {
      node *p = start1;

      while(start1!=NULL)
      {
          p = start1->link;
          delete start1;
          start1 = p;

      }
  }

  int main()
  {
      llist l;
      int ch;
      //system("cls");
      do{
            cout<<endl<<"1)Create and display the two linked lists"<<endl<<"2)Merge the 2 linked list"<<endl;
            cout<<"3)Exit"<<endl;
            cin>>ch;

            switch(ch)
            {
            case 1:
                l.create();
                l.display();
                break;
            case 2:
                l.mergef();
                l.display_second();
                break;
            case 3:
                break;
            default:
                cout<<"Wrong Option"<<endl;

            }

      }while(ch!=3);

    return 1;
  }
 

ex_bubblehead

Distinguished
Moderator

And what have you done to troubleshoot this code?

What is the input?
What is the expected output?

Have you single stepped the code, recording the intermediate values at every step and comparing to expected values?

As already stated, this is your homework and we're not going to do your work for you.