Search
 
SCRIPT & CODE EXAMPLE
 

CPP

linked list with classes c++

// C++ program for the above approach
#include <iostream>
using namespace std;
  
// Node class to represent
// a node of the linked list.
class Node {
public:
    int data;
    Node* next;
  
    // Default constructor
    Node()
    {
        data = 0;
        next = NULL;
    }
  
    // Parameterised Constructor
    Node(int data)
    {
        this->data = data;
        this->next = NULL;
    }
};
  
// Linked list class to
// implement a linked list.
class Linkedlist {
    Node* head;
  
public:
    // Default constructor
    Linkedlist() { head = NULL; }
  
    // Function to insert a
    // node at the end of the
    // linked list.
    void insertNode(int);
  
    // Function to print the
    // linked list.
    void printList();
  
    // Function to delete the
    // node at given position
    void deleteNode(int);
};
  
// Function to delete the
// node at given position
void Linkedlist::deleteNode(int nodeOffset)
{
    Node *temp1 = head, *temp2 = NULL;
    int ListLen = 0;
  
    if (head == NULL) {
        cout << "List empty." << endl;
        return;
    }
  
    // Find length of the linked-list.
    while (temp1 != NULL) {
        temp1 = temp1->next;
        ListLen++;
    }
  
    // Check if the position to be
    // deleted is less than the length
    // of the linked list.
    if (ListLen < nodeOffset) {
        cout << "Index out of range"
             << endl;
        return;
    }
  
    // Declare temp1
    temp1 = head;
  
    // Deleting the head.
    if (nodeOffset == 1) {
  
        // Update head
        head = head->next;
        delete temp1;
        return;
    }
  
    // Traverse the list to
    // find the node to be deleted.
    while (nodeOffset-- > 1) {
  
        // Update temp2
        temp2 = temp1;
  
        // Update temp1
        temp1 = temp1->next;
    }
  
    // Change the next pointer
    // of the previous node.
    temp2->next = temp1->next;
  
    // Delete the node
    delete temp1;
}
  
// Function to insert a new node.
void Linkedlist::insertNode(int data)
{
    // Create the new Node.
    Node* newNode = new Node(data);
  
    // Assign to head
    if (head == NULL) {
        head = newNode;
        return;
    }
  
    // Traverse till end of list
    Node* temp = head;
    while (temp->next != NULL) {
  
        // Update temp
        temp = temp->next;
    }
  
    // Insert at the last.
    temp->next = newNode;
}
  
// Function to print the
// nodes of the linked list.
void Linkedlist::printList()
{
    Node* temp = head;
  
    // Check for empty list.
    if (head == NULL) {
        cout << "List empty" << endl;
        return;
    }
  
    // Traverse the list.
    while (temp != NULL) {
        cout << temp->data << " ";
        temp = temp->next;
    }
}
  
// Driver Code
int main()
{
    Linkedlist list;
  
    // Inserting nodes
    list.insertNode(1);
    list.insertNode(2);
    list.insertNode(3);
    list.insertNode(4);
  
    cout << "Elements of the list are: ";
  
    // Print the list
    list.printList();
    cout << endl;
  
    // Delete node at position 2.
    list.deleteNode(2);
  
    cout << "Elements of the list are: ";
    list.printList();
    cout << endl;
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: power function in O(log(n)) time c++ 
Cpp :: reverse sort cpp 
Cpp :: how to sort in descending order c++ 
Cpp :: c++ convert binary string to decimal 
Cpp :: double max value c++ 
Cpp :: angle to vector2 
Cpp :: ue4 get bone location c++ 
Cpp :: c++ visual studio 19 how to make colord button from code 
Cpp :: hide terminal window c++ 
Cpp :: c++ get input without loop 
Cpp :: ue4 find component c++ 
Cpp :: what are various sections of process 
Cpp :: ue4 ftext to int 
Cpp :: penjanje 
Cpp :: c++ nodiscard 
Cpp :: custom comparator in set of struct 
Cpp :: sort in descending order c++ stl 
Cpp :: c++ find key in hashmap 
Cpp :: c++ std::fmin 
Cpp :: c++ vector add only unique elements 
Cpp :: point is on line 
Cpp :: how to initialize 2d vector in c++ 
Cpp :: 2d array using vector 
Cpp :: how to find size of int array in c++ 
Cpp :: bash test empty directory 
Cpp :: delete last char of string c++ 
Cpp :: string to number in c++ 
Cpp :: how to use decrement operator in c++ 
Cpp :: latex table landscape 
Cpp :: max of a vector c++ 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =