944,027 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3538
  • C++ RSS
Sep 26th, 2005
0

help with linked lists

Expand Post »
I was wondering if anyone can help me with linked lists???
I have to create a linked list with 10 random numbers rangeing from 1-5 and insert them into a node in the appriote postion of a linearly linked list and display the number of times each number of occurences of each item. and i have to create a function that would delete the replicate items in the linked list and only leave one of the same numbers. and then display the linked list sorted and unique.

I am having trouble starting this out because my teacher sucks and the book is horrible. I have tried help from computer people but it is still not sticking. I am sure if someone can help me get started i can probably get the rest done but i have no idea to get this started because i have only used c++.net and the teacher wants it done in c++6.0 so that is some of the trouble i am having so if someone can help i would be grateful.
heavyc is online now Edit/Delete Message
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
heavyc is offline Offline
15 posts
since Sep 2005
Sep 26th, 2005
0

Re: help with linked lists

Talk about bending over backward to use a linked list when a binary search tree would easily solve the problem.

Let's start by figuring out which part of the task is giving you trouble. Do you know what a linked list is? Have you ever written one? Start by inserting a few numbers in a linked list without trying to get it sorted and without trying to remove duplicates. Post your code when you get that part done and we'll move on to the next stage.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 26th, 2005
0

Re: help with linked lists

Quote originally posted by Narue ...
Talk about bending over backward to use a linked list when a binary search tree would easily solve the problem.

Let's start by figuring out which part of the task is giving you trouble. Do you know what a linked list is? Have you ever written one? Start by inserting a few numbers in a linked list without trying to get it sorted and without trying to remove duplicates. Post your code when you get that part done and we'll move on to the next stage.
The trouble that i am having is how to incorporate the random number generator into a linked list.. and no i have never written a linked list
Reputation Points: 10
Solved Threads: 0
Newbie Poster
heavyc is offline Offline
15 posts
since Sep 2005
Sep 26th, 2005
0

Re: help with linked lists

So, what's the problem with Linked lists? Just don't understand how they work, the code to make one, or something else?

I wrote what I think is a pretty good starter guide to linked lists a while back...

http://www.daniweb.com/techtalkforum...ad.php?t=30332

That should cover the 'what' and 'how', if that's your question.
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Sep 26th, 2005
0

Re: help with linked lists

>The trouble that i am having is how to incorporate the random number generator into a linked list
You already have a random number generator, it's as simple as doing this:
C++ Syntax (Toggle Plain Text)
  1. int i;
  2.  
  3. for ( i = 0; i < 10; i++ )
  4. list = list_insert ( list, rand() % 5 + 1 );
So the random number part isn't a problem.

>and no i have never written a linked list
This is your biggest problem right now. First you need to figure out how to make a linked list, then you need to figure out how to make it sorted. Here's a really big hint:
C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct node {
  5. int data;
  6. struct node *next;
  7. };
  8.  
  9. struct node *push_front ( struct node *list, int data )
  10. {
  11. struct node *p = malloc ( sizeof *p );
  12.  
  13. if ( p != NULL ) {
  14. p->data = data;
  15. p->next = list;
  16. list = p;
  17. }
  18.  
  19. return list;
  20. }
  21.  
  22. int main ( void )
  23. {
  24. struct node *list = NULL;
  25. struct node *save;
  26. int i;
  27.  
  28. for ( i = 0; i < 10; i++ )
  29. list = push_front ( list, rand() % 5 + 1 );
  30.  
  31. while ( list != NULL ) {
  32. save = list->next;
  33. printf ( "%d ", list->data );
  34. free ( list );
  35. list = save;
  36. }
  37.  
  38. printf ( "\n" );
  39.  
  40. return 0;
  41. }
This code inserts each new number onto the front of the list. It's a simple matter to walk through the list and find the sorted location. See if you can do it.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 26th, 2005
0

Re: help with linked lists

Quote originally posted by Narue ...
>The trouble that i am having is how to incorporate the random number generator into a linked list
You already have a random number generator, it's as simple as doing this:
C++ Syntax (Toggle Plain Text)
  1. int i;
  2.  
  3. for ( i = 0; i < 10; i++ )
  4. list = list_insert ( list, rand() % 5 + 1 );
So the random number part isn't a problem.

>and no i have never written a linked list
This is your biggest problem right now. First you need to figure out how to make a linked list, then you need to figure out how to make it sorted. Here's a really big hint:
C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct node {
  5. int data;
  6. struct node *next;
  7. };
  8.  
  9. struct node *push_front ( struct node *list, int data )
  10. {
  11. struct node *p = malloc ( sizeof *p );
  12.  
  13. if ( p != NULL ) {
  14. p->data = data;
  15. p->next = list;
  16. list = p;
  17. }
  18.  
  19. return list;
  20. }
  21.  
  22. int main ( void )
  23. {
  24. struct node *list = NULL;
  25. struct node *save;
  26. int i;
  27.  
  28. for ( i = 0; i < 10; i++ )
  29. list = push_front ( list, rand() % 5 + 1 );
  30.  
  31. while ( list != NULL ) {
  32. save = list->next;
  33. printf ( "%d ", list->data );
  34. free ( list );
  35. list = save;
  36. }
  37.  
  38. printf ( "\n" );
  39.  
  40. return 0;
  41. }
This code inserts each new number onto the front of the list. It's a simple matter to walk through the list and find the sorted location. See if you can do it.
what do you mean find the sorted location are you saying that it is already in here because I compiled it and it says that in line 11 that it can't concert from void * to struct node * i dont know what this means. i cant believe the trouble that i am having with this i did not think that this was gunna be this hard...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
heavyc is offline Offline
15 posts
since Sep 2005
Sep 26th, 2005
0

Re: help with linked lists

>I compiled it and it says that in line 11 that it can't concert from void * to struct node *
Well, we've now established that you're using C++ and not C. You should mention that explicitly in the future. Use this code instead:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct node {
  6. int data;
  7. node *next;
  8. };
  9.  
  10. struct node *push_front ( node *list, int data )
  11. {
  12. node *p = new node;
  13.  
  14. p->data = data;
  15. p->next = list;
  16. list = p;
  17.  
  18. return list;
  19. }
  20.  
  21. int main()
  22. {
  23. node *list = 0;
  24. node *save;
  25.  
  26. for ( int i = 0; i < 10; i++ )
  27. list = push_front ( list, rand() % 5 + 1 );
  28.  
  29. while ( list != 0 ) {
  30. save = list->next;
  31. cout<< list->data <<' ';
  32. delete list;
  33. list = save;
  34. }
  35.  
  36. cout<<'\n';
  37. }
>what do you mean find the sorted location
Say you have an empty list and you want to insert 5, you go ahead and do the same thing as my code by pushing 5 onto the front of the list. Then say you want to insert 9. You can't push 9 onto the front of the list because that's not sorted (assuming ascending order). So you walk through the list until the next node is the end of the list, or you find a node with a value greater than the new node and stick it there. I'm not going to write this for you, so you'd better get your creativity reflex in gear.

>i did not think that this was gunna be this hard...
Just wait until you get to multidimensional linked data structures.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: new and malloc
Next Thread in C++ Forum Timeline: continued new and malloc





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC