Linked list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • surender

    Linked list

    Hi,
    Let us think that we have 100 nodes in the single linked list.
    I have only 50th node address and i don't have the first node address
    but i want to print the 49th node data. So how can we print the 49th
    node data.
    Any body Pls help me.

    Thanks in advance
  • Karl Heinz Buchegger

    #2
    Re: Linked list

    surender wrote:[color=blue]
    >
    > Hi,
    > Let us think that we have 100 nodes in the single linked list.
    > I have only 50th node address and i don't have the first node address
    > but i want to print the 49th node data. So how can we print the 49th
    > node data.
    > Any body Pls help me.
    >[/color]

    You can't.
    You need at least the pointer to the first node (usually called
    the 'head of the list')


    --
    Karl Heinz Buchegger
    kbuchegg@gascad .at

    Comment

    • petermcmillan_uk@yahoo.com

      #3
      Re: Linked list

      Unless it was doubly linked with a 'previous' pointer. If he's
      implementing the list himself, then he could possibly add a 'previous'
      pointer.

      Comment

      • Ulrich Achleitner

        #4
        Re: Linked list

        On 4 Jan 2005 02:08:02 -0800, surender <surendrayeluri @rediffmail.com >
        wrote:[color=blue]
        > Let us think that we have 100 nodes in the single linked list.
        > I have only 50th node address and i don't have the first node address
        > but i want to print the 49th node data. So how can we print the 49th
        > node data.[/color]

        reverse traversing is not possible with a single linked list (because
        every node has only pointer to its successor).
        you need to use a doubly linked list to traverse from the e.g. 50th list
        to the 49th

        ulrich

        Comment

        • msalters

          #5
          Re: Linked list


          surender wrote:[color=blue]
          > Hi,
          > Let us think that we have 100 nodes in the single linked list.
          > I have only 50th node address and i don't have the first node address
          > but i want to print the 49th node data. So how can we print the 49th
          > node data.
          > Any body Pls help me.
          >
          > Thanks in advance[/color]

          A circular single linked list can go from the last to the first
          element. In that case, and only in that case, can you from
          50->99->0->49.

          Regards,
          Michiel Salters

          Comment

          • John Carson

            #6
            Re: Linked list

            "surender" <surendrayeluri @rediffmail.com > wrote in message
            news:21e90dfc.0 501040208.1af3b dc5@posting.goo gle.com[color=blue]
            > Hi,
            > Let us think that we have 100 nodes in the single linked list.
            > I have only 50th node address and i don't have the first node address[/color]

            What do you have?
            [color=blue]
            > but i want to print the 49th node data. So how can we print the 49th
            > node data.
            > Any body Pls help me.
            >
            > Thanks in advance[/color]


            --
            John Carson
            1. To reply to email address, remove donald
            2. Don't reply to email address (post here instead)

            Comment

            • KiLVaiDeN

              #7
              Re: Linked list


              "surender" <surendrayeluri @rediffmail.com > a écrit dans le message de
              news:21e90dfc.0 501040208.1af3b dc5@posting.goo gle.com...[color=blue]
              > Hi,
              > Let us think that we have 100 nodes in the single linked list.
              > I have only 50th node address and i don't have the first node address
              > but i want to print the 49th node data. So how can we print the 49th
              > node data.
              > Any body Pls help me.
              >
              > Thanks in advance[/color]

              If the list was built at once, there is chance that by some statistics on
              memory adresses, you can find the 49th node, if you know the position in
              memory of the 50th 51th 52nd 53rd.. But it's very unlikely ! It could be
              undefined data..

              K


              Comment

              • Chris Gordon-Smith

                #8
                Re: Linked list

                surender wrote:
                [color=blue]
                > Hi,
                > Let us think that we have 100 nodes in the single linked list.
                > I have only 50th node address and i don't have the first node address
                > but i want to print the 49th node data. So how can we print the 49th
                > node data.
                > Any body Pls help me.
                >
                > Thanks in advance[/color]

                Erm - Just out of curiosity, what stops you from using a doubly linked list?

                Chris Gordon-Smith
                London

                Comment

                • chris

                  #9
                  Re: Linked list

                  surender wrote:[color=blue]
                  > Hi,
                  > Let us think that we have 100 nodes in the single linked list.
                  > I have only 50th node address and i don't have the first node address
                  > but i want to print the 49th node data. So how can we print the 49th
                  > node data.
                  > Any body Pls help me.
                  >[/color]
                  If this is a programming question, your programming is poor! If this is
                  a homework question, then either you've read it wrong, or it's badly
                  worded :)

                  One possiblility is that the linked list is circular (the last node is
                  attached to the first one). If this is the case you could go all the way
                  around and get to it from the start :)

                  Chris

                  Comment

                  • Taran

                    #10
                    Re: Linked list

                    chris wrote:[color=blue]
                    > surender wrote:[color=green]
                    > > Hi,
                    > > Let us think that we have 100 nodes in the single linked list.
                    > > I have only 50th node address and i don't have the first node[/color][/color]
                    address[color=blue][color=green]
                    > > but i want to print the 49th node data. So how can we print the[/color][/color]
                    49th[color=blue][color=green]
                    > > node data.
                    > > Any body Pls help me.[/color][/color]

                    Simple. No you can't.
                    In a singly linked list given any node address you can traverse only
                    forward, cos the node only gives you the _next node address.

                    You can traverse backwards only if its a doubly linked list.
                    Or else if you have a circular linked list then you can traverse the
                    list counting the nodes and print the 99th node.
                    [color=blue]
                    >KiLVaiDeN wrote:[color=green]
                    >>If the list was built at once, there is chance that by some[/color][/color]
                    statistics on[color=blue][color=green]
                    >>memory adresses, you can find the 49th node, if you know the position[/color][/color]
                    in[color=blue][color=green]
                    >>memory of the 50th 51th 52nd 53rd.. But it's very unlikely ! It could[/color][/color]
                    be[color=blue][color=green]
                    >>undefined data..[/color][/color]

                    You can do this way, but I suggest not.
                    First: There's low probability of you hitting the right address.
                    Second: Second whatever address you land at, you have no clue whether
                    the data there is valid or not in terms of context.
                    HTH.

                    Regards,
                    Taran

                    Comment

                    • Robert B. Clark

                      #11
                      Re: Linked list

                      On 4 Jan 2005 02:08:02 -0800, surendrayeluri@ rediffmail.com (surender)
                      wrote:
                      [color=blue]
                      > Let us think that we have 100 nodes in the single linked list.
                      >I have only 50th node address and i don't have the first node address
                      >but i want to print the 49th node data. So how can we print the 49th
                      >node data.[/color]

                      You don't (unless this is a circular singly-linked list, which you did
                      not stipulate).

                      The usual practice is to always maintain a pointer to the head node of a
                      singly-linked list, or to use a doubly-linked list (nodes include
                      pointers to n-1 as well as n+1).

                      --
                      Robert B. Clark (email ROT13'ed)
                      Visit ClarkWehyr Enterprises On-Line at http://www.3clarks.com/ClarkWehyr/

                      Comment

                      • Flash Gordon

                        #12
                        Re: Linked list

                        petermcmillan_u k@yahoo.com wrote:[color=blue]
                        > Unless it was doubly linked with a 'previous' pointer. If he's
                        > implementing the list himself, then he could possibly add a 'previous'
                        > pointer.[/color]

                        Please include enough of the message you are replying to for people too
                        *see* what you are replying to. There is absolutely *no* guarantee that
                        people reading your message will have seen the message you are replying to.

                        However, in answer to your point (since I happen to have the message
                        handy), what part of "Let us think that we have 100 nodes in the single
                        linked list." do you think allows it to be a doubly linked list. The
                        last time I checked 1 and 2 were different numbers.
                        --
                        Flash Gordon
                        Living in interesting times.
                        Although my email address says spam, it is real and I read it.

                        Comment

                        • infobahn

                          #13
                          Re: Linked list

                          Flash Gordon wrote:[color=blue]
                          >[/color]
                          <snip>[color=blue]
                          >
                          > The
                          > last time I checked 1 and 2 were different numbers.[/color]

                          Let a = b.

                          Then multiply each side by b, so: ab = a^2
                          Now subtract b^2 from each side: ab - b^2 = a^2 - b^2
                          Factorise: b(a - b) = (a + b)(a - b)
                          Divide each side by common term: b = a + b
                          Substitute a (because a = b): b = 2b
                          Divide by b: 1 = 2

                          QED.

                          ;-)

                          Comment

                          • Gary Labowitz

                            #14
                            Re: Linked list

                            "infobahn" <infobahn@btint ernet.com> wrote in message
                            news:41DD8F23.4 2E54F45@btinter net.com...[color=blue]
                            > Flash Gordon wrote:[color=green]
                            > >[/color]
                            > <snip>[color=green]
                            > >
                            > > The
                            > > last time I checked 1 and 2 were different numbers.[/color]
                            >
                            > Let a = b.
                            >
                            > Then multiply each side by b, so: ab = a^2
                            > Now subtract b^2 from each side: ab - b^2 = a^2 - b^2
                            > Factorise: b(a - b) = (a + b)(a - b)
                            > Divide each side by common term: b = a + b
                            > Substitute a (because a = b): b = 2b
                            > Divide by b: 1 = 2
                            >
                            > QED.
                            >
                            > ;-)[/color]
                            Boy, talk about undefined behavior!!! ::))
                            --
                            Gary


                            Comment

                            • Lew Pitcher

                              #15
                              Re: Linked list

                              -----BEGIN PGP SIGNED MESSAGE-----
                              Hash: SHA1

                              infobahn wrote:[color=blue]
                              > Flash Gordon wrote:
                              >
                              > <snip>
                              >[color=green]
                              >>The
                              >>last time I checked 1 and 2 were different numbers.[/color]
                              >
                              >
                              > Let a = b.
                              >
                              > Then multiply each side by b, so: ab = a^2
                              > Now subtract b^2 from each side: ab - b^2 = a^2 - b^2
                              > Factorise: b(a - b) = (a + b)(a - b)
                              > Divide each side by common term:[/color]

                              A quick aside...
                              Assuming

                              a = b (1)

                              then, subtracting b from both sides gives
                              a - b = b - b (2)

                              solving the right hand side gives

                              a - b = 0 (3)

                              Back to your exposition

                              Divide each side by the common term (a - b)
                              is the same as
                              Divide each side by 0 (from (3) above)

                              So, your solution requires division by zero, which invokes "undefined behaviour"
                              in the 'real world' language <grin>
                              [color=blue]
                              > b = a + b
                              > Substitute a (because a = b): b = 2b
                              > Divide by b: 1 = 2
                              >
                              > QED.
                              >
                              > ;-)[/color]


                              - --
                              Lew Pitcher
                              IT Consultant, Enterprise Data Systems,
                              Enterprise Technology Solutions, TD Bank Financial Group

                              (Opinions expressed are my own, not my employers')
                              -----BEGIN PGP SIGNATURE-----
                              Version: GnuPG v1.2.4 (MingW32)

                              iD8DBQFB3Z32agV FX4UWr64RAjtOAK CHWSrbqBPTgC8gA lP1PHQ7Tatx8wCe KNTt
                              nHHELXK7cIsg+iP eq/xKtEU=
                              =F/rF
                              -----END PGP SIGNATURE-----

                              Comment

                              Working...