Linked List Sample Program
Sample Program 1
#include <iostream>
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
class LinkedList {
private:
Node* head;
public:
LinkedList() : head(nullptr) {}
// 1. Append a node to the end of the list
void append(int value) {
Node* newNode = new Node(value);
if (!head) {
head = newNode;
}
else {
Node* temp = head;
while (temp->next) {
temp = temp->next;
}
temp->next = newNode;
}
std::cout << "Appended: " << value << std::endl;
}
// 2. Traverse the list and print all elements
void traverse() {
if (!head) {
std::cout << "List is empty!" << std::endl;
return;
}
Node* temp = head;
std::cout << "List contents: ";
while (temp) {
std::cout << temp->data << " ";
temp = temp->next;
}
std::cout << std::endl;
}
// 3. Insert a node at a specific position
void insert(int value, int position) {
Node* newNode = new Node(value);
if (position == 0) { // Insert at the head
newNode->next = head;
head = newNode;
}
else {
Node* temp = head;
for (int i = 0; temp && i < position - 1; ++i) {
temp = temp->next;
}
if (!temp) {
std::cout << "Position out of bounds. Insert failed." << std::endl;
delete newNode;
return;
}
newNode->next = temp->next;
temp->next = newNode;
}
std::cout << "Inserted: " << value << " at position: " << position << std::endl;
}
// 4. Delete a node with a specific value
void remove(int value) {
if (!head) {
std::cout << "List is empty. Nothing to delete." << std::endl;
return;
}
if (head->data == value) { // Remove head node
Node* temp = head;
head = head->next;
delete temp;
std::cout << "Deleted: " << value << std::endl;
return;
}
Node* temp = head;
while (temp->next && temp->next->data != value) {
temp = temp->next;
}
if (temp->next) {
Node* toDelete = temp->next;
temp->next = toDelete->next;
delete toDelete;
std::cout << "Deleted: " << value << std::endl;
}
else {
std::cout << "Value " << value << " not found in the list." << std::endl;
}
}
// 5. Destroy the entire list
void destroy() {
while (head) {
Node* temp = head;
head = head->next;
delete temp;
}
std::cout << "List destroyed!" << std::endl;
}
// Destructor
~LinkedList() {
destroy();
}
};
int main() {
LinkedList list;
// 1. Appending nodes
list.append(10);
list.append(20);
list.append(30);
// 2. Traversing the list
list.traverse();
// 3. Inserting nodes
list.insert(15, 1); // Insert 15 at position 1
list.insert(5, 0); // Insert 5 at position 0 (head)
list.traverse();
// 4. Deleting nodes
list.remove(20); // Delete node with value 20
list.traverse();
list.remove(100); // Try to delete a non-existent value
// 5. Destroy the list
list.destroy();
list.traverse();
return 0;
}
Sample Program 2
Linked List with template
//save this file as LinkedList.h
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
using namespace std;
// Template for the LinkedList
template <class T>
class LinkedList {
private:
// Node structure for the list
struct ListNode {
T value; // Value of the node
ListNode* next; // Pointer to the next node
};
ListNode* head; // Pointer to the first node in the list
public:
// Constructor
LinkedList() : head(nullptr) {}
// Destructor
~LinkedList() {
// Free all nodes
ListNode* current = head;
while (current != nullptr) {
ListNode* temp = current;
current = current->next;
delete temp;
}
}
// Add a new node to the end of the list
void append(T value) {
ListNode* newNode = new ListNode{ value, nullptr };
if (!head) { // If the list is empty
head = newNode;
}
else {
ListNode* current = head;
while (current->next) {
current = current->next;
}
current->next = newNode;
}
}
// Display the list's contents
void display() const {
ListNode* current = head;
while (current) {
cout << current->value << " -> ";
current = current->next;
}
cout << "nullptr" << endl;
}
};
#endif
//save this file as. sample.cpp
#include <iostream>
#include "LinkedList.h" // Include the LinkedList header
int main() {
// Create a LinkedList for integers
LinkedList<int> intList;
// Add elements to the list
intList.append(10);
intList.append(20);
intList.append(30);
// Display the list
cout << "Integer List: ";
intList.display();
// Create a LinkedList for strings
LinkedList<string> stringList;
// Add elements to the list
stringList.append("Hello");
stringList.append("World");
// Display the list
cout << "String List: ";
stringList.display();
return 0;
}