?
Solved

APPLY BINARY MERGE on LINKED LIST INSTEAD OF FILE

Posted on 2007-04-06
3
Medium Priority
?
260 Views
Last Modified: 2010-04-01
APPLY BINARY MERGE on LINKED LIST INSTEAD OF FILE (using C)
0
Comment
Question by:bpc1985
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 18864038
What's your question ?
0
 

Author Comment

by:bpc1985
ID: 18864058
APPLY BINARY MERGE on LINKED LIST INSTEAD OF FILE (using C), I have a problem when merge them again (begin from // MERGE F1, F2 to F0 )

#include <iostream.h>
#include <conio.h>
#include <mem.h>
#include <stdlib.h>

typedef int elem;

typedef struct NODE{
      elem data;
        struct NODE *next;
} NODE;

typedef NODE *list;

void chen(list &l,elem x)
{
      l->next = new NODE;
      l = l->next;
        l->data = x;
}

void nhap(list &l, int &n)
{
      char c;
      elem x;
      l = new NODE;
      list p = l;
      l->next = NULL;
        do{
            cout<<"\n\t\tTAO FILE\n";
            cout<<"1. Tao ngau nhien"<<endl;
            cout<<"2. Tao bang tay"<<endl;
            cout<<"Chon: ";
                c = getche();
      }while (c!='1' && c!='2');
      cout<<"\nNhap so luong phan tu: ";
      cin >> n;
      if(c=='1')
      {
            randomize();
            for(int i=0;i<n;i++)
            {
                  x = random(1000);
                  chen(l,x);
            }
      }
      else
      {
              cout<<"Nhap vao day so: ";
            for(int i=0;i<n;i++)
            {
                  cin >> x;
                  chen(l,x);
              }
      }
      l->next = NULL;
      l=p->next;
        delete p;
}

void xuat(list l)
{
      list p = l;
      elem x;
      cout<<"\n\t\tBat dau xuat ket qua: \n";
      while(p!=NULL)
      {
            x = p->data;
            cout << x <<"\t";
            p = p->next;      
      }
}

void binary_list_sort(list &l, int n)
{
      int p=1,i,m,q,r;
      list l1,l2,pl;
      do
      {
                  // SPLIT F0 INTO F1,F2
            list p1 = l1 = new NODE;
            list p2 = l2 = new NODE;
            while(l!=NULL)
            {
                  i=0;
                  while(i<p && l!=NULL)
                  {
                        l1->next = l;
                        l1 = l;
                                i++;
                        l=l->next;
                  }
                  i=0;
                  while(i<p && l!=NULL)
                  {
                        l2->next = l;
                        l2 = l;
                                i++;
                        l=l->next;
                  }      
            }

            l1->next = l2->next = NULL;
            l1=p1->next;
            l2=p2->next;
            delete p1;
            delete p2;

                  // MERGE F1, F2 to F0
            pl = new NODE;
            l = pl ;
            m=n;
            do
                {
                  q = (p<m?p:m);
                  cout<<"\n GIA TRI l1: "<<l1->data;
                  m = m-q;
                  r = (p<m?p:m);
                  cout<<"\n GIA TRI l2: "<<l2->data;
                  m = m-r;
                  while(q>0 && r>0 && l1!=NULL &&l2!=NULL)
                  {
                        if(l1->data < l2->data)
                        {
                              l->next = l1;
                              l1= l1->next;
                                        q--;
                        }
                        else
                        {
                              l->next = l2;
                              l2= l2->next;
                                        r--;
                        }
                                l = l->next;
                  }
                  while(q>0 && l1!=NULL && l2==NULL)
                  {
                        l->next = l1;
                        l1= l1->next;
                                q--;
                        l=l->next;
                  }
                  while(r>0  && l1==NULL &&l2!=NULL)
                  {
                        l->next = l2;
                        l2= l2->next;
                                r--;
                        l=l->next;
                  }
            }
            while(m>0);
            p = p*2;
      }
      while(p<n);
      l1=l2=NULL;
      l=pl->next;
        delete pl;
}

int main()
{
      list l=NULL;
        int n;
      elem x;
      nhap(l,n);
      xuat(l);
      binary_list_sort(l,n);
      xuat(l);
        return 0;
}  
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 375 total points
ID: 18864549
A few problems :


1) replace this :

        #include <iostream.h>
        #include <conio.h>
        #include <mem.h>
        #include <stdlib.h>

    with this :

        #include <iostream>    // <iostream.h> is deprecated and should not be used !!
        #include <conio.h>
        #include <mem.h>
        #include <cstdlib>
       
        using std::cout;      // these to tell the compiler in which namespace cout etc. can be found
        using std::endl;
        using std::cin;



2) I assume these two lines were added for debugging :

                  cout<<"\n GIA TRI l1: "<<l1->data;
                  cout<<"\n GIA TRI l2: "<<l2->data;

    but be carefull, because you didn't check l1 and l2 before dereferencing. So, make that :

                  if (l1) cout<<"\n GIA TRI l1: "<<l1->data;
                  if (l2) cout<<"\n GIA TRI l2: "<<l2->data;



3) These two while loops :

                  while(q>0 && l1!=NULL  && l2==NULL)
                  // <SNIP>
                  while(r>0  && l1==NULL  &&l2!=NULL)

    should not contain the checks for NULL, so make that :

                  while(q>0 && l1!=NULL)
                  // <SNIP>
                  while(r>0  && l2!=NULL)



4) These 3 lines were put too late :

        l1=l2=NULL;
        l=pl->next;
        delete pl;

    They should have been here :

            // <SNIP>
            }
            while(m>0);
            l1=l2=NULL;                // <--- HERE
            l=pl->next;                // <--- HERE
            delete pl;                // <--- HERE
            p = p*2;
        }
        while(p<n);





To summarize, after these modifications, the binary_list_sort() function becomes :

void binary_list_sort(list &l, int n)
{
      int p=1,i,m,q,r;
      list l1,l2,pl;
      do
      {
                  // SPLIT F0 INTO F1,F2
                  cout << "SPLIT F0 INTO F1,F2" << endl;
            list p1 = l1 = new NODE;
            list p2 = l2 = new NODE;
            while(l!=NULL)
            {
                  i=0;
                  while(i<p && l!=NULL)
                  {
                        l1->next = l;
                        l1 = l;
                                i++;
                        l=l->next;
                  }
                  i=0;
                  while(i<p && l!=NULL)
                  {
                        l2->next = l;
                        l2 = l;
                                i++;
                        l=l->next;
                  }      
            }

            l1->next = l2->next = NULL;
            l1=p1->next;
            l2=p2->next;
            delete p1;
            delete p2;

                  // MERGE F1, F2 to F0
                  cout << "MERGE F1, F2 to F0" << endl;
            pl = new NODE;
            l = pl ;
            m=n;
            do
                {
                  q = (p<m?p:m);
                  if (l1) cout<<"\n GIA TRI l1: "<<l1->data;
                  m = m-q;
                  r = (p<m?p:m);
                  if (l2) cout<<"\n GIA TRI l2: "<<l2->data;
                  m = m-r;
                  while(q>0 && r>0 && l1!=NULL &&l2!=NULL)
                  {
                        if(l1->data < l2->data)
                        {
                              l->next = l1;
                              l1= l1->next;
                                        q--;
                        }
                        else
                        {
                              l->next = l2;
                              l2= l2->next;
                                        r--;
                        }
                                l = l->next;
                  }
                  while(q>0 && l1!=NULL)
                  {
                        l->next = l1;
                        l1= l1->next;
                                q--;
                        l=l->next;
                  }
                  while(r>0  &&l2!=NULL)
                  {
                        l->next = l2;
                        l2= l2->next;
                                r--;
                        l=l->next;
                  }
            }
            while(m>0);
      l1=l2=NULL;
      l=pl->next;
        delete pl;
                    p = p*2;
      }
      while(p<n);

}
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question