SHARE
TWEET

Untitled

a guest Dec 8th, 2019 103 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. program stack;
  2.  
  3. uses crt;
  4.  
  5. type
  6.   Subscriber = record
  7.     name: string[255];
  8.     adress : string[255];
  9.     number_phone : string[255];
  10.   end;
  11.  
  12. type
  13.   T = Subscriber;
  14.  
  15. type
  16.   PNode = ^ Node;
  17.   Node = record
  18.     data: T;
  19.     next: PNode;
  20.   end;
  21.  
  22. type List = class
  23.  
  24.   private
  25.  
  26.     head: PNode;
  27.     size: Integer;
  28.  
  29.   public
  30.  
  31.     constructor Create();
  32.     function get_size() : integer;
  33.     function get_el(pos:integer) : PNode;
  34.     procedure push_front(el: T);
  35.     procedure erase(pos:Integer);
  36.     procedure out_list();
  37.    
  38. end;    
  39.  
  40. constructor List.Create();
  41. begin
  42.   head:=nil;
  43.   size:=0;
  44. end;
  45.    
  46. function List.get_size() : integer;
  47. begin
  48.   get_size:=size;    
  49. end;
  50.  
  51. function List.get_el(pos:integer) : PNode;
  52. var
  53.   current : PNode = head;
  54. begin
  55.   for i:integer:=0 to size-1 do
  56.   begin
  57.     if(pos=i) then
  58.     begin
  59.       get_el:=current;
  60.       break;
  61.     end;
  62.     current:=current^.next;
  63.   end;
  64. end;
  65.        
  66. procedure List.push_front(el: T);
  67. var
  68.   current: PNode;
  69. begin
  70.   new(current);
  71.   current^ .data := el;
  72.   current^ .next := head;
  73.   head := current;
  74.   size+=1;
  75. end;
  76.  
  77. procedure List.erase(pos:Integer);
  78. var
  79.   current:PNode=head;
  80.   previous:PNode=nil;
  81.   found : boolean = false;
  82. begin
  83.   if(pos<0) then
  84.   begin
  85.     writeln('Incorrect position entered');
  86.     exit;
  87.   end;
  88.   for i: Integer :=0 to size-1 do
  89.   begin
  90.     if(i=pos) then
  91.     begin
  92.       found:=true;
  93.       if(previous = nil) then
  94.       begin
  95.         head := current^.next;
  96.         dispose(current);
  97.         break;
  98.       end
  99.       else
  100.       begin
  101.         previous^.next := current^.next;
  102.         dispose(current);
  103.         break;
  104.       end;
  105.     end;
  106.     previous:=current;
  107.     current:=current^.next;
  108.   end;
  109.  
  110.   if(found) then
  111.     size-=1
  112.   else
  113.     writeln('The entered position is larger than the size of the List');
  114. end;
  115.  
  116. procedure List.out_list();
  117. var
  118.   current: PNode;
  119. begin
  120.   current := head;
  121.   write('List = {');
  122.   while (current <> nil) do
  123.   begin
  124.     write(current^.data:3, ';');
  125.     current:=current^.next;
  126.   end;
  127.   write('}');
  128.   writeln();
  129.   write('Size = ',size);
  130.   writeln();
  131. end;
  132.  
  133. var a : List;
  134.     quit, buf_string : string;
  135.     number_command, pos, n_subscribers :integer;
  136.     data : T;
  137.     element : PNode;
  138.     f : file of Subscriber;
  139.  
  140. begin
  141.   a := new List;
  142.   Assign(f,'C:\PABCWork.NET\My Work\Subscribers.data');
  143.   Rewrite(f);
  144.   Writeln('Enter number of subscribers:');
  145.   Readln(n_subscribers);
  146.   for i:integer:=1 to n_subscribers do
  147.   begin
  148.     ClrScr();
  149.     writeln('Entered name:');
  150.     Readln(data.name);
  151.     writeln('Entered adress:');
  152.     Readln(data.adress);
  153.     writeln('Entered number phone:');
  154.     Readln(data.number_phone);
  155.     Write(f,data);
  156.   end;
  157.   Close(f);
  158.  
  159.   Reset(f);
  160.   while not Eof(f) do
  161.   begin
  162.     Read(f,data);
  163.     a.push_front(data);
  164.   end;
  165.   Close(f);
  166.  
  167.   while(quit<>'q') do
  168.   begin
  169.     ClrScr();
  170.     writeln('Choose a command:');
  171.     writeln('1) Push front');
  172.     writeln('2) Erase');
  173.     Writeln('3) Edit');
  174.     writeln('4) Out List');
  175.     writeln('5) Quit');
  176.     readln(number_command);
  177.     case number_command of
  178.       1:
  179.       begin
  180.         ClrScr();
  181.         writeln('Entered name:');
  182.         Readln(data.name);
  183.         writeln('Entered adress:');
  184.         Readln(data.adress);
  185.         writeln('Entered number phone:');
  186.         Readln(data.number_phone);
  187.         a.push_front(data);
  188.       end;
  189.       2:
  190.       begin
  191.         ClrScr();
  192.         writeln('Enter the position of the element to be deleted:');
  193.         Readln(pos);
  194.         a.erase(pos);
  195.         if((pos<0) or (pos>=a.get_size())) then
  196.           Readln();
  197.       end;
  198.      
  199.       3:
  200.       begin
  201.         ClrScr();
  202.         writeln('Enter the position of the element to be edit:');
  203.         Readln(pos);
  204.         if((pos<0) or (pos>=a.get_size())) then
  205.           Writeln('Incorrect position entered')
  206.         else
  207.         begin
  208.           element:=a.get_el(pos);
  209.           Writeln('Choose a command:');
  210.           Writeln('1)Edit name');
  211.           Writeln('2)Edit adress');
  212.           Writeln('3)Edit number phone');
  213.           Readln(number_command);
  214.           case number_command of
  215.             1:
  216.             begin
  217.               Writeln('Entered new name:');
  218.               Readln(buf_string);
  219.               element^.data.name:=buf_string;
  220.             end;
  221.            
  222.             2:
  223.             begin
  224.               Writeln('Entered new adress:');
  225.               Readln(buf_string);
  226.               element^.data.adress:=buf_string;
  227.             end;
  228.            
  229.             3:
  230.             begin
  231.               Writeln('Entered new number phone:');
  232.               Readln(buf_string);
  233.               element^.data.number_phone:=buf_string;
  234.             end;
  235.            
  236.             else
  237.             begin
  238.               ClrScr();
  239.               writeln('Command not found.');
  240.               readln();
  241.             end;
  242.           end;
  243.         end;
  244.       end;
  245.      
  246.       4:
  247.       begin
  248.         ClrScr();
  249.         a.out_list();
  250.         Readln();
  251.       end;
  252.       5: quit:='q';
  253.       else
  254.       begin
  255.         ClrScr();
  256.         writeln('Command not found. Please try again.');
  257.         readln();
  258.       end;
  259.     end;
  260.   end;
  261. end.
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top