Выбрать из исходного дека элементы, на номера которых указывает магазин, и поместить их в стэк, нумерация элементов дека начинается справа - задание
Код:
/*
rewit.c: ..
2012-12-02
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define file_out "out.txt"
#define first_in "first.txt"
#define second_in "second.txt"
#define stack_size 512
#define fifo_size 512
int* p_stack;
int* s_stack;
int* sp;
int* p_fifo;
int* s_fifo;
int* fp;
int init_fifo() {
p_fifo = (int*) malloc(sizeof(int) * fifo_size);
s_fifo = p_fifo;
if (!p_fifo) return -1;
fp = p_fifo;
return 0;
}
int is_fifo() {
if (p_fifo-s_fifo > 0)
return 1;
else
return 0;
}
int deinit_fifo() {
if (p_fifo)
free(p_fifo);
return 0;
}
int put_fifo(int value) {
*p_fifo++ = value;
return 0;
}
int get_fifo() {
return *(--p_fifo);
}
int init_stack() {
p_stack = (int*) malloc(sizeof(int) * stack_size);
s_stack = p_stack;
if (!p_stack) return -1;
sp = p_stack;
return 0;
}
int is_stack() {
if (p_stack-s_stack > 0)
return 1;
else
return 0;
}
int deinit_stack() {
if (p_stack)
free(p_stack);
return 0;
}
int push(int value) {
*p_stack++ = value;
return 0;
}
int pop() {
return *(--p_stack);
}
//элемент дэка
typedef struct snd {
int value;
int index;
struct snd* next;
struct snd* prev;
} SND;
int main(int argc, char** argv) {
SND* psnd; //current deck item
SND* ssnd; //start deck item
//file input prepare
FILE* fi = fopen(first_in,"rt");
if (!fi) {
printf("%s%s\n","could'nt open file: ",first_in);
return -1;
}
FILE* si = fopen(second_in,"rt");
if (!si) {
printf("%s%s\n","could'nt open file: ",second_in);
fclose(fi);
return -1;
}
FILE* fo = fopen(file_out,"wt");
if (!fo) {
printf("%s%s\n","could'nt open file: ",file_out);
fclose(fi);
fclose(si);
return -1;
}
init_stack();
init_fifo();
//ssnd = malloc(sizeof(SND));
psnd = 0;
//source read. deck
int value;
int count = 0;
int index = 0;
fprintf(fo,"%s\n","source:");
while (fscanf(fi,"%d",&value) != EOF) {
if (psnd) {
psnd->next = (snd*)malloc(sizeof(SND));
psnd->next->prev = psnd;
psnd->next->value = value;
psnd->index = index++;
psnd->next->next = 0;
psnd = psnd->next;
} else {
ssnd = (SND*)malloc(sizeof(SND));
psnd = ssnd;
psnd->value = value;
psnd->prev = 0;
psnd->next = 0;
psnd->index = index++;
}
count++;
fprintf(fo,"%d ",psnd->value);
}
fprintf(fo,"\n");
//index read. FIFO
int position;
fprintf(fo,"\n%s\n","positions:");
while (fscanf(si,"%d",&position) != EOF) {
put_fifo(position);
fprintf(fo,"%d ",position);
}
fprintf(fo,"\n");
SND* tsnd;
//result pushing. stack
while (is_fifo()) {
position = get_fifo();
while(psnd->index != position) {
tsnd = psnd;
psnd = psnd->prev;
psnd->next = 0;
free(tsnd);
}
value = psnd->value;
fprintf(fo,"value: %d; index: %d\n",psnd->value, psnd->index);
push(value);
}
fprintf(fo,"\n");
//free remaining deck item
while (psnd->prev) {
tsnd = psnd;
psnd = psnd->prev;
psnd->next = 0;
free(tsnd);
}
free(ssnd);
//result printing
fprintf(fo,"%s\n","result:");
while (is_stack()) {
fprintf(fo,"%d ",pop());
}
fprintf(fo,"\n");
//free(ssnd);
deinit_fifo();
deinit_stack();
fclose(fi);
fclose(si);
fclose(fo);
return 0;
}
проблема собственно в том что не могу догнать чтобы выполнялась выборка элементов справа, в общем мозг кипит по жути, третий день парюсь. и как добавить проверку на пустой ввод? я уже забыл что это такое (((
p.s. ребят не судите строго я вернулся к учебе после армии....
P.s.s. кто может помочь заранее спасибо, кто хочет сказать что я м%дак, говорите пожалуйста по мягче с аргументами и по возможности - вариант решения