GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Apr-2005, 00:50
etymologist etymologist is offline
New Member
 
Join Date: Apr 2005
Posts: 1
etymologist is on a distinguished road
Exclamation

merging two sorted linklist problem


can someone tell me where is the bug
the merge function is not working as desired


CPP / C++ / C Code:
#include <iostream>
using namespace std;

class linklist
{
	private:
		struct node
		{
				int data;
				node *link;
		} *p;

	public:
		linklist();
		void addatend(int num);
		void addatbeg(int num);
		void addafter (int loc, int num);
		void display();
		int count();
		void add(int num);
		void del(int num);
		void merge (linklist &m, linklist &n);
		//void sort();
		void reverse();
		~linklist();
};

void linklist:: reverse()
{
	node *q, *r, *s;
	q=p;
	r=NULL;
	
	while(q!=NULL)
	{
		s=r;
		r=q;
		q=q->link;
		r->link=s;
	}
	
	p=r;
}

linklist:: linklist()
{
	p = NULL;
}

void linklist :: addatbeg(int num)
{
	node *temp;
	temp = new node;
	temp->data = num;
	temp->link=p;
	p=temp;
}

void linklist :: addatend(int num)
{
	struct node *temp, *r;
	if (p==NULL)
	{
		p = new node;
		p->data=num;
		p->link=NULL;
	}
	
	else
	{
		temp=p;
		
		while(temp->link != NULL)
		{
			temp=temp->link;
		}
		
		r =new node;
		r->data=num;
		r->link=NULL;
		temp->link=r;
	}
}

void linklist :: display()
{
	node *temp;
	temp = p;
	
	while (temp != NULL)
	{
		cout << endl;
		cout << temp->data;
		temp= temp->link;
	}
	
	cout << "Hello";
}

linklist :: ~linklist()
{
	node *temp, *r;
	r=p;
	while(temp!= NULL)
	{ 
		temp = r->link;
		delete r;
		r = temp;
	}
}	

void linklist :: addafter (int loc, int num)
{
	int cnt ;
	cnt = count();
	if ((loc < 1) || (loc > cnt ))
	{
		cout << "Input not valid ";
	}
	else
	{
		node *m=p;
		for (int i=1; i<loc; i++)
		{
			m = m->link;
		}
		
		node *temp;
		temp = new node;
		temp->data= num;
		temp->link=m->link;
		m->link=temp;
	}
}
	

int linklist :: count ()
{
	if (p==NULL)
	{
		return  0;
	}
	
	else
	{
		int count = 0;
		
		node *ct;
		ct = p;
		while(ct!=NULL)
		{
			count++;
			ct=ct->link;
		
		}
			
		return count;
	}
}
		
void linklist::add(int num) // add in ascending order
{
	node *r, *temp;
	r= new  node;
	r->data=num;
	temp=p;
	
	if (p==NULL || temp->data>num)
	{
		p=r;
		p->link=temp;
	}
	
	else
	{
		while(temp!=NULL)
		{
			if (temp->data <= num && ((temp->link==NULL)||(temp->link->data>num)))
			{
				r->link=temp->link;
				temp->link=r;
				return;
			}
			
			temp=temp->link;
		}		
	}
}
	
void linklist::merge(linklist &m, linklist &n)
{
    node *z;
    z=p;
    
    if(m.p==NULL && n.p==NULL) // if both list are empty
    return;
    
    while(m.p!=NULL && n.p!=NULL)
    {
        if(m.p->data>n.p->data)
        {
            z=new node;
            z->data=m.p->data;
            z->link=NULL;
            z=z->link;
            m.p=m.p->link;
        }
        
        else
        {
            if (m.p->data<n.p->data)
            {
                z=new node;
                z->data=n.p->data;
                z->link=NULL;
                z=z->link;
                n.p=n.p->link;
            }
            
            else 
            {
                z=new node;
                z->data=n.p->data;
                z->link=NULL;
                z=z->link;
                n.p=n.p->link;
                m.p=m.p->link;
            }
        }
    }
        
        while(m.p!=NULL)
        {
            z=new node;
            z->data=m.p->data;
            z->link=NULL;
            z=z->link;
            m.p=m.p->link;
        }
            
        while(n.p!=NULL)
        {
            z=new node;
            z->data=n.p->data;
            z->link=NULL;
            z=z->link;
            n.p=n.p->link;
        }
}
	
int main()
{

linklist m,n,l;

m.add(87);
m.add(82);
m.add(37);
m.add(47);
m.add(57);
m.add(77);
m.add(81);
m.display();

n.add(77);
n.add(93);
n.add(97);
n.add(33);
n.add(63);
n.add(73);
n.display();

l.merge(m,n);
l.display();
return 0;
}
Last edited by LuciWiz : 21-Apr-2005 at 01:10. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 21-Apr-2005, 03:14
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
CPP / C++ / C Code:
if(m.p->data>n.p->data)
 {
    z=new node;
    z->data=m.p->data;
    z->link=NULL;
    z=z->link;
    m.p=m.p->link;
  }
You set the link for z to NULL, then you set z to point to whatever its link points to, but its link points to NULL, so now z points to NULL, and you have lost the node. This is done several times throughout your Merge algorithm. Also, when you are done using z, you should delete it. As it stands, your algorithm will produce many memory leaks. A few more tips to help you get it working include:

1) Use more meaninful names. Instead of using the letter p to denote the front of the list, try using "front". And instead of using "link" to denote the pointer to the next node in the list, try using "next". Most of your variable names are quite meaningless to the naked eye.

2) Write the pseudo-code first. The "big bang" is not a good method of programming. Plan, then execute. Don't ever just start writing code for a program unless it is completely trivial.

3) Use C and /C tags when posting code on this forum... it adds syntax hilighting that makes it much easier to help you! And no... it does not do it automatically, someone edited your post for the sake of the people reading it! So, be sure to use the C and /C tags whenever you post code on this forum!
__________________
-Aaron
  #3  
Old 21-Apr-2005, 03:24
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Here is a proper algorithm for merging two sorted lists.

CPP / C++ / C Code:
/*
while both lists are not empty...
  {
    if the top of list1 is less than the top of list2...
      insert the value at the top of list1 into the union
    else if the top of list1 is less than the top of list2...
      insert the top of list2 into the union
    else
      insert the top of list1 into the intersection
  }
while list1 is not empty...
  insert the top of list1 into the union
while list2 is not empty...
  insert the top of list2 into the union
*/
__________________
-Aaron
  #4  
Old 10-May-2005, 02:55
Aghak Aghak is offline
New Member
 
Join Date: May 2005
Posts: 1
Aghak is on a distinguished road
This is classical problem.
CPP / C++ / C Code:
// First find the nodes of each list.
// Create a list of data type;
I will use int, but you may use template.

int* pDataListA = new int[ElemntInListA];
int* pDataListB = new int[ElemntInListB];
int* pResultList = new int[ElemntInListA + ElemntInListB];


Node* pANode = HeadOfLinkListA;
int Index = 0;
while (pANode != NULL)
{
	pDataListA[Index] = 	pANode->data;
Index++;
pANode = pANode->link;
}

Node* pBNode = HeadOfLinkListB;
Index = 0;
while (pBNode!= NULL)
{
	pDataListB[Index] = 	pBNode ->data;
Index++;
pBNode = pBNode ->link;
}

MergeSort(pDataListA, pDataListB, pResultList);

Create a new linklist and use your add Function with pResultList.
delete pDataListA ;
delete pDataListB;
delete pDataListC;

MergeSort(int* pArrayA, int SizeA, int* pArrayB, int SizeB, int* pArrayC)
{
int aDex = 0;
int bDex = 0;
int cDes = 0;
while ( aDex < SizeA && bDex < SizeB)
pArrayC[cDex++] =  (pArrayA[aDex] < pArrayB[bDex]) ? pArrayA[aDex++] :
 pArrayB[BDex++]
while (aDex < SizeA)
pArrayC[cDex++] =   pArrayA[aDex++] :

while (bDex < SizeB)
pArrayC[cDex++] =   pArrayB[bDex++] :
}


Agha Khan
ahmedshujaa@hotmail.com
Last edited by LuciWiz : 10-May-2005 at 02:59. Reason: Please insert your C code between [c] & [/c] tags
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13
C I/O problem kelly80 C Programming Language 4 27-Apr-2004 21:15
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 08:53
How I sorted my website compression problem jrobbio MySQL / PHP Forum 0 05-Mar-2003 21:41

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:01.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.