Solved

APPLY BINARY MERGE on LINKED LIST INSTEAD OF FILE

Posted on 2007-04-06
3
251 Views
Last Modified: 2010-04-01
APPLY BINARY MERGE on LINKED LIST INSTEAD OF FILE (using C)
0
Comment
Question by:bpc1985
  • 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 125 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now