//linked list implementation using class in c++.
#include <bits/stdc++.h>
using namespace std;
class node{
friend class linked;
int data;
public:
node *next;
node(){
data=0;
next=NULL;
}
};
class linked{
public:
node *start=NULL;
node *temp=NULL;
node *head;
void insert(int item);
void print();
};
void linked::insert(int data){
head = new node();
head->data=data;
if(start == NULL){
start=head;
temp=head;
start->next=NULL;
}
else{
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=head;
head->next=NULL;
}
}
void linked::print(){
node *head=start;
while(head){
cout<<head->data<<endl;
head=head->next;
}
}
int main() {
linked list;
for(int i=1;i<11;i++)
list.insert(i);
list.print();
return 0;
}
#include <bits/stdc++.h>
using namespace std;
class node{
friend class linked;
int data;
public:
node *next;
node(){
data=0;
next=NULL;
}
};
class linked{
public:
node *start=NULL;
node *temp=NULL;
node *head;
void insert(int item);
void print();
};
void linked::insert(int data){
head = new node();
head->data=data;
if(start == NULL){
start=head;
temp=head;
start->next=NULL;
}
else{
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=head;
head->next=NULL;
}
}
void linked::print(){
node *head=start;
while(head){
cout<<head->data<<endl;
head=head->next;
}
}
int main() {
linked list;
for(int i=1;i<11;i++)
list.insert(i);
list.print();
return 0;
}
No comments:
Post a Comment