0
일부 함수를 사용하여 deque를 구현하려고합니다 : front(), back(), push_front(), push_back(), pop_front(), pop_back(). 대기열에 요소가 하나 있는데이 요소를 팝업하려고하면 인쇄 기능에서 "읽기 액세스 위반"이 발생하지만 첫 번째 요소가 deque에 있는지 확인합니다.큐 앞뒤로 deque
#include<iostream>
#include<cstdlib>
using namespace std;
struct Nod {
int info;
Nod* next, *back;
};
void create_queue(Nod*& p, Nod*& u)
{
Nod *c = new Nod;
cout << "c->info: "; cin >> c->info;
if (!p)
{
p = c;
p->back = NULL;
p->next = NULL;
u = p;
}
else
{
u->next = c;
c->back = u;
u = c;
u->next = NULL;
}
}
void print_queue(Nod* p, Nod* u)
{
if (p) {
Nod *c = p;
while (c) {
cout << c->info << " ";
c = c->next;
}
}
else
cout << "Deque is empty";
}
int front(Nod *p) {
return p->info;
}
int back(Nod *u) {
return u->info;
}
void push_front(Nod*& p, Nod*& u) {
Nod *c = new Nod;
cout << "Push front c->info "; cin >> c->info;
c->next = p;
p->back = c;
c->back = NULL;
p = c;
}
void push_back(Nod*& p, Nod*& u) {
Nod *c = new Nod;
cout << "Push back c->info "; cin >> c->info;
c->back = u;
u->next = c;
u = c;
u->next = NULL;
}
void pop_front(Nod*& p, Nod*& u) {
if (p) {
Nod *c = p;
if (p->next != NULL) {
p->next->back = NULL;
p = p->next;
}
delete c;
}
else
{
cout << "Can't pop, deque is empty";
}
}
void pop_back(Nod*& p, Nod*& u) {
if (u){
Nod *c = u;
if (u->back != NULL) {
u->back->next = NULL;
u = u->back;
}
delete c;
}
else
{
cout << "Can't pop, deque is empty";
}
}
int main()
{
int n, i = 1;
Nod *p, *u = new Nod;
p = NULL;
u = NULL;
cout << "Nr nod: "; cin >> n;
while (i <= n){
create_queue(p, u);
i++;
}
pop_front(p, u); //problems if there is only one element in deque
print_queue(p, u);
system("Pause");
}