Thread: LinkedList operator= and deep copy

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by sethjackson View Post
    http://www.perlmonks.org/?node_id=227909

    O(n * n) = O(n2) which would be quadratic.

    I don't know the superscript tag for this board LOL.
    But why would you square n? You're only doing each operation once.

    From what I saw on that site:
    Remove all nodes = O(n)
    Add new nodes = O(m)
    O(n) + O(m) = O(n + m)

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    96
    Quote Originally Posted by cpjust View Post
    But why would you square n? You're only doing each operation once.

    From what I saw on that site:
    Remove all nodes = O(n)
    Add new nodes = O(m)
    O(n) + O(m) = O(n + m)
    Clearing is a linear operation. Linear = O(n)
    Cloning the nodes is also Linear because you have to iterate through each node of the other list.

    O(N2) says that the algorithm's performance is proportional to the square of the data set size. This happens when the algorithm processes each element of a set, and that processing requires another pass through the set. The infamous Bubble Sort is O(N2).

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by sethjackson View Post
    Clearing is a linear operation. Linear = O(n)
    Cloning the nodes is also Linear because you have to iterate through each node of the other list.
    That would be true if you add all elements of List B to List A for each element in List A that you remove.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template and friend class
    By black_spot1984 in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2008, 05:50 PM
  2. Deep and Shallow Copy
    By peckitt99 in forum C++ Programming
    Replies: 2
    Last Post: 05-13-2007, 07:28 AM
  3. In deep copy
    By Belzebuts in forum C Programming
    Replies: 1
    Last Post: 02-05-2006, 11:58 AM
  4. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2005, 11:55 PM
  5. Copy constructors and operator=()
    By filler_bunny in forum C++ Programming
    Replies: 13
    Last Post: 08-25-2003, 07:43 AM