Files
  • main.cpp
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Header files
#include <iostream>

struct node
{
    int data;
    struct node *next;
};

// Head pointer always points to first element of the linked list
struct node *head = NULL;

/* Printing data in each node */

// Display the list
void printList()
{
    struct node *ptr = head;

    // Start from the beginning
    while(ptr != NULL)
    {
        std::cout << ptr->data << " ";
        ptr = ptr->next;
    }

    std::cout << std::endl;
}

/* Insertion at the beginning */

// Insert link at the beginning
void insertFirst(int data)
{
    // Create a new node
    struct node *new_node = new struct node;

    new_node->data = data;

    // Point it to old head
    new_node->next = head;

    // Point head to new node
    head = new_node;

    std::cout << "Inserted successfully" << std::endl;
}

/* Deletion at the beginning */

// Delete first item
void deleteFirst()
{
    // Save reference to head
    struct node *temp = head;

    // Point head to head's next
    head = head->next;

    // Free memory used by temp
    delete temp;

    std::cout << "Deleted successfully" << std::endl;
}

/* Size */

// Find no. of nodes in link list
void size()
{
    int length = 0;
    struct node *current;

    for(current = head; current != NULL; current = current->next)
    {
        length++;
    }

    std::cout << "Size of Linked List is " << length << std::endl;
}

/* Searching */

// Find node with given data
void find(int data){

    // Start from the head
    struct node* current = head;

    // If list is empty
    if(head == NULL)
    {
        std::cout << "List is empty" << std::endl ;
        return;
    }

    // Traverse through list
    while(current->data != data){

        // If it is last node
        if(current->next == NULL){
            std::cout << "Not Found" << std::endl;
            return;
        }
        else{
            // Go to next node
            current = current->next;
        }
    }

    // If data found
    std::cout << "Found" << std::endl;
}

/* Deletion after a node */

// Delete a node with given data
void del(int data){

    // Start from the first node
    struct node* current = head;
    struct node* previous = NULL;

    // If list is empty
    if(head == NULL){
        std::cout << "List is empty" << std::endl;
        return ;
    }

    // Navigate through list
    while(current->data != data){

        // If it is last node
        if(current->next == NULL){
            std::cout << "Element not found" << std::endl;
            return ;
        }
        else {
            // Store reference to current node
            previous = current;
            // Move to next node
            current = current->next;
        }

    }

    // Found a match, update the node
    if(current == head) {
        // Change head to point to next node
        head = head->next;
    }
    else {
        // Skip the current node
        previous->next = current->next;
    }

    // Free space used by deleted node
    delete current;
    std::cout << "Deleted succesfully" << std::endl;
}


int main() {
    insertFirst(10); // prints Inserted successfully
    insertFirst(20); // prints Inserted successfully
    insertFirst(30); // prints Inserted successfully
    insertFirst(1); // prints Inserted successfully
    insertFirst(40); // prints Inserted successfully
    insertFirst(56); // prints Inserted successfully

    // print list
    printList(); // prints 56 40 1 30 20 10

    deleteFirst(); // prints Deleted successfully

    printList(); // prints 40 1 30 20 10

    find(4); // prints Not Found
    find(1); // prints Found

    del(4); // prints Element not found
    del(1); // prints Deleted succesfully

    printList(); // prints 40 30 20 10

    return 0;
}