1 / 10

Реализация на опашка в статична памет

Реализация на опашка в статична памет. 1. Дефиниране на опашка. Const MAX=100; Type T = integer; queue = record front, rear: 0..MAX-1; n: integer; que: array[0..MAX-1] of T; end; Var q: queue;. 2. Операции с опашка. Създаване на празна опашка

zeus-alston
Download Presentation

Реализация на опашка в статична памет

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Реализация на опашка в статична памет

  2. 1. Дефиниране на опашка Const MAX=100; Type T = integer; queue = record front, rear: 0..MAX-1; n: integer; que: array[0..MAX-1] of T; end; Var q: queue;

  3. 2. Операции с опашка • Създаване на празна опашка procedure create_empty_queue(var q:queue); begin q.n:=0; q.front:=0; q.rear:=0; end; • Проверка за празна опашка function empty_queue(q:queue):boolen; begin empty_queue:=q.n=0; end; • Проверка за пълна опашка function full_queue(q:queue):boolen; begin full_queue:=q.n=MAX; end;

  4. Добавяне на компонента procedure push_in_queue(var q:queue, elem:T); begin if full_queue(q) then writeln(‘Operaciqta ne e wyzmovna’) else begin q.que[q.rear]:=elem; q.n:=q.n+1; q.rear:=(q.rear+1) mod MAX end end;

  5. Отстраняване на компонента procedure pop_of_queue(var q:queue, elem:T); Begin if not empty_queue(q) then begin elem:=q.que[q.front]; q.n:=q.n-1; q.front:=(q.front+1) mod MAX end end;

  6. Намиране на главата на опашката procedure top_of_queue(q:queue; var a:T); begin if not empty_queue(q) then a:=q.que[q.front] end; • Намиране броя на компонентите на опашката function Size(q:queue):integer; begin Size:=q.n end;

  7. 3. Задачи за опашки • Задача1: Да се дефинира процедура, която създава опашка от тип integer с n елемента: procedure create_queue(n:integer; var q:queue); Var x,i:integer; Begin create_empty_queue(q); for i:=1 to n do if not full_queue(q) then begin write(‘> ’); readln(x); push_in_queue(q,x); end End;

  8. Задача 2: Да се дефинира процедура, която извежда елементите на опашката от тип integer: procedure print_queue(q:queue); Var a:T; Begin while not empty_queue(q) do begin pop_of_queue(q,a); write(a:4) end; writeln End;

  9. Задача 3: Да се дефинира процедура, която само с едно преминаване през елементите на едномерен масив с базов тип integer извежда отначало всички елементи на масива,които са по- малки от 10, след това тези от интервала [10, 50] и накрая елементите, които са по- големи от 50. При извеждането да се запази редът на елементите в масива. • Например: 15, 8, 1, 13, 3, 45, 123, 4, 21, 47, 74, 189, 78, 56, 49, 12, 9; Извеждане: 8, 1, 3, 4, 9, 15, 13, 45, 21, 47, 49, 12, 123, 74, 189, 78, 56;

  10. Решение на задача 3: Type T= integer; Seg=array[1..MAX] of integer; Procedure Print_Array(n:integer; var a:Seg); Var i:integer; q1,q2:queue; Begin create_empty_queue(q1); create_empty_queue(q2); for i:=1 to n do if a[i]<10 then write(a[i]:4) else if a[i]<=50 then push_in_queue(q1,a[i]) else push_in_queue(q2,a[i]); print_queue(q1); print_queue(q2); End;

More Related