Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Recursive function to display the number of nodes of a singly connected list?

Recursive function to display the number of nodes of a singly connected list?

Scheduled Pinned Locked Moved C#
algorithmsquestion
12 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E ektoras

    Could someone give me an algorithm of a recursive function to display-NOT TO RETURN-the number of nodes of a singly connected list?

    J Offline
    J Offline
    J4amieC
    wrote on last edited by
    #2

    I am probably misunderstanding your question, but any implementation of a List (ArrayList, LinkedList etc) should expose a Count property, and so your question of how to display this is down to where you want to display it. on the console: Console.WriteLine(myList.Count); in a messageBox MessageBox.Show(miList.Count.ToString()); sorry if ive misunderstood ;)

    E 1 Reply Last reply
    0
    • J J4amieC

      I am probably misunderstanding your question, but any implementation of a List (ArrayList, LinkedList etc) should expose a Count property, and so your question of how to display this is down to where you want to display it. on the console: Console.WriteLine(myList.Count); in a messageBox MessageBox.Show(miList.Count.ToString()); sorry if ive misunderstood ;)

      E Offline
      E Offline
      ektoras
      wrote on last edited by
      #3

      This is not a C# question but an algorithm question for my homework at university.

      W J 2 Replies Last reply
      0
      • E ektoras

        This is not a C# question but an algorithm question for my homework at university.

        W Offline
        W Offline
        Wjousts
        wrote on last edited by
        #4

        Perhaps you should do you own homework? You learn a lot more that way. This isn't a difficult question.

        E 1 Reply Last reply
        0
        • E ektoras

          This is not a C# question but an algorithm question for my homework at university.

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #5

          while I Agree with wjousts assertion that a) this is not a particularly difficult question and b) you should do your own homework ;) - I will provide some assistance. You must know what a linked list is, if not go back and refer to your course notes. Recursion you probably also know about - essentially calling a function from itself (or put another way "to understand recursion, you must first understand recursion /geekhumour) So, to help write this algorithm, I find it easy to start off in pseudo-code

          while {not at end of list)
          Incrememnt counter
          loop
          return counter

          Now, I know you super-highlight-stressed that you want it DISPLAYED and not RETURNED!!11 (your homework said display it right?) however in order to dispaly something...it must first be returned from somewhere. Turning the above into a "recursive function" is the bit you've got to do. If this is not enough to get you going on your homework, then seriously consider whether you should be studying a subject you cannot do! Good luck.

          E 1 Reply Last reply
          0
          • W Wjousts

            Perhaps you should do you own homework? You learn a lot more that way. This isn't a difficult question.

            E Offline
            E Offline
            ektoras
            wrote on last edited by
            #6

            It is absolutely sure that I can not do that, although I have tried very much. Any idea?

            W 1 Reply Last reply
            0
            • E ektoras

              It is absolutely sure that I can not do that, although I have tried very much. Any idea?

              W Offline
              W Offline
              Wjousts
              wrote on last edited by
              #7

              What have you tried? Show people that you are thinking about this and not just too lazy to do your homework and you'll find people are much more responsive. You need to write a recursive function, something like this (you fill in the blanks):

              public int CountNodes(Node n)
              {
                  if(ExitCondition)    // what's your exit condition?
                  {
                      return ?;        // what should you return for your base 
              case?
                  }
                  else
                  {
                  // what do you need to return here. What are you going to pass to the recursive call?
                      return ? + CountNodes(?);     
                  } 
              }
              
              1 Reply Last reply
              0
              • J J4amieC

                while I Agree with wjousts assertion that a) this is not a particularly difficult question and b) you should do your own homework ;) - I will provide some assistance. You must know what a linked list is, if not go back and refer to your course notes. Recursion you probably also know about - essentially calling a function from itself (or put another way "to understand recursion, you must first understand recursion /geekhumour) So, to help write this algorithm, I find it easy to start off in pseudo-code

                while {not at end of list)
                Incrememnt counter
                loop
                return counter

                Now, I know you super-highlight-stressed that you want it DISPLAYED and not RETURNED!!11 (your homework said display it right?) however in order to dispaly something...it must first be returned from somewhere. Turning the above into a "recursive function" is the bit you've got to do. If this is not enough to get you going on your homework, then seriously consider whether you should be studying a subject you cannot do! Good luck.

                E Offline
                E Offline
                ektoras
                wrote on last edited by
                #8

                My English is not good. But I mean something like that: int count(POINTER head) { return head!=NULL ? count(head->next)+1 : 0 ; } But not to return the value. I WANT TO DISPLAY THE VALUE.(and this is difficult)

                J 1 Reply Last reply
                0
                • E ektoras

                  My English is not good. But I mean something like that: int count(POINTER head) { return head!=NULL ? count(head->next)+1 : 0 ; } But not to return the value. I WANT TO DISPLAY THE VALUE.(and this is difficult)

                  J Offline
                  J Offline
                  J4amieC
                  wrote on last edited by
                  #9

                  Where do you want to display it? In a window? on a command line?

                  E 1 Reply Last reply
                  0
                  • J J4amieC

                    Where do you want to display it? In a window? on a command line?

                    E Offline
                    E Offline
                    ektoras
                    wrote on last edited by
                    #10

                    on a Command Line.

                    J 1 Reply Last reply
                    0
                    • E ektoras

                      on a Command Line.

                      J Offline
                      J Offline
                      J4amieC
                      wrote on last edited by
                      #11

                      Console.WriteLine(myList.Count(myList.FirstNode)) seriously, you couldnt work this out? Swap to a different course while you still can.

                      E 1 Reply Last reply
                      0
                      • J J4amieC

                        Console.WriteLine(myList.Count(myList.FirstNode)) seriously, you couldnt work this out? Swap to a different course while you still can.

                        E Offline
                        E Offline
                        ektoras
                        wrote on last edited by
                        #12

                        It is a Data Structure work J4amieC. Anyway thank you.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups