Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program linked_lists;
- type TLinkedList = ^TLL;
- TLL = record
- data : integer;
- next : TLinkedList;
- end;
- procedure AddValue(head : TLinkedList; const v : integer);
- (* Add value to the linked list *)
- var cur : TLinkedList;
- begin
- new(cur); // the new node
- cur^.data := v;
- cur^.next := nil;
- while head^.next <> nil do // search the last node of the list
- head := head^.next;
- head^.next := cur; // link the list to the new node
- end;
- procedure ShowValuesAsQueue(head : TLinkedList);
- (* Shows the values of the linked list through FIFO *)
- begin
- head := head^.next; // jumps the "pseudovalue"
- while head <> nil do begin
- write(head^.data,', ');
- head := head^.next;
- end;
- writeln;
- end;
- procedure ShowValuesAsStack(head : TLinkedList);
- (* Shows the values of the linked list through LIFO *)
- var cur, last : TLinkedList;
- begin
- head := head^.next; // jumps the "pseudovalue"
- cur := head; // the "head" pointer must not be lost, the method depends on it
- while cur^.next <> nil do
- cur := cur^.next; // finds the last element...
- last := cur; // and stores it in "last" pointer.
- while last <> head do begin
- cur := head; // goes to the first element of the list
- while cur^.next <> last do // finds the penultimate element - it'll be the new last
- cur := cur^.next;
- last := cur; // stores the penultimate element as the new last
- cur := cur^.next; // goes to the last element...
- write(cur^.data, ', '); // ...and shows it.
- end;
- writeln;
- end;
- procedure CopyList(head, newlist : TLinkedList);
- (* Creates a copy of the list pointed by "head" *)
- begin
- head := head^.next; // jumps the "pseudovalue"
- while head <> nil do begin
- AddValue(newlist, head^.data);
- head := head^.next;
- end;
- end;
- procedure SortList(head : TLinkedList);
- (* Sorts a linked list using the Bubble Sort Algorithm *)
- var head2 : TLinkedList;
- val : integer;
- begin
- head := head^.next;
- while head^.next <> nil do begin
- head2 := head^.next;
- while head2 <> nil do begin
- if head^.data > head2^.data then begin
- val := head^.data;
- head^.data := head2^.data;
- head2^.data := val;
- end;
- head2 := head2^.next;
- end;
- head := head^.next;
- end;
- end;
- procedure DisposeList(head : TLinkedList);
- (* Frees memory related to a linked list pointed by "head" *)
- var cur : TLinkedList;
- begin
- while head <> nil do begin
- cur := head; // points to the current element
- head := head^.next; // points to the next element
- dispose(cur); // disposes this element
- end;
- end;
- var i : integer;
- list : TLinkedList;
- sorted : TLinkedList;
- begin
- new(list); // the "new" method creates a new element which is not of our interest - "pseudovalue"
- writeln('Loading list with random values...');
- // Loads the linked list with 10 random values
- randomize;
- for i := 1 to 10 do
- AddValue(list, random(20));
- // Shows the created list as a stack and as a queue
- write(' Stack: ');
- ShowValuesAsStack(list);
- write(' Queue: ');
- ShowValuesAsQueue(list);
- // Creates a copy of the list, and then it is sorted and shown as a queue
- new(sorted);
- write('Sorted: ');
- CopyList(list, sorted);
- SortList(sorted);
- ShowValuesAsQueue(sorted);
- // Frees memory
- DisposeList(list);
- DisposeList(sorted);
- readln; // pause
- end.
Advertisement
Add Comment
Please, Sign In to add comment