Linked lists 
  Apr 4, 2010 at 2:25pm UTC  
 
I need to write a program to concatenate two linked lists.
I have to include functions to:
 - create the two lists
 - display each list
 - concatenate the two lists
 - display the new list created after the concatenation process.
Here is what i managed to do till now
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 
#include <cstdlib> 
#include <iostream> 
using  namespace  std;
struct  link
    {
           char  *data;
           link *next;
    };
    
    typedef  struct  link node;
    node *head;
struct  link2
    {
           char  *data2;
           link2 *next;
    };
           
    typedef  struct  link2 node2;
    node *head2, *current, *templist;
    
    char  a[30];
    void  populatelist();
    void  displaylist();
int  main()
{    
    head = new  node;
    head->data = "Name: " ;
    head->next = NULL;
    
    head->next = new  node;
    head->next->data = "Surname: " ;
    head->next->next = NULL;
    
    head->next->next = new  node;
    head->next->next->data = "Telephone: " ;
    head->next->next->next = NULL;
    
    cout << "List 1 has been created\n\n" ;
    
    cout << head->data <<endl;
    cout << head->next->data <<endl;
    cout << head->next->next->data <<endl;
    cout << "\n\n" ;
    
    head2 = NULL;
    
    populatelist();
    
    cout << "Data Entered\n\n" ;
    displaylist();
    system("pause" );
    return  EXIT_SUCCESS;
}
void  populatelist()
{
     cout << "Enter Name, Surname and then Address\n\n" ;
     for (int  i=0;i<3;i++)
     {
           cout << "->Enter data: " ;
           cin >> a;
           current = new  node;
           current->data2=a;
           current->next = head2;
           head2 = current;
           cout << "Data has been populated...next data\n\n" ;
     }
}
void  displaylist()
{
     templist = head2;
     while (templist != NULL)
     {
          cout << "-->"  << templist->data <<endl;
          templist=templist->next;
     }
     
     cout << "\n" ;
}
 
PLZ HELP!
 
 
 
 
 
  Apr 4, 2010 at 9:13pm UTC  
 
That's easy, just change the link pointer of the last element of the first list, to the first element of the second list, done.
 
 
 
 
  Apr 5, 2010 at 12:46pm UTC  
 
im new at programming and i barely managed to do this much by going over several tutorials 
i don't think my 2nd list is even being read 
because as output i only get the phone number displayed thrice
 
 
 
 
  Apr 6, 2010 at 10:28am UTC  
 
Mistake here(line 69):
All nodes in 2nd list take pointer to the same array a[].
Solution may be like this:
1 2 
current->data2 = new  char  [strlen(a)+1];  //plus 1 for null-character 
strcpy(current->data2, a);
 
*don't forget release memory afterwards,
may be like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
void  DisposeList()
{
node2 *templist;
node2 *templist_next;
	templist = head2;
	while  (templist)
	{
		templist_next = templist->next;
		delete  [] templist->data2;
		delete  templist;
		templist = templist_next;
	}
}
 
** here a misprint(line 22) ?
node *head2, *current, *templist; 
mean:
node2 *head2, *current, *templist; 
 
 
 
 
Topic archived. No new replies allowed.