#"PowerShell/Simple Linked List"

54 messages · Page 1 of 1 (latest)

agile pasture
#

I am trying to run this code, but I am making some mistakes and can't find which one

Class Node {
    [int] $data
    [Node] $next
    
    Node() {
        $this.data = 0
        $this.next = $null
    }
    
    Node ( [Int]$value,[Node]$nextnode) {
        $this.data = $value
        $this.next = $nextnode
    }
}


Class LinkedList {
    [Node] $top
    [int] $count
    
    LinkedList() {
        $this.top = [Node]::new()
        $this.count = 0
    }

    Linkedlist ( [int[]] $value){
        foreach ($elt in $value){
            $this.Push($elt)
        }
    }

   [int] Size() {
        return $this.count
    }

    [Node] Head() {
        if ($this.top -ne $null){
             return $this.top
        }else{
            Throw "*The List is empty*" 
        }
    }

   [void] Push([int] $var) {
        if ( -not $this.top ){
            $this.top = [Node]::new($var,$null)
        }else{
            $this.top = [Node]::new($var,$this.top)     
        }  
        $this.count++
    }

    [Int]Pop() {
        if ($this.top){
            $suppnode=$this.top.data
            $this.top=$this.top.next
            $this.count--
            return $suppnode 
        }else{
            Throw "The list is empty"
        }
    }

   [void] Reverse() {
        $count=$this.count
        for ($i=0, $i -le $count, $i++ ){
            $list += $this.pop
        }
        for ($i=0, $i -le $count, $i++ ){
            $this.Push($list[i])
        }
    }

   [array] ToArray() { 
        foreach ($elt in $this.Reverse()){
            $array+=$elt.data
        }
        return $array
    }
}```
hybrid verge
#

Please share error messages as text. Please do not use images to share errors.

agile pasture
hybrid verge
#

+cc @unique minnow

unique minnow
#

hm, i just run it with my examplar code and it passed everything

#

@agile pasture have you tried it again? how many times did you try and run the test?
i havent read your code yet btw, but that is a very unusual error message imply something went wrong in the background, but everything worked here for me

agile pasture
#

yes but it still not working, i got the same issue for an other exercice too, i think i missunderstand something ( i tried the other solution too and they worked )

unique minnow
#

i got the same issue for an other exercice too. i tried the other solution too and they worked )
This is too vague to understand
what other exercises dont work? and what other solution that you got that worked?

agile pasture
#

i had tried a Community solution and it worked

unique minnow
#

I think i see what's going on now. THe test suite failed to skip the last 3 tests properly I think

#

because those are extra test that require implementation of IEnumerator

unique minnow
agile pasture
#

yes

#

and the code look like mine

unique minnow
#

well, yeah, then that's what going on maybe. it only suppose to pass 17 of them, the last 3 are optionals

#

so was your solution a copy from community?

agile pasture
#

yes i change to see

unique minnow
#

a change to see?

#

i mean did you implement and try to solve the exercise at all steps by steps yourself and see which test fail one by one so you can fix it. or did you just copy a solution from the community somehow and changed it and it now didnt work

agile pasture
#

i tried to solve it step by step

unique minnow
#

ok. this seem to be a problem from pester. imma need some time to figure out why it failed wholesale like this and not just individual test
atm you should probably take a look at your reverse and toarray method

#
[array] ToArray() { 
        foreach ($elt in $this.Reverse()){
            $array+=$elt.data
        }
        return $array
    }

reversed() return [void] so using foreach make no sense here

#

even if it somehow work, it still wrong here at the end because you didn't init the array properly
in your node structure you declare the value for Node.data as int, if you simply array += them like that out of thin air, it will just became an int value at the end because array will default to be 0

agile pasture
#

ok i change it

unique minnow
#

if that community solution work, it's worth looking at their code and see what's going on

agile pasture
#

the moment when all the test fail was when i implemented size and head

unique minnow
#

well yeah your constructor is also incorrect

#

each constructor is indepedent to each other

#
    LinkedList() {
        $this.top = [Node]::new()
        $this.count = 0
    }

    Linkedlist ( [int[]] $value){
        foreach ($elt in $value){
            $this.Push($elt)
        }
    }

actually both of these missing the init value

#

sometimes it work out because powershell is smart enough to assign default value, but it is a bad idea

#
if ($this.top -ne $null){

this is also an old pattern, correct way to do is make sure $null is on the left side of the comparison

unique minnow
#

because the param for them is different
you are telling the class how to contruct an instance based on how many args it is taking in

agile pasture
#

i call Push

unique minnow
#

so based on how many param you are providing the constructor will act differently

#

yes, you called push
and this is your push

[void] Push([int] $var) {
        if ( -not $this.top ){
#

it can't make sense what it should do, because you declare again top should be a node class type, but now you just do a -not comparison like it is boolean

#

when you start using class, things need to be more precise in term of init and data type because it is .NET behind the scene

#

it can't just be as loose like the usual powershell experience, (but tbh you should also be strict with the type and init as always to avoid these pitfalls later on )

agile pasture
unique minnow
#

ok. when a linked list got created from scratch. what is its head?

agile pasture
#

for me it $null

unique minnow
#

yes, then what should happen is you assign the head to be null

#

and then again compare $null from left side to the head later on when you need to check head

#

did you do that for your constructors?

agile pasture
#

When I call the constructor, if there is no head, it creates a node that points on $null. Then, in the constructor, if the array is not unitary, the loop continues, and this time the previous nodes no longer point to null but to the newly created node

#

it how i see that

unique minnow
#

just so we are clear, there is always a head variable when any contructor got call, because that is a define feature of a linked list

#

what im saying is that the value of that head should be assigned to null and not any node at first

agile pasture
#

And I use push to implement this function all at once

unique minnow
#

because the head just got created without any value assigned to it

agile pasture
#

ahh you mean the "-not this.top" is nt correct ?

unique minnow
#

you have to use $null -ne $this.top after assigned $this.top = $null in contructors at first

#

i gotta go and eat lunch now, your code is at the right direction it just need some tweaking. you could consul the community solution again to see where the differences lie