#include
using namespace std;
struct node {
int data;
node* next;
};
void insertAtBegining();
void insertAtAPosition();
void insertAtLast();
void displayList();
void remove();
struct node *head;
int main(){
int choice;
head=NULL; // list empty
while (1) {
cout<<"-------------------------"<>choice;
switch (choice) {
case 1:
insertAtBegining();
break;
case 2:
displayList();
break;
case 3:
insertAtAPosition();
break;
case 4:
exit(0);
case 5:
insertAtLast();
break;
default:
break;
}
}
return 0;
}
void insertAtBegining(){
int i, newdata=0;
cout<<"How many datas you want to insert: \n";
cin>>i;
for (int k=0; k>newdata;
node* temp= new node;
temp->data=newdata;
if (head==NULL) {
temp->next=NULL;
head=temp;
}
else {
temp->next=head;
head=temp;
}
}
}
void insertAtLast(){
int i, newdata=0;
cout<<"How many datas you want to insert at last:\n";
cin>>i;
for (int k=0; k>newdata;
node* temp=new node;
node* ptr=head;
temp->data=newdata;
while (head!=NULL) {
ptr=ptr->next;
if ( ptr->next==NULL ) {
ptr->next=temp;
break;
}
}
}
}
void insertAtAPosition(){
int pos, newdata;
cout<<"Enter position you want to insert your data:\n ";
cin>>pos;
cout<<"Enter data:\n";
cin>>newdata;
node* temp = new node;
temp-> data = newdata;
temp->next=NULL;
if (pos==1) {
temp->next=head;
head=temp;
return;
}
node* ptr=head;
for (int i=0; inext;
}
temp->next=ptr->next;
ptr->next=temp;
}
void displayList(){
node *temp=head;
cout<<"[ head ] =>";
while (temp!=NULL) {
cout<<" [ "<data<<"]=>";
temp=temp->next;
}
cout<<"\n";
}