class array
{
char arr[100];
int max, current;
int is_empty();
public:
array(){max=100; current=0;}
array(int size);
~array(){};
int add(char sym);
char get();
};
array::array(int size)
{
if (size>100 || size<=0) max=100;
else max=size;
current=0;
}
int array::is_empty()
{
if (current==0) return 1;
else return 0;
}
int array::add(char sym)
{
if (current==max)
{
cout<<"overflow"<<endl;
return 1;
}
else
{
arr[current]=sym;
current++;
return 0;
}
}
char array::get()
{
if (is_empty())
{
cout<<"nothin to get"<<endl;
return 1;
}
else
{
current--;
return arr[current];
}
}
Классы С++ (массив с проверкой на пустоту)
Элементы вводятся с клавиатуры. При попытке извлечения пустого элемента, вывести сообщение об ошибке, но продолжить выполнение программы.
Я знаю что элементы можно добавлять и извлекать. Так же знаю что нужно количество элементов.
Помогите реализовать.
Цитата:
Слева в массив входят элементы, справа выходят.
а где у массивов лево и параво?
Цитата:
Помогите реализовать.
что уже есть? с какими трудностями столкнулся?
очередь" (FIFO)
Массив типа "
Код:
#include <iostream>
using namespace std;
template <class T> class myqueue
{
private:
struct record
{
record(T& element): val(element){next=NULL;};
record(){next=NULL;};
T val;
record * next;
};
record * first;
record * last;
public:
myqueue()
{
first=NULL;
last=NULL;
};
bool isEmpty()//проверка на пустоту
{
return !first;
};
void push(T element)//добавление в конец списка
{
record * newelement=new record(element);
if(!last)
{
first=newelement;
last=newelement;
}
else
{
last->next=newelement;
last=newelement;
}
};
T pop()//выборка из начала списка
{
T ret=T();
if(first)
{
ret=first->val;
record* newfirst=first->next;
delete first;
first=newfirst;
}
return ret;
};
};
int main()
{
myqueue<int> a;
int b=5;
a.push(b);
a.push(24);
a.push(36);
cout<<a.pop()<<endl;//5
cout<<a.pop()<<endl;//24
cout<<a.pop()<<endl;//36
cout<<a.pop()<<endl;//0 (елементы закончились, выводим новый елемент с конструктором по умолчанию)
cout<<a.isEmpty()<<endl;//1 (1-пустой, 0-непустой)
myqueue<char> c;
char d='A';
c.push(d);
c.push('B');
c.push('C');
cout<<c.pop()<<endl;//A
cout<<c.pop()<<endl;//B
cout<<c.pop()<<endl;//C
cout<<c.pop()<<endl;// (елементы закончились, выводим новый елемент с конструктором по умолчанию)
cout<<c.isEmpty()<<endl;//1 (1-пустой, 0-непустой)
return 0;
}
using namespace std;
template <class T> class myqueue
{
private:
struct record
{
record(T& element): val(element){next=NULL;};
record(){next=NULL;};
T val;
record * next;
};
record * first;
record * last;
public:
myqueue()
{
first=NULL;
last=NULL;
};
bool isEmpty()//проверка на пустоту
{
return !first;
};
void push(T element)//добавление в конец списка
{
record * newelement=new record(element);
if(!last)
{
first=newelement;
last=newelement;
}
else
{
last->next=newelement;
last=newelement;
}
};
T pop()//выборка из начала списка
{
T ret=T();
if(first)
{
ret=first->val;
record* newfirst=first->next;
delete first;
first=newfirst;
}
return ret;
};
};
int main()
{
myqueue<int> a;
int b=5;
a.push(b);
a.push(24);
a.push(36);
cout<<a.pop()<<endl;//5
cout<<a.pop()<<endl;//24
cout<<a.pop()<<endl;//36
cout<<a.pop()<<endl;//0 (елементы закончились, выводим новый елемент с конструктором по умолчанию)
cout<<a.isEmpty()<<endl;//1 (1-пустой, 0-непустой)
myqueue<char> c;
char d='A';
c.push(d);
c.push('B');
c.push('C');
cout<<c.pop()<<endl;//A
cout<<c.pop()<<endl;//B
cout<<c.pop()<<endl;//C
cout<<c.pop()<<endl;// (елементы закончились, выводим новый елемент с конструктором по умолчанию)
cout<<c.isEmpty()<<endl;//1 (1-пустой, 0-непустой)
return 0;
}
а все на указателях построил. может, тогда очередь в массиве?