Advertisement
wojtas626

[PAS] Stos

Dec 15th, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.19 KB | None | 0 0
  1. program Project1;
  2.  
  3. uses
  4.     crt, DOS;
  5. type
  6.   listPtr = ^list;
  7.  
  8.   list = record
  9.        next: listPtr;
  10.        previous: listPtr;
  11.        x: String;
  12.   end;
  13.  
  14. procedure push(var listStart: listPtr; var listEnd: listPtr);
  15. var
  16.    newElement: listPtr;
  17. begin
  18.      clrScr;
  19.      if listEnd = NIL then
  20.      begin
  21.           newElement := listStart;
  22.           new(newElement);
  23.           writeLn('Wprowadz element stosu: ');
  24.           readLn(newElement^.x);
  25.           newElement^.next := NIL;
  26.           newElement^.previous := NIL;
  27.           listEnd := newElement;
  28.           listStart := newElement;
  29.      end else
  30.      begin
  31.           newElement := listEnd;
  32.           New(newElement^.next);
  33.           newElement := newElement^.next;
  34.           writeLn('Wprowadz element stosu: ');
  35.           readLn(newElement^.x);
  36.  
  37.           newElement^.next := NIL;
  38.           newElement^.previous := listEnd;
  39.           listEnd := newElement;
  40.      end;
  41. end;
  42.  
  43.  
  44. procedure pop(var listStart: listPtr; var listEnd: listPtr);
  45. var
  46.    pointer: listPtr;
  47. begin
  48.      if listStart = NIL then
  49.      begin
  50.           clrScr;
  51.           writeLn('Nie moge usunac - stos pusty!');
  52.           writeLn;
  53.           writeLn('Nacisnij dowolny klawisz, aby powrocic do menu...');
  54.           ReadKey;
  55.      end;
  56.      if listStart^.next = NIL then
  57.      begin
  58.           pointer := listStart;
  59.           dispose(pointer);
  60.           listStart := NIL;
  61.           listEnd := NIL;
  62.      end else
  63.      begin
  64.           pointer := listEnd;
  65.           listEnd := pointer^.previous;
  66.           listEnd^.next := NIL;
  67.           dispose(pointer);
  68.      end;
  69. end;
  70.  
  71.  
  72. procedure isEmpty(var listStart: listPtr);
  73. var
  74.    pointer: listPtr;
  75. begin
  76.      clrScr;
  77.      if listStart = NIL then
  78.      begin
  79.           WriteLn('Stos pusty!');
  80.      end else
  81.      begin
  82.           writeLn('Stos NIE jest pusty!');
  83.      end;
  84.      writeLn;
  85.      writeLn('Nacisnij dowolny klawisz, aby powrocic do menu...');
  86.      ReadKey;
  87.  
  88. end;
  89.  
  90.  
  91. procedure listShow(var listStart: listPtr);
  92. var
  93.    pointer: listPtr;
  94. begin
  95.      clrScr;
  96.      pointer := listStart;
  97.      if pointer = NIL then
  98.      begin
  99.           writeLn('Lista pusta!');
  100.      end else
  101.      begin
  102.           writeLn('Element: ', pointer^.x);
  103.  
  104.           while pointer^.next <> NIL do
  105.           begin
  106.                writeLn;
  107.                pointer := pointer^.next;
  108.                writeLn('Element: ', pointer^.x);
  109.           end;
  110.      end;
  111.      writeLn;
  112.      writeLn('Nacisnij dowolny klawisz, aby powrocic do menu...');
  113.      ReadKey;
  114. end;
  115.  
  116.  
  117. var
  118.    listStart, listEnd, pointer: listPtr;
  119.    choice: String;
  120. begin
  121.      listEnd:= NIL;
  122.      listStart := NIL;
  123.  
  124.      while true do
  125.      begin
  126.           clrScr;
  127.           WriteLn('1. Dodaj element do stosu');
  128.           WriteLn('2. Usun element ze stosu');
  129.           WriteLn('3. Sprawdz czy stos jest pusty');
  130.           WriteLn('4. Wyswietl zawartosc stosu');
  131.           WriteLn('0. Wyjscie');
  132.           ReadLn(choice);
  133.           case choice of
  134.           '1': push(listStart, listEnd);
  135.           '2': pop(listStart, listEnd);
  136.           '3': isEmpty(listStart);
  137.           '4': listShow(listStart);
  138.           '0': break;
  139.           end;
  140.      end;
  141. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement