Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 07.12.2016, 03:45   #1
Yacub
 
Регистрация: 23.10.2016
Сообщений: 4
По умолчанию Нужно реализовать FIFO на языке Си

Имея заголовочный файл queue.h нужно реализовать FIFO в файле queue.c, файл queue.h имеет вид:

Код:
#ifndef __QUEUE_H__
#define __QUEUE_H__

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

/* Queue structure which holds all necessary data */
typedef struct {
   // TODO - Include your data structure here
} queue_t;

/* creates a new queue with a given size */
queue_t* create_queue(int capacity);
/* deletes the queue and all allocated memory */
void delete_queue(queue_t *queue);

/* 
 * inserts a reference to the element into the queue
 * returns: true on success; false otherwise
 */
bool push_to_queue(queue_t *queue, void *data);

/* 
 * gets the first element from the queue and removes it from the queue
 * returns: the first element on success; NULL otherwise
 */
void* pop_from_queue(queue_t *queue);

/* 
 * gets idx-th element from the queue 
 * returns: the idx-th element on success; NULL otherwise
 */
void* get_from_queue(queue_t *queue, int idx);

/* gets number of stored elements */
int get_queue_size(queue_t *q);

#endif /* __QUEUE_H__ */
Yacub вне форума Ответить с цитированием
Старый 07.12.2016, 03:49   #2
Yacub
 
Регистрация: 23.10.2016
Сообщений: 4
По умолчанию

И также в наличии исходный файл main.c
Код:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

#include "queue.h"

/* the tested queue */
queue_t *q;

/* allocate new integer with value a and add it to the queue */
void add(int a)
{
   int *p = (int*) malloc(sizeof(int));
   *p = a;
   bool ret = push_to_queue(q, (void*)p);
   if (!ret) {
      // free memory on failure
      free(p);
   }
}

/* print the int value on pointer p */
void print_int(void* p){
    if(p != NULL){
        printf("%d\n", *((int*)p));
    } else {
        printf("NULL\n");
    }
}

/* pop from the queue, print and free the element */
void pop(){
  void* p = pop_from_queue(q);
  print_int(p);
  free(p);
}

/* get i-th element and print it (do not remove them) */
void get(int idx){
  print_int(get_from_queue(q, idx));
}


/* 
 * TEST PROGRAM
 * - reads commands from stdin and executes them in the queue
*/
int main(int argc, char *argv[])
{
   int n;
   // read the size of the queue
   scanf("%d", &n);
   // create queue
   q = create_queue(n);

   while (true) {
      char s[2];
      // read one command
      int ret = scanf("%1s", s);
      if (ret != 1) {
         break;
      }

      // add command
      if (s[0] == 'a') {
         int a;
	 // read the argument of the command
         ret = scanf("%d", &a);
         if (ret != 1) {
            break;
         }
         add(a);
      // remove command	 
      } else if (s[0] == 'r') {
         pop();
      // get command	 
      } else if (s[0] == 'g') {
         int a;
	 // read the argument of the command
         ret = scanf("%d", &a);
         if (ret != 1) {
            break;
         }
         get(a);
      }
   }

   // remove rest of the elements in the queue
   while (get_queue_size(q)) {
      void* p = pop_from_queue(q);
      free(p);
   }

   // free memory
   delete_queue(q);
   q = NULL;
   // return 0 on succes
   return 0;
}
Yacub вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
На каком языке можно реализовать след. задачи Abibok Помощь студентам 7 14.01.2014 06:24
Реализовать прогу на языке С++ (желательно для Qt) Эйфиль Помощь студентам 0 29.05.2013 20:01
Реализовать решение двух задач на языке СИ mixasik28 Фриланс 5 13.10.2012 11:38
реализовать программу на языке Java X1TREC Помощь студентам 1 10.01.2011 01:16
Подскажите пожалуйста на каком языке реализовать? wolfcruel Помощь студентам 1 05.07.2009 18:20