niepok

choinka

Jan 25th, 2015
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 4.58 KB | None | 0 0
  1. (* Lab10 - choinka *)
  2.  
  3. program choinka;
  4. uses crt;
  5.  
  6. type
  7. PListItem = ^ListItem;
  8.  
  9. PTreeItem = ^TreeItem;
  10.  
  11. ListItem = record
  12.     next : PListItem;
  13.     prev : PListItem;
  14.     val : PTreeItem;
  15. end;
  16.  
  17. TreeItem = record
  18.     firstChild : PListItem;
  19.     name : String;
  20. end;
  21.  
  22. var
  23. root : PTreeItem;
  24. tempListItem, tempListItem2 : PListItem;
  25. cmd : char;
  26. depth, i : integer;
  27. personName, presentName : string;
  28.  
  29. procedure initTree;
  30. begin
  31.     new(root);
  32.     root^.name := 'choinka';
  33.     root^.firstChild := nil;
  34. end;
  35.  
  36. procedure addPerson(name : string);
  37. var newPerson : PTreeItem;
  38. begin
  39.     new(newPerson);
  40.     newPerson^.name := name;
  41.     newPerson^.firstChild := nil;
  42.    
  43.     if root^.firstChild = nil then
  44.     begin
  45.         new(root^.firstChild);
  46.         root^.firstChild^.val := newPerson;
  47.     end
  48.     else
  49.     begin
  50.         tempListItem := root^.firstChild;
  51.         while (tempListItem^.next <> nil) do
  52.         begin
  53.             tempListItem := tempListItem^.next;
  54.         end;
  55.         new(tempListItem^.next);
  56.         tempListItem^.next^.val := newPerson;
  57.         tempListItem^.next^.prev := tempListItem;
  58.         tempListItem^.next^.next := nil;
  59.     end;
  60. end;
  61.  
  62. procedure removePerson(name : string);
  63. begin
  64.     if root^.firstChild = nil then
  65.     begin
  66.         tempListItem := root^.firstChild;
  67.        
  68.         while (tempListItem^.val^.name <> name) and (tempListItem^.next <> nil) do
  69.         begin
  70.             tempListItem := tempListItem^.next;
  71.         end;
  72.        
  73.         if tempListItem^.val^.name = name then
  74.         begin
  75.             if (tempListItem^.next = nil) and (tempListItem^.prev = nil) then
  76.             begin
  77.                 root^.firstChild := nil;
  78.             end
  79.             else
  80.             begin
  81.                 if (tempListItem^.next = nil) then
  82.                 begin
  83.                     tempListItem^.next^.prev := tempListItem^.prev
  84.                 end;
  85.                 if (tempListItem^.next = nil) then
  86.                 begin
  87.                     tempListItem^.prev^.next := tempListItem^.next;
  88.                 end;
  89.             end;
  90.         end;
  91.     end
  92. end;
  93.  
  94. procedure addPresent(personName : string; presentName : string);
  95. begin
  96.     if root^.firstChild = nil then
  97.     begin
  98.         tempListItem := root^.firstChild;
  99.        
  100.         while (tempListItem^.val^.name <> personName) and (tempListItem^.next <> nil) do
  101.         begin
  102.             tempListItem := tempListItem^.next;
  103.         end;
  104.        
  105.         if tempListItem^.val^.name = personName then
  106.         begin
  107.             tempListItem := tempListItem^.val^.firstChild;
  108.            
  109.             if tempListItem = nil then
  110.             begin
  111.                 new(tempListItem);
  112.                 new(tempListItem^.val);
  113.                 tempListItem^.val^.name := presentName;
  114.                 tempListItem^.val^.firstChild := nil;
  115.             end
  116.             else
  117.             begin
  118.                 while (tempListItem^.next <> nil) do
  119.                 begin
  120.                     tempListItem := tempListItem^.next;
  121.                 end;
  122.                 new(tempListItem^.next);
  123.                 new(tempListItem^.next^.val);
  124.                 tempListItem^.next^.val^.name := presentName;
  125.                 tempListItem^.next^.prev := tempListItem;
  126.             end;
  127.         end;
  128.     end
  129. end;
  130.  
  131. procedure removePresent(personName : string; presentName : string);
  132. begin
  133.     if root^.firstChild = nil then
  134.     begin
  135.         tempListItem := root^.firstChild;
  136.        
  137.         while (tempListItem^.val^.name <> personName) and (tempListItem^.next <> nil) do
  138.         begin
  139.             tempListItem := tempListItem^.next;
  140.         end;
  141.        
  142.         if tempListItem^.val^.name = personName then
  143.         begin
  144.             tempListItem := tempListItem^.val^.firstChild;
  145.            
  146.            
  147.             while (tempListItem^.val^.name <> presentName) and (tempListItem^.next <> nil) do
  148.             begin
  149.                 tempListItem := tempListItem^.next;
  150.             end;
  151.            
  152.             if tempListItem^.val^.name = presentName then
  153.             begin
  154.                 if (tempListItem^.next = nil) and (tempListItem^.prev = nil) then
  155.                 begin
  156.                     removePerson(personName);
  157.                 end
  158.                 else
  159.                 begin
  160.                     if (tempListItem^.next = nil) then
  161.                     begin
  162.                         tempListItem^.next^.prev := tempListItem^.prev
  163.                     end;
  164.                     if (tempListItem^.next = nil) then
  165.                     begin
  166.                         tempListItem^.prev^.next := tempListItem^.next;
  167.                     end;
  168.                 end;
  169.             end;
  170.         end;
  171.     end;
  172. end;
  173.  
  174. procedure printNode(treeItem : PTreeItem);
  175. begin
  176.     for i := 1 to depth do
  177.     begin
  178.         write('  ');
  179.     end;
  180.    
  181.     writeln(treeItem^.name);
  182.    
  183.     tempListItem2 := treeItem^.firstChild;
  184.    
  185.     if treeItem^.firstChild <> nil then
  186.     begin
  187.         while tempListItem2 <> nil do
  188.         begin
  189.             inc(depth);
  190.             printNode(tempListItem2^.val);
  191.             tempListItem := tempListItem2^.next;
  192.             dec(depth);
  193.         end;
  194.     end;
  195. end;
  196.  
  197.  
  198. begin
  199.     initTree();
  200.    
  201.     repeat
  202.     begin
  203.         clrscr;
  204.         writeln('Twoja choinka:');
  205.         depth := 0;
  206.         printNode(root);
  207.         writeln('1 dodaj osobe, 2 usun osobe, 3 dodaj prezent, 4 usun prezent, 0 wyjscie');
  208.         cmd := readkey;
  209.        
  210.         case cmd of
  211.             '1':
  212.             begin
  213.                 write('Podaj osobe: ');
  214.                 readln(personName);
  215.                 addPerson(personName);
  216.             end;
  217.            
  218.             '2':
  219.             begin
  220.                
  221.             end;
  222.            
  223.             '3':
  224.             begin
  225.                
  226.             end;
  227.            
  228.             '4':
  229.             begin
  230.                
  231.             end;
  232.         end;
  233.     end
  234.     until cmd = '0';
  235. end.
Advertisement
Add Comment
Please, Sign In to add comment