thoga31

Linked list - stack, queue and sorted

Aug 2nd, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.65 KB | None | 0 0
  1. program linked_lists;
  2.  
  3. type TLinkedList = ^TLL;
  4.      TLL = record
  5.         data : integer;
  6.         next : TLinkedList;
  7.      end;
  8.  
  9. procedure AddValue(head : TLinkedList; const v : integer);
  10. (* Add value to the linked list *)
  11. var cur : TLinkedList;
  12. begin
  13.     new(cur);  // the new node
  14.     cur^.data := v;
  15.     cur^.next := nil;
  16.     while head^.next <> nil do  // search the last node of the list
  17.         head := head^.next;
  18.     head^.next := cur;  // link the list to the new node
  19. end;
  20.  
  21. procedure ShowValuesAsQueue(head : TLinkedList);
  22. (* Shows the values of the linked list through FIFO *)
  23. begin
  24.     head := head^.next;  // jumps the "pseudovalue"
  25.     while head <> nil do begin
  26.         write(head^.data,', ');
  27.         head := head^.next;
  28.     end;
  29.     writeln;
  30. end;
  31.  
  32. procedure ShowValuesAsStack(head : TLinkedList);
  33. (* Shows the values of the linked list through LIFO *)
  34. var cur, last : TLinkedList;
  35. begin
  36.     head := head^.next;  // jumps the "pseudovalue"
  37.     cur := head;  // the "head" pointer must not be lost, the method depends on it
  38.     while cur^.next <> nil do
  39.         cur := cur^.next;  // finds the last element...
  40.     last := cur;           // and stores it in "last" pointer.
  41.    
  42.     while last <> head do begin
  43.         cur := head;  // goes to the first element of the list
  44.         while cur^.next <> last do  // finds the penultimate element - it'll be the new last
  45.             cur := cur^.next;
  46.         last := cur;  // stores the penultimate element as the new last
  47.         cur := cur^.next;        // goes to the last element...
  48.         write(cur^.data, ', ');  // ...and shows it.
  49.     end;
  50.     writeln;
  51. end;
  52.  
  53. procedure CopyList(head, newlist : TLinkedList);
  54. (* Creates a copy of the list pointed by "head" *)
  55. begin
  56.     head := head^.next;  // jumps the "pseudovalue"
  57.     while head <> nil do begin
  58.         AddValue(newlist, head^.data);
  59.         head := head^.next;
  60.     end;
  61. end;
  62.  
  63. procedure SortList(head : TLinkedList);
  64. (* Sorts a linked list using the Bubble Sort Algorithm *)
  65. var head2 : TLinkedList;
  66.     val : integer;
  67. begin
  68.     head := head^.next;
  69.     while head^.next <> nil do begin
  70.         head2 := head^.next;
  71.         while head2 <> nil do begin
  72.             if head^.data > head2^.data then begin
  73.                 val := head^.data;
  74.                 head^.data := head2^.data;
  75.                 head2^.data := val;
  76.             end;
  77.             head2 := head2^.next;
  78.         end;
  79.         head := head^.next;
  80.     end;
  81. end;
  82.  
  83. procedure DisposeList(head : TLinkedList);
  84. (* Frees memory related to a linked list pointed by "head" *)
  85. var cur : TLinkedList;
  86. begin
  87.     while head <> nil do begin
  88.         cur := head;  // points to the current element
  89.         head := head^.next;  // points to the next element
  90.         dispose(cur);  // disposes this element
  91.     end;
  92. end;
  93.  
  94. var i : integer;
  95.     list   : TLinkedList;
  96.     sorted : TLinkedList;
  97. begin
  98.     new(list);  // the "new" method creates a new element which is not of our interest - "pseudovalue"
  99.     writeln('Loading list with random values...');
  100.    
  101.     // Loads the linked list with 10 random values
  102.     randomize;
  103.     for i := 1 to 10 do
  104.         AddValue(list, random(20));
  105.    
  106.     // Shows the created list as a stack and as a queue
  107.     write(' Stack: ');
  108.     ShowValuesAsStack(list);
  109.     write(' Queue: ');
  110.     ShowValuesAsQueue(list);
  111.    
  112.     // Creates a copy of the list, and then it is sorted and shown as a queue
  113.     new(sorted);
  114.     write('Sorted: ');
  115.     CopyList(list, sorted);
  116.     SortList(sorted);
  117.     ShowValuesAsQueue(sorted);
  118.    
  119.     // Frees memory
  120.     DisposeList(list);
  121.     DisposeList(sorted);
  122.    
  123.     readln; // pause
  124. end.
Advertisement
Add Comment
Please, Sign In to add comment