#include #include #include struct NodeRec { int value; struct NodeRec* nextPtr; }; typedef struct NodeRec Node; struct LinkedListRec { int count; Node* headPtr; }; typedef struct LinkedListRec List; /* Make a new node which contains item. */ Node* makeNode(int item) { Node* newNodePtr = (Node*)malloc(sizeof(Node)); if (newNodePtr == NULL) { fprintf(stderr, "Out of memory"); exit(1); } else { /* initialising the node's contents */ newNodePtr->value = item; newNodePtr->nextPtr = NULL; } return newNodePtr; } /* Initialize a list to be empty. */ /*void initializeList(List* listPtr) { listPtr->headPtr = NULL; listPtr->count = 0; fprintf(stderr, "initalized\n"); return; }*/ /* Given a position, this function returns a pointer * to the node in that position in the list. */ Node* setPosition(const List* listPtr, int position) { int i; Node* nodePtr = listPtr->headPtr; if (position < 0 || position >= listPtr->count) { fprintf(stderr, "Invalid position\n"); exit(1); } else { for (i = 0; i < position; i++) { nodePtr = nodePtr->nextPtr; } } return nodePtr; } /* Insert an item at a position in the Linked List. */ void insertItem(List* listPtr, int item, int position) { Node* newNodePtr = makeNode(item); Node* prevPtr = NULL; if (position == 0) { newNodePtr->nextPtr = listPtr->headPtr; listPtr->headPtr = newNodePtr; } else { prevPtr = setPosition(listPtr, position-1); newNodePtr->nextPtr = prevPtr->nextPtr; prevPtr->nextPtr = newNodePtr; } listPtr->count++; } /* Delete the node at a position in the Linked List. */ void deleteNode(List* listPtr, int position) { Node* oldNodePtr = NULL; Node* prevPtr = NULL; if (/*listPtr->headPtr!=NULL && position < listPtr->count*/ 1) { if (position == 0) { oldNodePtr = listPtr->headPtr; listPtr->headPtr = oldNodePtr->nextPtr; } else { prevPtr = setPosition(listPtr, position-1); oldNodePtr = prevPtr->nextPtr; prevPtr->nextPtr = oldNodePtr->nextPtr; } listPtr->count--; free(oldNodePtr); } else { fprintf(stderr, "List is empty or invalid position\n"); exit(1); } } void freeMem(List* listPtr) { while((listPtr->count)>0) { deleteNode(listPtr,0); } } int main(int argc, /* Count of command line arguments (parameters) */ char *argv[]) /* Values of parameters: array of string (pointer to cha= r) */ { printf("start"); if(argc == 0) exit(1); List* storage = (List*)malloc(32); /*initializeList(storage);*/ int i, temp; int m; /*m = atoi(argv[1]);*/ m=5; FILE *fp; fp = fopen(/*argv[2]*/"input.txt", "r"); if(fp==NULL) /* error, could not open= it */ { printf("could not open file \"%s\", halting\n", argv[1]); exit(1); } int input, min=10000000, max=0, length=0, biggest=0, addLow, addH= igh, counter=0; while(fscanf(fp, "%d", &input)!=EOF) { printf("%d", input); if(inputmax) { max=input; addHigh=counter; /*check if extreme*/ } if(max-min>=m) { length=0; deleteNode(storage, 0); for(i=0;i<(storage->count);i++) { printf("%d\n", i); temp = (setPosition(storage, i))->value; if(tempmax) max=temp; } } if(max-minbiggest) biggest++; insertItem(storage, input, storage->count); } counter++; } printf("%d %d %d\n", addLow, addHigh, biggest); freeMem(storage); return 1; } int main(int argc, /* Count of command line arguments (parameters) */ char *argv[]) /* Values of parameters: array of string (pointer to cha= r) */ { printf("start\n"); if(argc == 0) exit(1); int* array; array = malloc(5000000*sizeof(int)); int i, j, temp, tmax, tmin; int m=0, low=0, ok=0; /*printf("%d\n", argv[1]);*/ printf("%s\n", argv[2]); /*m = atoi(argv[1]);*/ m=5; FILE *fp; fp = fopen("input.txt", "r"); if(fp==NULL) /* error, could not open= it */ { printf("could not open file \"%s\", halting\n", argv[2]); exit(1); } int input, min=10000000, max=0, length=0, biggest=0, addLow, addH= igh; for(j=0;fscanf(fp, "%d", &input)!=EOF;j++) { printf("%d\n", input); array[j]=input; if(inputbiggest) biggest++; } if(input>max && input-minbiggest) biggest++; /*check if extreme*/ } if((inputmin)) { length++; if(length>biggest) biggest++; } if(max-input>=m||input-min>=m) { length=0; addLow=low; addHigh=j; for(i=low;(ok==1);i++) { temp=array[i]; tmin=min; tmax=max; if(i=j) exit(1); if(temptmax) tmax=temp; if((tmax-tmin)>>%d %d %d", addLow, addHigh, biggest); return 1; }