Linked List class 2: Take Inputs
// Online C++ compiler to run C++ program online
#include <iostream>
using namespace std;
struct node{
public:
int data;
node* next;
node(int data){
this->data=data;
next=NULL;
}
};
void display(node* head){
node* temp=head;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
}
node* input(){
int data;
cin>>data;
node* head=NULL;
while(data!=-1){
node*n=new node(data);
if (head==NULL){
head=n;
}
else{
node* temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=n;
}
cin>>data;
}
return head;
}
int main() {
node*head=input();
display(head);
return 0;
}
Comments
Post a Comment