Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
66
Добавлен:
08.07.2017
Размер:
3.75 Кб
Скачать
//---------------------------------------------------------------------------

#include <vcl.h>
#include <iostream>
#include <stdlib.h>
#include <windows.h>

using namespace std;
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused


struct link {
  char* data;
  int priority;
  struct link *next;
};

void PrintQueue(struct link*);
struct link *InitQueue(struct link*);  // инициализация очереди
int GetPriority(char*);       // определяет приоритет элемента в очереди
struct link *PredElQueue(struct link *head,struct link *queue); // поиск указателя на предыдущий элемент
struct link *AddElQueue(struct link *queue,char *data);  // вставка элемента, с учетом приоритета
void DeletElQueue(struct link *); // удаление элемента


int main()
{
  struct link *queue; // адрес, указывающий на голову стека
  queue = (struct link *)malloc(sizeof(struct link));
  queue = NULL;
  cout << "Elementy slovara - iznachalno:" << endl;
  queue=InitQueue(queue);
  system("pause");
  return 0;
}

void PrintQueue(struct link *p)
{
  int kol=0;
  while (p!=NULL)
  {
    if (!kol) cout << p->data;
    else cout << " > " << p->data;
    kol++;
    p=p->next;
  }
  cout << endl;
}

struct link *InitQueue(struct link *queue)
{
  struct link *head; // адрес, указывающий на голову стека
  head = (struct link *)malloc(sizeof(struct link));
  if (queue==NULL)
    head->next = queue;
  char a[50][5];
  for(int i=0;i<50;i++)
  {
     for(int j=0;j<4;j++)
        a[i][j] = 97+rand() % 26;
  a[i][4] = '\0';
  if(i==0)
     cout <<a[i];
  else
     cout << " > " << a[i];
  }
  head->data=a[0];
  head->priority=GetPriority(a[0]);
  for (int i=1;i<50;i++)
  {
     head=AddElQueue(head,a[i]);
  }
  cout << "\n\nElementy slovara v ocheredy s prioritetom \n";
  PrintQueue(head);
  int n = 0;
  cout << "\n Elementov udalit? n= ";
  cin >> n;
  cout << "\n\n Udalim " << n << " elementov: \n";
  for(int i=1;i<n;i++)
        DeletElQueue(head);
  PrintQueue(head);
  return head;
}

int GetPriority(char *data)
{
  return data[0]-97;
}

struct link *PredElQueue(struct link *head,struct link *queue)
{
  while (head!=NULL)
  {
    if (head==queue)
    {
      return NULL;
      break;
    }
    if (head->next==queue)
    {
       return head;
       break;
    }
    head=head->next;
  }
}

struct link *AddElQueue(struct link *queue,char *data)
{
  struct link *element; // указатель на новую структуру
  element = (struct link *)malloc(sizeof(struct link)); // выделяем память
  element->priority = GetPriority(data);
  element->data = data;
  struct link *head; // адрес, указывающий на голову стека
  head = (struct link *)malloc(sizeof(struct link));
  head = queue;
  while (queue!=NULL)
  {
    if (GetPriority(data)<=queue->priority)
    {
      // вставка элемента перед текущим указателем
      if (!(PredElQueue(head,queue)==NULL)) // предыдущий элемент - не конец очереди
      {
        element->next=queue;
        PredElQueue(head,queue)->next=element;
      }
      else  // конец очереди
      {
        element->next=queue;
        head=element;
      }
      break;
    }
    if (queue->next==NULL)  // элемент последний и не вставили, тогда вставляем в начало очереди
    {
      queue->next=element;
      element->next=NULL;
      break;
    }
    queue=queue->next;
  }
  return head;
}

void DeletElQueue(struct link *p)
{
  while (p!=NULL)
  {
    if (p->next->next==NULL)
    {
      p->next=NULL;
      break;
    }
    p=p->next;
  }
}

//---------------------------------------------------------------------------

Соседние файлы в папке lab3
  • #
    08.07.201798 б67Project1.bpf
  • #
    08.07.20173.42 Кб66Project1.bpr
  • #
    08.07.2017876 б66Project1.res
  • #
    08.07.2017196.61 Кб67Project1.tds
  • #
    08.07.20173.75 Кб66Unit1.cpp
  • #
    08.07.2017145.17 Кб66Unit1.obj
  • #
    08.07.20173.75 Кб66Unit1.~cpp
  • #
    08.07.2017492 б66vocabulary.txt