need help with singly linked-list

This is a discussion on need help with singly linked-list within the C++ Programming forums, part of the General Programming Boards category; Hello, i dont know where is the problem. i should input some data in the linked-list. when i put 2 ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    29

    need help with singly linked-list

    Hello,
    i dont know where is the problem.
    i should input some data in the linked-list.
    when i put 2 person,the result just show the last person.

    can someone help me to solve this prob ?

    this is my code :

    Code:
    REMOVED
    Last edited by vearns; 04-08-2008 at 12:41 PM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Hm?
    Code:
    system("CLS");
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    29
    oo yeah..
    my fault.
    hahahahaha.
    thanks.

    can someone show me how to list out person who have salary less than 100 ?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Can't you just go through the list (like your code currently does) and then use an if statement to check the salary and only output if the salary is less than 100?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Give it a go, and if you can't solve it yourself, post your attempt.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    29
    ok,
    here is my code

    Code:
    REMOVED
    the compiler dont show any error.
    but when i test the program,it crash.
    maybe stuck in the loop or something ?
    Last edited by vearns; 04-08-2008 at 12:40 PM.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you want to list ONLY those who are under 100 in salary, perhaps you should put the if-statement inside your loop.

    If you want to first print ALL entries, then only the ones under 100, then you need two loops.

    Your current code crashes, because temp3 is NULL when the loop is done.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    29
    Code:
    REMOVED
    ok,
    i put inside the loop and it still crash.
    Last edited by vearns; 04-08-2008 at 12:40 PM.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your if-statement is after the "next" step, which means on the last iteration, it's going to access NULL->salary, which is an invalid memory location.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    29
    ok mats,
    i found the solution,
    i put it like this

    Code:
    REMOVED
    however,it seems not so good if we have more than 1 person that have less than 100 salary and not a great solution since the person who have salary less than 100 always appear before NAME,SALARY...etc.

    maybe you can give me some other ideas ?
    Last edited by vearns; 04-08-2008 at 12:40 PM.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    What part of that code is outputting the person information?

    When you find that code, put it inside the if block since you only want the output to happen if the if statement is true.

  12. #12
    Registered User
    Join Date
    Dec 2005
    Posts
    29
    OK,i did a lot of changes in my code.
    thanks to mats who give the idea to me.

    here is my code

    Code:
    REMOVED
    now i faced with 2 more problems.

    1.for Person with salary less than 100 and not in SALES DEPMNT,the system should display only person who do not work under SALES DEPMNT.but i dont know why the system show the person with SALES department also.where is the problem ?

    2.how to make the system can calculate how much average salary to person who join after 1 Jan 2008 ? i declared date as string.i dont think system know it is a date.need some idea from u.
    Last edited by vearns; 04-08-2008 at 03:07 PM.

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    The problem is
    Code:
    REMOVED
    and to fix this you'll need to be
    Code:
    REMOVED
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Your code still doesn't look quite right. It is moving to the next value only in the if block.

  15. #15
    Registered User
    Join Date
    Dec 2005
    Posts
    29
    LOL. REMOVED ?
    hahahaha

    i removed old codes.
    and i put my new codes.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 01:48 PM
  2. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21