#include <stdio.h> #include <math.h> #include <stdlib.h> /*Создать очередь, введя элемент 3.8. Заполнение очереди произвадить 7 числами * А[i]=sqrt(i). После окончания ввода в окне вывести структуру очереди. * */ struct X{ float Value; X *p; }; X*FirstElement(float A) { X *FirstX; FirstX=(X*)malloc(sizeof(X)); FirstX->Value=A; FirstX->p=0; return FirstX; } void In(X**EndX,float A) { //заносит последующие элементы в конец очереди X*ElementX; ElementX=(X*)malloc(sizeof(X)); ElementX->Value=sqrt(A); ElementX->p=0; (*EndX)->p=ElementX; //указатель на конец очереди *EndX=ElementX; } double Out(X**BeginX) { int n=(*BeginX)->Value; X*ElementX=*BeginX; *BeginX=(*BeginX)->p; free(ElementX); return n; } int main() { X*first=FirstElement(3.8); X*end=first; //Тут не могу сделать. нужна помощь. for(int i=1;i<7;i++) In(&end,sqrt(3.8)); while(first) printf("%lf\n",Out(&first)); return 0; } 
  • Please add some more information. What specifically fails and where an error occurs or does not work correctly. - Denis Bubnov
  • If a question with a tag c ++, then it is necessary to implement normally, and not in the same way - int3
  • Specifically, it is impossible to implement the output of subsequent elements. I can not understand how to properly organize the output structure of the queue. So rounding does not work correctly. It is necessary to output up to 3 characters, and it displays in the format: 1.0000000. - zoohan41
  • printf ("%. 2f \ n", 123.456); for rounding - Alexsandr Ter
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

 #include <iostream> #include <stdlib.h> using namespace std; struct qitem { struct qitem *next, *prev; int data; }; struct queue { struct qitem *first, *last; }; struct qitem *add_head (struct queue *q, struct qitem *e) { if (e) { e->prev = 0; if (e->next = q->first) q->first->prev = e; else q->last = e; q->first = e; } return e; } struct qitem *add_tail (struct queue *q, struct qitem *e) { if (e) { e->next = 0; if (e->prev = q->last) q->last->next = e; else q->first = e; q->last = e; } return e; } struct qitem *remove (struct queue *q, struct qitem *e) { if (e) { if (e->next) e->next->prev = e->prev; else q->last = e->prev; if (e->prev) e->prev->next = e->next; else q->first = e->next; } return e; } struct qitem *find_max (struct queue *q) { struct qitem *cur = q->first, *pmax = cur; if (cur) { while (cur) { if (cur->data > pmax->data) pmax = cur; cur = cur->next; } } return pmax; } struct qitem *make_item (int n) { struct qitem *p = new struct qitem; p->data = n; return p; } void print_list (struct queue *q) { cout << "queue " << q->first << " : " << q->last << '\n'; for (struct qitem *cur = q->first; cur; cur = cur->next) cout << cur->data << " [ " << cur <<": next " << cur->next << " prev " << cur->prev <<"]\n"; } struct queue randlist (int n) { struct queue my_q = {0, 0}; for (int i = 0; i < n; i++) add_tail(&my_q, make_item((rand() % 100) + 1)); return my_q; } int main() { int N; cout << "Enter N: "; cin >> N; struct queue my_q = randlist(N); print_list(&my_q); cout << "---- find max and move it to head ----\n"; add_head(&my_q, remove(&my_q, find_max(&my_q))); print_list(&my_q); cout << "---- free memory (remove all queue items and delete them) ----\n"; struct qitem *p; int n = 0; while (p = remove(&my_q, my_q.first)) { delete p; n++; } cout << "remove " << n << " items\n"; print_list(&my_q); if (n == N) cout << "OK!\n"; else cout << "FAIL...\n"; return N != n; }