-
-
Save skhozinova/c5ba562a8b07feb7157f2d97c0541c14 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<conio.h> | |
#include<stdlib.h> | |
#include<locale.h> | |
//Создать список из целых чисел. Подсчитать количество отрицательных элементов, создав из них новый список. | |
struct tqueue //структура очереди | |
{ | |
int inf; | |
tqueue *next; | |
}; | |
tqueue *head,*tail,*nhead,*ntail;//указатели на первые и последние элементы очередей | |
void init_queue(tqueue *&h, tqueue *&t)//инициализация очереди | |
{ | |
h=t=NULL; | |
} | |
void insert(tqueue *&h,tqueue *&t, int item)//добавление элемента в очередь | |
{ | |
tqueue *r=new tqueue; | |
r->inf=item; | |
r->next=NULL; | |
if (!h&&!t) | |
{ | |
h=t=r; | |
} | |
else | |
{ | |
t->next=r; | |
t=r; | |
} | |
} | |
int take(tqueue *&h, tqueue *&t)//удаление элемента из очереди | |
{ | |
tqueue *y=h; | |
int j=y->inf; | |
h=y->next; | |
if (!h) | |
t=NULL; | |
delete y; | |
return j; | |
} | |
int number_of_negative_item(tqueue *&h, tqueue *&t,tqueue *nhead, tqueue *ntail,int num) | |
{ | |
tqueue *r=h; | |
int i=r->inf; | |
if(i<0) | |
{ | |
insert(nhead,ntail,i); | |
printf("%d\t",take(nhead,ntail)); | |
num++; | |
} | |
h=r->next; | |
if (!h) | |
t=NULL; | |
delete r; | |
return num; | |
} | |
int main(void) | |
{ | |
int i,num=0; | |
setlocale(LC_ALL,"RUS"); | |
init_queue(head,tail); | |
init_queue(nhead,ntail); | |
FILE *fp; | |
fp=fopen("in.txt","r"); | |
while(!feof(fp)) | |
{ | |
fscanf(fp,"%d",&i); | |
insert(head,tail,i); | |
} | |
fclose(fp); | |
printf("Новый список:\n"); | |
while(head) | |
{ | |
num=number_of_negative_item(head,tail,nhead,ntail,num); | |
} | |
printf("\nКоличество отрицательных элементов: %d\n",num); | |
system("pause"); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-9 | |
-10 | |
-33 | |
-89 | |
-12 | |
-7 | |
-2 | |
-45 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment