Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<iomanip>
- using namespace std;
- struct queue
- {
- int D;
- queue* next;
- queue* prev;
- };
- void push(queue** top, queue** top_first_in_first_out, int element)
- {
- queue* q = new queue();
- q->D = element;
- q->next = NULL;
- q->prev=NULL;
- if (*top_first_in_first_out == NULL)
- {
- *top = *top_first_in_first_out = q;
- }
- else
- {
- (*top)->next = q;
- q->prev=(*top);
- *top = q;
- }
- }
- void print(queue* top_first_in_first_out)
- {
- queue* q = top_first_in_first_out;
- while (q != NULL)
- {
- cout << setw(10) << q->D;
- q = q->next;
- }
- cout << endl;
- }
- void insert_99(queue* top)
- {
- //проверка если очередь пуста
- if(top==NULL){
- cout<<"queue is empty!";
- exit(1);
- }
- //проверка! если у нас в очереди один улемент то заканчиваем программу//так как некорруктный ввод данных для вставки
- if(top->next==NULL && top->prev==NULL){
- cout<<"в очереди один улемент!!!";
- exit(1);
- }
- // -4 -5 -6 8 4 -5 -6
- // 99
- while (top->next != NULL)
- {
- if(top->D<0 && top->next->D<0){
- queue *q=new queue();
- q->D=99;
- q->next=top->next;
- q->prev=top;
- top->next->prev=q;
- top->next=q;
- top=top->next;
- }
- top=top->next;
- }
- }
- void delete_queue(queue* top_first_in_first_out)
- {
- queue* q ;
- while (top_first_in_first_out != NULL)
- {
- q = top_first_in_first_out;
- top_first_in_first_out = top_first_in_first_out->next;
- q->next = NULL;
- delete q;
- }
- }
- int main()
- {
- queue* top = NULL;
- queue* top_first_in_first_out = NULL;
- int n;
- cout << "Write number of elements: " << endl;
- cin >> n;
- int element=0;
- for(int i=0;i<n;i++){
- cin>>element;
- push(&top, &top_first_in_first_out, element);
- }
- cout << endl<<endl<< "Old queue:" << endl;
- print(top_first_in_first_out);
- insert_99(top_first_in_first_out);
- cout << endl << "New queue" << endl;
- print(top_first_in_first_out);
- delete_queue(top_first_in_first_out);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement