GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 161 of 203    Print  
concatenate two circular linked lists
Write a program to concatenate two circular linked lists into a single circular list.


  
Total Answers and Comments: 3 Last Update: October 19, 2009     Asked by: vivek180984 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
September 01, 2008 18:10:51   #1  
Keyurpatel80 Member Since: October 2007   Contribution: 2    

RE: concatenate two circular linked lists
Hi
It is very simple.
Get the Node values from first Circular list and stored in one temp. array.
You can keep track of repeated values by checking and comparing each value and then put into array.

Now you can add all this array values into second link list.

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
July 13, 2009 13:09:05   #2  
Bhimasena Member Since: July 2009   Contribution: 1    

RE: concatenate two circular linked lists
void cat(Node* ll1 Node* ll2)
{
Node* temp ll1->next;
ll1->next ll2->next;
ll2->next temp;
}

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
October 19, 2009 00:35:50   #3  
j_l_larson Member Since: June 2008   Contribution: 4    

RE: concatenate two circular linked lists
class node {
public:
node(int n) : m_n(n) { m_pNext this; }
~node() {}
void SetNext( node* p ) { m_pNext p; }
node* GetNext() const { return m_pNext; }
int GetData() const { return m_n; }
static void deleteNode(node* p) { delete p; }
private:
int m_n;
node* m_pNext;
};

typedef void (*visitFnc)(node*);

class CyclicList {
public:
CyclicList() { Null(); }
~CyclicList() {
Visit(node::deleteNode);
}
void Null() { m_pTail m_pHead NULL; }
void Append(int n) {
node* p new node(n);
if ( ! m_pHead ) {
m_pHead m_pTail p;
return;
}
m_pTail->SetNext(p);
p->SetNext( m_pHead );
m_pTail p;
}
void Visit( visitFnc f ) {
node* p m_pHead;
while (p) {
node* pNext p->GetNext();
f(p);
if ( p m_pTail )
break;
p pNext;
}
}
void Join( CyclicList& cl ) {
if ( cl.m_pHead NULL )
return;
if ( m_pHead NULL ) {
m_pHead cl.m_pHead;
m_pTail cl.m_pTail;
cl.Null();
return;
}
m_pTail->SetNext(cl.m_pHead);
cl.m_pTail->SetNext(m_pHead);
m_pTail cl.m_pTail;
cl.Null();
}
private:
node* m_pHead;
node* m_pTail;
};


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape