//This program implements queue using Linked List
#include<bits/stdc++.h>
using namespace std;
struct Node{
int data;
struct Node* next;
};
struct Node* front=NULL;
struct Node* rear=NULL;
void Enqueue(int x){
struct Node* new_node=new Node();
new_node->data=x;
new_node->next=NULL;
if(front==NULL && rear==NULL){
front=rear=new_node;
return;
}
rear->next=new_node;
rear=new_node;
}
void Dequeue(){
struct Node* temp=front;
if(front==rear){
front=rear=NULL;
return;
}
else{
front=front->next;
}
delete temp;
}
void display(){
struct Node* current=front;
while(current!=NULL){
cout<<current->data<<" ";
current=current->next;
}
}
int main(){
cout<<"Enqueuing......"<<endl;
Enqueue(5);
Enqueue(4);
Enqueue(1);
Enqueue(8);
display();
cout<<"\nDequeuing......"<<endl;
Dequeue();
display();
return 0;
}
В void Dequeue () используется delete temp, но я не выделил Node * temp в памяти кучи, но тем не менее он удаляет временный Node. Но удаление можно использовать только тогда, когда что-то выделено в куче. PS: Код работает нормально, я просто хочу знать, можно ли использовать delete для удаления в stati c выделение памяти.