-- this class is used to represent each node of the linked list where each node -- has a Integer value and a pointer to the next node. the class has set and get -- methods for both attributes. class Node { value : Int; -- integer value stored in each node of the list nextNode : Node; -- the pointer to the next node in the list -- this method is used to initialize the attributes of the Node object and the object is -- returned when evaluated. init(val : Int) : Node { { value <- val; self; } }; -- this method is used to set the pointer for the next node followed by the current node. setNextNode(node : Node) : Node { { nextNode <- node; self; } }; -- this method is used to access next node followed by the current node. the pointer to the -- next node is returned. getNextNode() : Node { nextNode }; -- this method is used to set the integer value stored in the current node. setValue(val : Int) : Node { { value <- val; self; } }; -- this method is used to access the integer value stored in the current node. getValue() : Int { value }; }; -- this class is used to implement a linked list consisting of Node objects linked -- to each other. the operations add and remove are both implemented to add/remove -- from the back of the list so that this linked list structure can be used as a -- Stack data strucure. class LinkedList inherits IO { head : Node; -- this node is the first element of the linked list size : Int; -- an integer value used to keep track of list size temp : Node; -- this node is used to traverse the linked list where it goes until it becomes void temp2 : Node; -- this node is used to traverse the linked list where it keeps the previous node of temp temp3 : Node; -- this node is used to delete elements since it is not initialized it is void value : Int <- 0; -- this integer attribute is used to return the deleted node's value -- this method is used to initialize LinkedList objects. Only size attribute is -- is initialized because i wanted to have a void value for Node elements so i can -- recognize the end of the list. end of the list is when you arrive a Node element -- with void value. init() : LinkedList { { size <- 0; self; } }; -- this method is used to determine if the list is empty or not. it returns true if the -- list is empty and false otherwise. isEmpty() : Bool { if (size = 0) then { out_string("Empty\n"); true; } else { out_string("Not Empty\n"); false; } fi }; -- this method is used to access size of the list. listSize() : Int { { out_string("List Size : "); out_int(size); out_string("\n"); size; } }; -- this method is used to add a new Node to the end of the list. add(value : Int) : LinkedList { { size <- size + 1; if (isvoid(head)) then -- empty list { head <- (new Node).init(value); } else -- list not empty { temp <- head; while (not isvoid(temp)) loop { temp2 <- temp; temp <- temp.getNextNode(); } pool; temp2.setNextNode((new Node).init(value)); } fi; self; } }; -- this method is used to remove the last Node of the list if linked -- list is not empty and return integer value of the deleted Node. remove() : Int { { value <- 0; out_string("Check list size before deletion! "); if (listSize() = 0) then { out_string("The list is EMPTY! You cant delete.\n"); } else -- deletion { temp <- head; while (not isvoid(temp.getNextNode())) loop { temp2 <- temp; temp <- temp.getNextNode(); } pool; value <- temp.getValue(); temp2.setNextNode(temp3); } fi; size <- size - 1; value; } }; -- this method is used to print the Integer values stored in the list Nodes. printList() : LinkedList { { temp <- head; while (not isvoid(temp)) loop { out_string(" -> "); out_int(temp.getValue()); temp <- temp.getNextNode(); } pool; out_string("\n"); self; } }; }; -- this class implements a Stack data strucure and uses a Linked List to -- accomplish its operations. class Stack inherits IO { list : LinkedList; -- LinkedList used to store Stack value : Int <- 0; -- this variable is used to return the value -- of the removed elements of the Stack -- this method is used to initialize a Stack objects attributes initial values init() : Stack { { list <- (new LinkedList).init(); self; } }; -- this method is used to add a new element to the top of the stack. push(value : Int) : Stack { { list.add(value); self; } }; -- this method is used to remove an element from the top of the stack and -- returns Integer value stored in the last element. pop() : Int { { value <- 0; if (list.listSize() = 0) then out_string("Stack is EMPTY! You cant delete.\n") else { value <- list.remove(); } fi; value; } }; -- this method is used to print the contents(integer values) of the stack. printStack() : Stack { { if (list.listSize() = 0) then out_string("Stack is EMPTY! No elements in the stack.\n") else { list.printList(); } fi; self; } }; }; -- this class is the Main class and is needed to run programs using its main() method. class Main inherits IO { list : LinkedList; -- linked list object stack : Stack; -- stack object main() : Object { { out_string("-- LINKED LIST --\n"); -- some test case operations on list <- (new LinkedList).init(); -- linked list object out_string("An empty list is created!\n"); list.listSize(); out_string("Test if the linked list is empty : "); list.isEmpty(); out_string("Add an element to the linked list : "); list.add(5); list.printList(); list.listSize(); out_string("Add more elements to the linked list : "); list.add(7); list.add(3); list.add(6); list.add(10); list.add(2); list.printList(); out_string("Remove an element from the list : "); list.remove(); list.printList(); list.listSize(); out_string("Remove an other element from the list : "); list.remove(); list.printList(); list.listSize(); out_string("-- STACK --\n"); -- some test case operations on stack <- (new Stack).init(); -- stack object out_string("An empty stack is created!\n"); stack.pop(); stack.printStack(); stack.push(3); stack.push(8); stack.push(1); stack.printStack(); let num1 : Int, num2 : Int in { num1 <- stack.pop(); out_string("One number is deleted from the stack : "); out_int(num1); out_string("\n"); num2 <- stack.pop(); out_string("Another number is deleted from the stack : "); out_int(num2); out_string("\n"); stack.printStack(); }; } }; };