fork download
#include<bits/stdc++.h>
 
using namespace std;
class node {
public:
    int data;
    node* next;
};
 
class linked {
public:
    node* head;
 
    linked() {
        head = NULL;
    }
 
    bool impty() {
        return (head == NULL);
    }
 
    bool found(int value) {
        bool f = false;
        node* temp = head;
        while (temp != NULL) {
            if (temp->data == value) {
                f = true;
                break;
            }
            temp = temp->next;
        }
        return f;
    }
 
    void inserf(int v2) {
        node* newnode = new node();
        newnode->data = v2;
        newnode->next = head;
        head = newnode;
    }
 
    void append(int v3) {
        node* newnode = new node();
        newnode->data = v3;
        newnode->next = NULL;
        if (impty()) {
            head = newnode;
        } else {
            node* temp = head;
            while (temp->next != NULL) {
                temp = temp->next;
            }
            temp->next = newnode;
        }
    }
 
    void disp() {
        if (impty()) {
            cout << "No elements\n";
        } else {
            node* temp = head;
            while (temp != NULL) {
                cout << temp->data << " ";
                temp = temp->next;
            }
            cout << endl;
        }
    }
 
    void insertAt(int pos, int value) {
        node* newNode = new node();
        newNode->data = value;
        newNode->next = NULL;
 
        if (pos == 1) {
            newNode->next = head;
            head = newNode;
            cout << "Element inserted successfully at position 1\n";
            return;
        }
 
        node* temp = head;
        for (int i = 1; i < pos - 1 && temp != NULL; ++i) {
            temp = temp->next;
        }
 
        if (temp == NULL) {
            cout << "Position out of range\n";
            return;
        }
 
        newNode->next = temp->next;
        temp->next = newNode;
        cout << "Element inserted successfully at position " << pos << endl;
    }
 
    bool search(int value) {
        node* temp = head;
        while (temp != NULL) {
            if (temp->data == value) {
                return true;
            }
            temp = temp->next;
        }
        return false;
    }
 
    void deleteElement(int value) {
        node* temp = head;
        node* prev = NULL;
 
        if (temp != NULL && temp->data == value) {
            head = temp->next;
            delete temp;
            return;
        }
 
        while (temp != NULL && temp->data != value) {
            prev = temp;
            temp = temp->next;
        }
 
        if (temp == NULL) {
            cout << "Element not found\n";
            return;
        }
 
        prev->next = temp->next;
        delete temp;
    }
};
 
int main() {
    linked link;
    int n, data;
 
    cout << "Enter the number of elements to append: ";
    cin >> n;
    cout << "Enter " << n << " elements to append:\n";
    for (int i = 0; i < n; ++i) {
        cin >> data;
        link.append(data);
    }
 
    link.disp();
 
    int pos, value;
 
    cout << "Enter position and value to insert: ";
    cin >> pos >> value;
    link.insertAt(pos, value);
    link.disp();
 
    int searchValue;
    cout << "Enter value to search: ";
    cin >> searchValue;
    if (link.search(searchValue)) {
        cout << searchValue << " found in the list\n";
    } else {
        cout << searchValue << " not found in the list\n";
    }
 
    int dt;
    cout << "Enter value to delete: ";
    cin >> dt;
    link.deleteElement(dt);
    link.disp();
 
    return 0;
}
Success #stdin #stdout 0.01s 5284KB
stdin
3
1
2
3
2
9
3
2
stdout
Enter the number of elements to append: Enter 3 elements to append:
1 2 3 
Enter position and value to insert: Element inserted successfully at position 2
1 9 2 3 
Enter value to search: 3 found in the list
Enter value to delete: 1 9 3