Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,537 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,521 people online right now. Registration is fast and FREE... Join Now!




Linked list: merge nodes

 
Reply to this topicStart new topic

Linked list: merge nodes, Merge 2 nodes into 1 node, sum the node values

CrazyJ
30 Oct, 2007 - 03:35 AM
Post #1

D.I.C Head
**

Joined: 15 Oct, 2007
Posts: 51


My Contributions
Hi, I am trying to understand the method used to merge 2 nodes in a linked list. I am new to this and am having a difficult time. The goal is to take from the user a nodeNumber and merge that node with the next node in a list into a single node, whose data element is the sum of the elements from the 2 merged nodes. Please help?

CODE

//  =============
    struct Node {
       int    datum;
       Node* nodePtr;
    }; // Node
//  ==========

//=========================================================================
// General Notes:

// 1) A link list is a collection of components called nodes.  Every node
//    in a linked list, except for the very last node, contains the address
//    of the next node in the list.
//
// 2) Thus, every node in a linked list contains two kinds of information:
//    A) the node data, and
//    B) the address of the next node in the list.  This address is called
//       the link to the next node.
// 3) The address of the first node in the list is stored in a separate
//    location called the head of the list or, sometimes, first.

//    =========================================================================

//    =======================
    class SinglyLinkedList
    {

        public:
            SinglyLinkedList( void );
           ~SinglyLinkedList( void );

            const SinglyLinkedList& operator= (const SinglyLinkedList& otherList);
            void  SinglyLinkedList::DeleteFirst();
            void  SinglyLinkedList::DeleteLast();
            void  SinglyLinkedList::DeleteNode( int );
            void  SinglyLinkedList::DestroyList();
            void  SinglyLinkedList::InitializeList();
            void  SinglyLinkedList::InsertFirst( int );
            void  SinglyLinkedList::InsertLast( int );
            bool  SinglyLinkedList::IsEmpty();
            int   SinglyLinkedList::Length();
            void  SinglyLinkedList::ObtainFirst( int& );
            void  SinglyLinkedList::ObtainLast( int& );
            void  SinglyLinkedList::Print();
            void  SinglyLinkedList::Search( int, int& );

//  The pointer last makes it possible to easily add a new node at the end
//  of the singly linked list without having to traverse the list from the
//  beginning (first) to the end.
    private:
        Node* first;
        Node* last;  

    }; // class SinglyLinkedList
//  ============================


CODE

// SinglyLinkedListProject.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include "SinglyLinkedList.h"

using namespace std;

int main(int argc, _TCHAR* argv[])
{
    int               nodeLocation;
    SinglyLinkedList list;
    SinglyLinkedList listB;

    cout << "Program Execution Beginning... " << endl;
    cout << endl;

    list.InitializeList();

    list.InsertFirst( 13 );
    list.InsertFirst( 44 );
    list.InsertFirst( 343 );
    list.InsertFirst( 65 );
    list.InsertFirst( 123 );
    list.InsertFirst( 45 );

    list.InsertFirst( 54 );
    list.InsertFirst( 69 );
    list.InsertFirst( 113 );
    list.InsertFirst( 34 );

    list.Print();
    list.Search( 12, nodeLocation );

    cout << "The length of the list is " << list.Length() << endl;
    cout << "Node location = " << nodeLocation << endl;

    list.DeleteNode(44);
    list.DeleteNode(45);
    list.Print();

    listB = list;  // Overloaded the assignment operator.
    listB.Print();

//  Provide an example of self-assignment.
    listB = listB;

    char userChar;
    cout << "Enter a character to terminate execution." << endl;
    cin >> userChar;

    return 0;
} // method main    


Thanks for any help in advance.
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Linked List: Merge Nodes
30 Oct, 2007 - 03:48 AM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
well you can just update one node.. like there are two nodes that needs to be merged, so work(the sum of the elements) with one node, and then just delete the other one, so the node that stays is the merged one. I hope you see where I'm getting at.
User is offlineProfile CardPM
+Quote Post

CrazyJ
RE: Linked List: Merge Nodes
30 Oct, 2007 - 05:00 AM
Post #3

D.I.C Head
**

Joined: 15 Oct, 2007
Posts: 51


My Contributions
QUOTE(PennyBoki @ 30 Oct, 2007 - 04:48 AM) *

well you can just update one node.. like there are two nodes that needs to be merged, so work(the sum of the elements) with one node, and then just delete the other one, so the node that stays is the merged one. I hope you see where I'm getting at.


I dont know how I would go about doing so... I am am very new to C++ and linked lists. I am springboarding from a completed linked list program, to merging 2 nodes in said program. Can you provide some sort of an example to illustrate the structure of merging nodes? blink.gif
Thank you so much!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:12PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month