ТОР 5 статей: Методические подходы к анализу финансового состояния предприятия Проблема периодизации русской литературы ХХ века. Краткая характеристика второй половины ХХ века Характеристика шлифовальных кругов и ее маркировка Служебные части речи. Предлог. Союз. Частицы КАТЕГОРИИ:
|
Найти наибольшее из значений компонент.#include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <io.h> #include <stdio.h> #include <stdlib.h>
struct list { int info; list *prev; list *next; list() { prev=NULL; next=0; info=0; } };
void insert_post(list *, int); void printList(list *); int findMax(list *);
list *first=0; list *last=0;
char buffer[100];
void main(void) { int fh; unsigned int i, nbytes = 100, bytesread;
char digit[10]; list *p;
/* Open file for input: */ if((fh = _open("file.dat", _O_RDONLY)) == -1) { perror("open failed on input file"); exit(1); } /* Read in input: */ if((bytesread = _read(fh, buffer, nbytes)) <= 0) { perror("Problem reading file"); exit(1); } for(i=0; i<bytesread; i++) { if(buffer[i]==0x0a) continue; p=last; sprintf(digit, "%c", buffer[i]); insert_post(p, atoi(digit)); } printList(first); printf("The max item in the list is %d\n", findMax(first)); _close(fh); }
/* Tставка элемента после текуего */ void insert_post(list *q, int value) { list *p = new list; if (q!= 0) { p->next = q->next; p->prev = q; } p->info = value; if (q == 0) { first = p; last = p; } else { if (q->next == 0) last = p; else q->next->prev = p; q->next = p; } }
/* find max item in the list */ int findMax(list *q) { int max=-9999; while(q!=NULL) { if(q->info > max) max = q->info; q = q->next; }
return max; }
/* Print the list */ void printList(list *q) { if (q == 0) printf("List is empty.\n\n"); else { printf("The list is:\n");
while (q!= 0) { printf("%d-> ", q->info); q = q->next; } printf("NULL\n\n"); } }
Не нашли, что искали? Воспользуйтесь поиском:
|