Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

APPLY BINARY MERGE on LINKED LIST INSTEAD OF FILE

Posted on 2007-04-06
3
Medium Priority
?
262 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
When there is a disconnect between the intentions of their creator and the recipient, when algorithms go awry, they can have disastrous consequences.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
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…

610 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