#include <iostream>
using namespace std;
struct node *head = NULL;
struct node{
int num;
struct node* next;
};
void append(int n){
struct node* p = (struct node*)malloc(sizeof(struct node));
struct node* temp = head;
p->num = n;
p->next = NULL;
if(head==NULL){
head = p;
}
else{
while(temp->next!=NULL){
temp = temp->next;
}
temp->next = p;
}
}
void del(int pos){
if(head==NULL)
cout << "The list is empty" << endl;
else if(pos==1){
struct node* temp = head;
head = head->next;
free(temp);
}
else{
struct node* curr = head;
struct node* prev = head;
for(int i=1; i<pos; i++){
prev = curr;
if(curr->next==NULL){
cout << "You have inserted wrong value for deletion" << endl;
exit(1);
}
else
curr = curr->next;
}
prev->next = curr->next;
free(curr);
}
}
void display(){
if(head == NULL)
cout << "The list is empty" << endl;
else{
struct node* temp = head;
while(temp!=NULL){
cout << temp->num << " " << endl;
temp = temp->next;
}
cout << "------***------" << endl ;
}
}
void insert(int pos, int n){
struct node* p = (struct node*)malloc(sizeof(struct node));
struct node* curr = head;
struct node* prev = head;
for(int i=1; i<pos; i++){
prev = curr;
if(curr==NULL){
cout << "You have inserted wrong position for insertion" << endl;
exit(1);
}
else
curr = curr->next;
}
p->next = curr;
p->num = n;
prev->next = p;
}
void search(int n){
struct node* temp = head;
if(head==NULL)
cout << "The list is empty" << endl;
else{
int i=1;
while(temp!=NULL){
if(temp->num==n){
cout << i << endl;
break;
}
temp=temp->next;
i++;
}
if(temp==NULL)
cout << "Element cannot be found" << endl;
}
}
int main(){
display();
append(1);
append(2);
append(3);
insert(4, 4);
display();
search(5);
}