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.