Linked List
We will be covering Linked List in more detail in future modules. For now - here is a brief code example:
IntNode.hpp
//
// IntNode.hpp
// LinkedListIntro
//
// Created by Kevin Roark on 2/3/24.
//
#ifndef IntNode_hpp
#define IntNode_hpp
class IntNode {
public:
// Constructor
IntNode(int dataInit = 0, IntNode* nextLoc = nullptr);
// Insert node after this node
void InsertAfter(IntNode* nodeLoc);
// Get location pointed by nextNodePtr
IntNode* GetNext();
// Print dataVal
void PrintNodeData();
private:
// Data value
int dataVal;
// Pointer to the next node
IntNode* nextNodePtr;
};
#endif /* IntNode_hpp */
IntNode.cpp
#include "IntNode.hpp"
#include <iostream>
using namespace std;
//MARK: Constructor: The constructor IntNode(int dataInit = 0, IntNode* nextLoc = nullptr)
//initializes a new instance of IntNode.
//It accepts two parameters: dataInit (default value 0) which sets the value
//of dataVal and nextLoc (default value nullptr) which sets the value of nextNodePtr.
IntNode::IntNode(int dataInit, IntNode* nextLoc) {
this->dataVal = dataInit;
this->nextNodePtr = nextLoc;
}
/* Insert node after this node.
* Before: this -- next
* After: this -- node -- next
*/
//MARK: InsertAfter: The method InsertAfter(IntNode* nodeLoc) inserts
//the node pointed to by nodeLoc immediately after the current node.
//It starts by storing the old next node in tmpNext. Then it sets nextNodePtr
//of the current node to nodeLoc, effectively placing nodeLoc after the
//current node in the list. Finally, it sets the nextNodePtr of nodeLoc to
//tmpNext, placing the original next node after nodeLoc.
void IntNode::InsertAfter(IntNode* nodeLoc) {
IntNode* tmpNext = nullptr;
tmpNext = this->nextNodePtr; // Remember next
this->nextNodePtr = nodeLoc; // this -- node -- ?
nodeLoc->nextNodePtr = tmpNext; // this -- node -- next
}
//MARK: PrintNodeData: The method PrintNodeData() prints the value of
//dataVal to the console. This value is the data stored in the current node.
void IntNode::PrintNodeData() {
cout << " Data in the Node: " << this->dataVal << endl;
}
// Get location pointed by nextNodePtr
//MARK: GetNext: The method GetNext() returns the node that comes after
//the current node in the list. This is the node pointed to by nextNodePtr.
IntNode* IntNode::GetNext() {
return this->nextNodePtr;
}
main.cpp (Driver)
#include <iostream>
#include "IntNode.hpp"
using namespace std;
int main() {
// Create IntNode objects
IntNode* headObj = nullptr;
IntNode* nodeObj1 = nullptr;
IntNode* nodeObj2 = nullptr;
IntNode* nodeObj3 = nullptr;
IntNode* nodeObj4 = nullptr;
IntNode* currObj = nullptr;
IntNode* tempObj = nullptr; // To hold the next node before deletion
// Front of nodes list
headObj = new IntNode(-1);
// Additional nodes are inserted after the headObj
nodeObj1 = new IntNode(111);
headObj->InsertAfter(nodeObj1);
nodeObj2 = new IntNode(222);
nodeObj1->InsertAfter(nodeObj2);
nodeObj3 = new IntNode(333);
nodeObj2->InsertAfter(nodeObj3);
nodeObj4 = new IntNode(444);
nodeObj1->InsertAfter(nodeObj4);
// Print linked list
currObj = headObj;
while (currObj != nullptr) {
currObj->PrintNodeData();
currObj = currObj->GetNext();
}
// Reset currObj to headObj for deletion
currObj = headObj;
while (currObj != nullptr) {
tempObj = currObj->GetNext(); // Save the next node
delete currObj; // Delete the current node
currObj = tempObj; // Move to the next node
}
return 0;
}
This C++ code defines a linked list of integer values using a custom class called IntNode
. Each IntNode
object represents a node in the linked list, and contains an integer value (dataVal
) and a pointer to the next node in the list (nextNodePtr
).
Here's a brief overview of the code:
The
IntNode
class defines a constructor that takes two arguments:dataInit
andnextLoc
. These arguments initialize thedataVal
andnextNodePtr
members, respectively.The
InsertAfter
member function takes a pointer to anIntNode
object as an argument, and inserts the object after the current node in the list. This is done by first storing the next node in a temporary pointer (tmpNext
), updatingnextNodePtr
to point to the new node, and then updating the new node'snextNodePtr
to point to the previous next node.The
GetNext
member function returns a pointer to the next node in the list.The
PrintNodeData
member function prints the integer value of the node to the console.In the
main
function, a linked list is created using severalIntNode
objects. TheheadObj
object is the first node in the list and is initialized with a data value of -1. Additional nodes are inserted after theheadObj
object using theInsertAfter
function.Finally, the linked list is printed to the console using a loop that iterates through each node in the list and prints its value using the
PrintNodeData
function.
2024 - Programming 3 / Data Structures - Author: Dr. Kevin Roark