C++ PROGRAME USING LINKED LIST
STORE DATA SEE AND ADRESS
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int data) {
this->data = data;
this->next =NULL ;
}
};
int main() {
Node* node1 = new Node(10);
cout << node1->data << endl;
cout << node1->next << endl;
return 0;
}
INSERTHEAD HEAD
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int data) {
this->data = data;
this->next = nullptr;
}
};
void insertathead(Node* &head, int d) {
Node* temp = new Node(d);
temp->next = head;
head = temp;
}
void print(Node* head) {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " "; // Use cout, not count
temp = temp->next;
}
cout << endl; // Use cout, not count
}
int main() {
Node* node1 = new Node(10);
//cout << node1->data << endl;
//cout << node1->next << endl; // It will print 0 or some addres
Node* head = node1; // Initialize head with node1
insertathead(head, 20);
insertathead(head, 30);
print(head); // Now print the linked list
return 0;
}
Comments
Post a Comment