Closed Thread
Results 1 to 7 of 7

Thread: Join 2 linked list (sorting is not important)

  1. #1
    nishbur's Avatar
    nishbur is offline Newbie
    Join Date
    Nov 2007
    Location
    mauritius
    Posts
    9
    Rep Power
    0

    Join 2 linked list (sorting is not important)

    HTML Code:
    hi everybody! thank you for viewing my post in order to help me
    i've created to linked list below start_ptr and begin_ptr.
    i just want to join the 2 linked lists in any order and i want to display the results
    the selected code below (in bold) is the one that should be modified to get the appropriate result 
    thank u 


    Code:
    #include < iostream.h>
    
    struct node
    {
        int age;
        node *nxt;
    };
    
    node *start_ptr= NULL;
    node *current;
    int choice = 7;
    
    struct node1
    {
        int age1;
        node1 *nxt;
    };
    
    node1 *begin_ptr= NULL;
    node1 *current1;
    
    
    void add_node1()
      {  node *temp, *temp2;   // Temporary pointers
    
         // Reserve space for new node and fill it with data
         temp = new node;
         cout << endl<<"Please enter number: ";
         cin >> temp->age;
         temp->nxt = NULL;
    
         // Set up link to this node
         if (start_ptr == NULL)
           { start_ptr = temp;
         current = start_ptr;
           }
         else
           { temp2 = start_ptr;
             // We know this is not NULL - list not empty!
             while (temp2->nxt != NULL)
               {  temp2 = temp2->nxt;
                  // Move to next link in chain
               }
             temp2->nxt = temp;
           }
      }
    
    void display_node1()
      {  node *temp;
         temp = start_ptr;
         cout << endl;
         if (temp == NULL)
           cout << "The list is empty!" << endl;
         else
           { while (temp != NULL)
           {  // Display details for what temp points to
                  cout << "Age : " << temp->age << endl;
                   cout << endl;
              temp = temp->nxt;
    
           }
         cout << "End of list!" << endl;
           }
      }
    void add_node2()
      {  node1 *temp, *temp2;   // Temporary pointers
    
         // Reserve space for new node and fill it with data
         temp = new node1;
         cout <<endl<< "Please enter number: ";
         cin >> temp->age1;
         temp->nxt = NULL;
    
         // Set up link to this node
         if (begin_ptr == NULL)
           { begin_ptr = temp;
         current1 = begin_ptr;
           }
         else
           { temp2 = begin_ptr;
             // We know this is not NULL - list not empty!
             while (temp2->nxt != NULL)
               {  temp2 = temp2->nxt;
                  // Move to next link in chain
               }
             temp2->nxt = temp;
           }
      }
    
    void display_node2()
      {  node1 *temp;
         temp = begin_ptr;
         cout << endl;
         if (temp == NULL)
           cout << "The list is empty!" << endl;
         else
           { while (temp != NULL)
           {  // Display details for what temp points to
                  cout << "Age : " << temp->age1 << endl;
                   cout << endl;
              temp = temp->nxt;
    
           }
         cout << "End of list!" << endl;
           }
      }
    
    void merge_list(struct node1 *begin_ptr ,struct node *start_ptr)
    {    
        if (start_ptr == NULL || begin_ptr == NULL)
        return; // In case of null pointers, do nothing
        while (begin_ptr->nxt != NULL)
        begin_ptr = begin_ptr->nxt;
        begin_ptr->nxt = start_ptr;
    
    }
    
    
    
    void main()
    {
        start_ptr = NULL;
        begin_ptr = NULL;
    
        do
        {
            cout << "***************************"<<endl;
            cout << "1.Add Records to list #1"<<endl<<endl;
            cout << "2.Display list #1" <<endl<<endl;
            cout << "3.Add Records to list #2"<<endl<<endl;
            cout << "4.Display list #2" <<endl<<endl;
            cout << "5.Merge list #1 and list #2"<<endl<<endl;
            cout << "6.Display Merged list" <<endl<<endl;
            cout << "7.Quit"<<endl<<endl;
            cout << endl;
            cout << " >>>";
    
            cin>> choice;
            switch(choice)
            {
                case 1: add_node1();break;
                case 2: display_node1();break;
                case 3: add_node2();break;
                case 4: display_node2();break
                case 5: merge_list();break;
                case 6: display_list();
            
            
            }
        }
            while (choice != 7);
    }

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    bobdark's Avatar
    bobdark is offline Programmer
    Join Date
    Jan 2010
    Location
    Haifa, Israel
    Posts
    164
    Rep Power
    9

    Re: Join 2 linked list (sorting is not important)

    How about something like this:
    Code:
    typedef struct nodeStruct {
        element   value;
        struct nodeStruct* next;
        struct nodeStruct* previous;
    //    int index;
    }  listNodeStruct;
    typedef  listNodeStruct* listNode;
    
    typedef struct listHeadStruct{
        listNode head;
        listNode tail;
        listNode iterator;
        copy userCopy;
        compare userCompare;
    //    print userPrint;
        kill userKill;
        
    } listStruct;
    typedef listStruct* list;
    
    ListResult merge(list first, list second){
        //params check
        first->tail->next = second->head;
        second->head->previous = first->tail;
        first->tail = second->tail;
        return ListSuccess;
    }
    I took the declarations of list and list node from my old implementation of list and just quickly wrote the approximate code for merge.So sorry if it doesn't compile or whatever. ListResult is an enum I defined and didn't put it here, it uses mostly to indicate what was wrong if some function could not be executed properly.
    Oh and same for the copy, kill, print, compare - those are typedefs for functions that should be provided by the user.

  4. #3
    nishbur's Avatar
    nishbur is offline Newbie
    Join Date
    Nov 2007
    Location
    mauritius
    Posts
    9
    Rep Power
    0

    Re: Join 2 linked list (sorting is not important)

    hi bobdark i am newbie in programming i know u trying hard to help
    but can u upload ur hold program or e-mail ur cpp file in order for me to understand it well
    thanks for ur help

  5. #4
    bobdark's Avatar
    bobdark is offline Programmer
    Join Date
    Jan 2010
    Location
    Haifa, Israel
    Posts
    164
    Rep Power
    9

    Re: Join 2 linked list (sorting is not important)

    I'm very sorry but I can not upload these specific files for some reasons. I will be very glad though to help you by explaining and pointing out errors in your code.

  6. #5
    nishbur's Avatar
    nishbur is offline Newbie
    Join Date
    Nov 2007
    Location
    mauritius
    Posts
    9
    Rep Power
    0

    Re: Join 2 linked list (sorting is not important)

    thanks bobdark i'll try explain it more in order for you to help


    Code:
    list 
    
    *********
    | 1 | 4 | -> NULL
    *********
    
    
    list2
    
    *********
    | 9 | 7 | -> NULL
    *********
    
    how to list3 should be
    
    
    list3
    
    ******************
    | 1 | 4 |9 | 7 | -> NULL (Unsorted)
    *****************
    
    or
    
    ******************
    | 1 | 4 |7 |9 | -> NULL (sorted)
    *****************
    
    below in the code (modified from that posted before)
    
    void merge_list()
    {
    
    don't know really what to write, despite having google the answer 
    
    
    }
    Code:
    #include < iostream.h>
    
    struct node
    {
        int age;
        node *nxt;
    };
    
    node *list= NULL;
    node *list1 =  NULL;
    node *list2= NULL;
    node *current;
    node *current1; 
    
    int choice = 7;
    
    
    void add_node1()
      {  node *temp, *temp2;   // Temporary pointers
    
         // Reserve space for new node and fill it with data
         temp = new node;
         cout << endl<<"Please enter number: ";
         cin >> temp->age;
         temp->nxt = NULL;
    
         // Set up link to this node
         if (list == NULL)
           { list = temp;
         current = list;
           }
         else
           { temp2 = list;
             // We know this is not NULL - list not empty!
             while (temp2->nxt != NULL)
               {  temp2 = temp2->nxt;
                  // Move to next link in chain
               }
             temp2->nxt = temp;
           }
      }
    
    void display_node1()
      {  node *temp;
         temp = list;
         cout << endl;
         if (temp == NULL)
           cout << "The list is empty!" << endl;
         else
           { while (temp != NULL)
           {  // Display details for what temp points to
                  cout << "Age : " << temp->age << endl;
                   cout << endl;
              temp = temp->nxt;
    
           }
         cout << "End of list!" << endl;
           }
      }
    void add_node2()
      {  node *temp, *temp2;   // Temporary pointers
    
         // Reserve space for new node and fill it with data
         temp = new node;
         cout << endl<<"Please enter number: ";
         cin >> temp->age;
         temp->nxt = NULL;
    
         // Set up link to this node
         if (list1 == NULL)
           { list1 = temp;
         current1 = list1;
           }
         else
           { temp2 = list1;
             // We know this is not NULL - list not empty!
             while (temp2->nxt != NULL)
               {  temp2 = temp2->nxt;
                  // Move to next link in chain
               }
             temp2->nxt = temp;
           }
      }
    
    void display_node2()
      {  node *temp;
         temp = list1;
         cout << endl;
         if (temp == NULL)
           cout << "The list is empty!" << endl;
         else
           { while (temp != NULL)
           {  // Display details for what temp points to
                  cout << "Age : " << temp->age << endl;
                   cout << endl;
              temp = temp->nxt;
    
           }
         cout << "End of list!" << endl;
           }
      }
    
    void merge_list()
    {
    
    } 
    
        
    void main()
    {
        list = NULL;
        list1 = NULL;
        List2 = NULL;
     
        do
        {
            cout << "***************************"<<endl;
            cout << "1.Add Records to list #1"<<endl<<endl;
            cout << "2.Display list #1" <<endl<<endl;
            cout << "3.Add Records to list #2"<<endl<<endl;
            cout << "4.Display list #2" <<endl<<endl;
            cout << "5.Merge list #1 and list #2"<<endl<<endl;
            cout << "6.Display Merged list" <<endl<<endl;
            cout << "7.Quit"<<endl<<endl;
            cout << endl;
            cout << " >>>";
    
            cin>> choice;
            switch(choice)
            {
                case 1: add_node1();break;
                case 2: display_node1();break;
                case 3: add_node2();break;
                case 4: display_node2();break;
                case 5: merge_list();
                
            }
        }
            while (choice != 7);
    }
    Last edited by ZekeDragon; 03-21-2010 at 01:38 AM. Reason: You should use [code] tags instead of [quote] tags.

  7. #6
    nishbur's Avatar
    nishbur is offline Newbie
    Join Date
    Nov 2007
    Location
    mauritius
    Posts
    9
    Rep Power
    0

    Re: Join 2 linked list (sorting is not important)

    thanks bolid! i would be really gratefull if u continue help

    the end of list should point to list2

    list->list2
    list3= list->list2

    how will i code it , i am trying for the past 7 days, still i can't get an answer

  8. #7
    bobdark's Avatar
    bobdark is offline Programmer
    Join Date
    Jan 2010
    Location
    Haifa, Israel
    Posts
    164
    Rep Power
    9

    Re: Join 2 linked list (sorting is not important)

    You didn't get my idea. You represent your linked list only by the first node, from which all other nodes are reachable. So if you want to merge two such lists, you have to find the last node in one of them, and concatenate to this node the first node of the second list.
    I represent a list by a struct that has pointers to first and last nodes of the list, and other useful information. This helps because I don't have to iterate over the whole list in order to find the last node. In my implementation I access the last node and make it's 'next' field point to the 'head' of the second list.
    If you want it sorted, the implementation depends on the invariants you keep true. If the given lists are sorted, what you do is this: you keep two iterators, one per list, at the start each iterator points to the first node of each list.
    At each step you compare the nodes that are pointed to by the iterators and for the smaller node, insert it to the new list and advance its iterator to the next node in that list.
    When one of the lists finishes, concatenate the rest of the second list to the resulted new list. Basically its called merge sort if I'm not wrong - try googling it.
    If your lists are not sorted, you will have to find at each iteration the minimal value and insert it to the new list. Of course there are other methods too, there are even tutorials here in the forum on array sorting - you can read them and use the provided ideas and methods.
    I'm sorry if I was unclear, if you don't understand I will right down the algorithms later.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. linked list
    By Hamed in forum C and C++
    Replies: 21
    Last Post: 01-06-2011, 09:19 PM
  2. Replies: 2
    Last Post: 06-28-2010, 08:45 PM
  3. help with Linked List
    By M0SS_99 in forum C and C++
    Replies: 3
    Last Post: 01-08-2010, 03:18 PM
  4. Linked list
    By BINNY88 in forum C and C++
    Replies: 5
    Last Post: 10-03-2008, 06:04 AM
  5. Linked list
    By borny86 in forum Pascal and Delphi
    Replies: 9
    Last Post: 12-06-2007, 09:42 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts