i make twwo single linkedlist,,
but i didn't know how i can merge those lists ;;

#include <iostream.h>
class linklist
{ private:
             struct node
			 {
               int data;
             node *link;
			 } *p;
  
   public:
  
              linklist();
        void append( int num );
		void appendd(int size);
		void display();
};
 linklist::linklist()
 {
      p=NULL;
 }
 void linklist::append(int num)
 {
      node *q,*t;
    if( p == NULL )
    {
        p = new node;
       p->data = num;
       p->link = NULL;
	   cin>>p->data;
    }
    else
    {
         q = p;
       while( q->link != NULL )
           q = q->link;
       t = new node;
       t->data = num;
       t->link = NULL;
       q->link = t;
	   cin>>t->data;
    }
 }
 void linklist::appendd(int size)
 {
      node *z,*m;
    if( p == NULL )
    {
        p = new node;
       p->data = size;
       p->link = NULL;
	   cin>>p->data;
    }
    else
    {
         z = p;
       while( z->link != NULL )
           z = z->link;
       m= new node;
       m->data = size;
       m->link = NULL;
       z->link = m;
	   cin>>m->data;
    }
 }
 void linklist::display(){
	 
	   
	 cout<<" Elements are ";
	
	 while (p!=NULL){
		 
		cout<<p->data<<" ";
p=p->link;
 }} 
 int main()
 {int num,size;
      linklist opj;
    cout<<"No. of elements = ";
	cin>>num;
	cout<<"The nodes are #";
	for (int i=0;i<num;i++)
   opj.append(num );
	opj.display();
	cout<<"\nENTER";
	cin>>size;
for (  i=0;i<size;i++)
	opj.appendd(size);
opj.display();
    return 0;
 }

I'm not going to read your code, it is messy.

In general, say you have two singly linked lists p and q. To link them together, you can simply make the last element in p point to the first element of q.

In pseudocode:

LinkedList p,q;

// traverse to the end of p
while( nullptr != p->next )
    p = p->next;

// We're at the end, now point the next element to the beginning of q
p->next = q->first;

In case you wonder, nullptr is a c++0x feature which should be used to 'replace' the use of 'NULL' and 0 in 'old-style' C++.

Edited 5 Years Ago by thelamb: n/a

ok...
i reformatted your code and it really did not make any sense.....

#include <iostream.h>

class linklist
{ 
	private:
		struct node
			{
				int data;
				node *link;
			} *p;
  
	public:
		linklist();
        void append( int num );
		void appendd(int size);
		void display();
};

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

void linklist::append(int num)
{
	node *q,*t;
    
	if( p == NULL )
    {
		p = new node;
		p->data = num;
		p->link = NULL;
		cin>>p->data;
    }
    else
    {
		q = p;
		while( q->link != NULL )
			q = q->link;
		t = new node;
		t->data = num;
		t->link = NULL;
		q->link = t;
		cin>>t->data;
    }
 }
 
void linklist::appendd(int size)
{
	node *z,*m;
    if( p == NULL )
    {
		p = new node;
		p->data = size;
		p->link = NULL;
		cin>>p->data;
    }
    else
    {
		z = p;
		while( z->link != NULL )
			z = z->link;
		m= new node;
		m->data = size;
		m->link = NULL;
		z->link = m;
		cin>>m->data;
    }
}

void linklist::display()
{
	cout<<" Elements are ";
	while (p!=NULL)
	{
		cout<<p->data<<" ";
		p=p->link;
	}
}
 
int main()
{
	int num,size;
    linklist opj;
    cout<<"No. of elements = ";
	cin>>num;
	cout<<"The nodes are #";
	for (int i=0;i<num;i++)
		opj.append(num );
	opj.display();
	cout<<"\nENTER the size";
	cin>>size;
	for (  i=0;i<size;i++)
		opj.appendd(size);
	opj.display();
    return 0;
}

When you write a code please follow proper formatting.....
Please explain your code.....
what exactly are you trying to achieve....
first you ask for number of elements....
suppose i enter 8 ... then you create nodes storing numbers 1 to 8....
and then you again ask for input and over write the data....
then you ask for size.....
and you repeat the same for size too...
tell us what exactly you want.....
The code is real messy bro....

This article has been dead for over six months. Start a new discussion instead.