Hello All,
I tried this piece of code and I am able to add element to the head, but when I tried to print the complete link list, the last node is not pointing to the null and I am stuck in the while loop. Any comments ?
module link_list;
class node_class;
int la;
static node_class nc_next;
endclass: node_class
class list_class;
int ll;
static node_class head;
// function new();
// head = new();
// endfunction: new
endclass: list_class
// function create_list(list_class lc);
function list_class create_list();
list_class llc;
llc = new();
// lc = llc;
return llc;
endfunction: create_list
function void add_node_at_header(list_class nlc, int val);
node_class nnc;
nnc = new();
nnc.la = val;
if (nlc.head == null) begin
$display("nlc.head==null",nlc.head);
nnc.nc_next = null;
nlc.head = nnc;
end
else begin
nnc.nc_next = nlc.head;
nlc.head = nnc;
$display("add_node_at_header",nlc.head);
// nnc.nc_next = null;
end
$display("nnc.nc_next", nnc.nc_next);
endfunction: add_node_at_header
function void print_full_list(list_class plc);
node_class tmp_next;
tmp_next = plc.head;
$display("print_full_list", plc.head, tmp_next, tmp_next.nc_next);
while (tmp_next.nc_next != null) begin
$display(tmp_next.la);
$display(tmp_next);
$display(tmp_next.nc_next);
tmp_next = tmp_next.nc_next;
end
endfunction: print_full_list
initial begin
list_class new_ll;
$cast(new_ll, create_list());
$display(new_ll);
$display(new_ll.head);
new_ll.ll = 5;
add_node_at_header(new_ll, 10);
$display(new_ll.head.la);
add_node_at_header(new_ll, 20);
$display(new_ll.head.la);
$display(new_ll.head);
print_full_list(new_ll);
end
endmodule: link_list