Solved

APPLY BINARY MERGE on LINKED LIST INSTEAD OF FILE

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

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

728 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