Solved

APPLY BINARY MERGE on LINKED LIST INSTEAD OF FILE

Posted on 2007-04-06
3
255 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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 …
Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

776 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