Добавил:
okley
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:3 семестр / lab19
.c#include <stdio.h>
#include <stdlib.h>
typedef struct el{ // element ocheredi
int inf;
struct el *link;
} EL;
typedef struct q{ // nach. i kon. ukazateli ocheredi
EL *beg;
EL *end;
} Q;
void push(Q *q, int val) {
EL *p;
p = (EL*) malloc(sizeof(EL));
p->inf = val;
p->link = NULL;
if (q->end == NULL) {
q->beg = p;
q->end = p;
}
else {
q->end->link = p;
q->end = p;
}
}
void pop(Q *q) {
EL *p;
p = (EL*) malloc(sizeof(EL));
p->link = NULL;
if (q->beg == NULL) { // popitka udaleniya iz pustoi ocheredi
exit(1);
}
else if((q->beg)==(q->end)){
q->beg=NULL;
q->end=NULL;
}
else {
q->beg=q->beg->link;
}
}
void output(Q *q){
EL *p;
if((q->beg)!=NULL){
p=q->beg;
while((p->link)!=NULL){
printf("%4d", p->inf);
p=p->link;
}
printf("%4d\n", p->inf);
}
}
int summ(Q *q){ // summa
int s=0;
EL *p;
if((q->beg)!=NULL){
p=q->beg;
while((p->link)!=NULL){
s+=(p->inf);
p=p->link;
}
s+=(p->inf);
}
return(s);
}
int main(void){
Q *queue;
int a, b;
queue = (Q*) malloc(sizeof(Q));
queue->beg == NULL;
queue->end = NULL;
a=scanf("%d", &b); // boolean: esli chislo, daet true, elsi bukva (v tom chisle "n" - to false)
push(queue, b);
while(a){ // zapolnenie
a=scanf("%d", &b);
if(a) push(queue, b);
}
pop(queue);
pop(queue);
output(queue);
printf("%d\n", summ(queue));
}
