OneCompiler

LL

1626

#include<bits/stdc++.h>
using namespace std;

struct Node*{
int data;
Node* link;
};

class LinkedList{
Node* head;
public:
LinkedList(){
head = NULL;
}

void create();
void addfront(int n);
int deletefront();
int deletespec();
void display();
};

void LinkedList ::create(){
int n;
cin>>n;
for(int i = 0; i < n; i++){
Node* nn = new Node;
cin>>nn->data;
nn->link = NULL;

if(i == 0){
  head = nn;
  continue;
}

Node* temp = head;
while(temp->link != NULL){
  temp = temp -> link;
}
temp->link = nn;
temp = nn;

}
}

void LinkedList :: addfront(int n){
Node* nn = new Node;
nn->data = n;

if(nn == NULL){
  return;
}

nn->link = head;
head = nn;

}

int LinkedList :: deletefront(){
Node* temp = head;
int data = temp->data;
head = head->link;
delete temp;
return data;
}

int LinkedList :: deletespec(int key){
Node* temp = head;

if(head->data = key){
return deletefront();
}
else{
while(temp!= NULL && temp->data != key){
prev = temp;
temp = temp->link;
}

if(temp == NULL){
  cout<<"not found";
  return -1;
}

int data = temp->data;
prev->link = temp->link;
delete temp;
return data;

}
}