| | |
help with linked lists
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2005
Posts: 15
Reputation:
Solved Threads: 0
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
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
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.
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.
I'm here to prove you wrong.
•
•
Join Date: Sep 2005
Posts: 15
Reputation:
Solved Threads: 0
•
•
•
•
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.
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Solved Threads: 5
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.
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.
>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:
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:
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.
You already have a random number generator, it's as simple as doing this:
C++ Syntax (Toggle Plain Text)
int i; for ( i = 0; i < 10; i++ ) list = list_insert ( list, rand() % 5 + 1 );
>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)
#include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *push_front ( struct node *list, int data ) { struct node *p = malloc ( sizeof *p ); if ( p != NULL ) { p->data = data; p->next = list; list = p; } return list; } int main ( void ) { struct node *list = NULL; struct node *save; int i; for ( i = 0; i < 10; i++ ) list = push_front ( list, rand() % 5 + 1 ); while ( list != NULL ) { save = list->next; printf ( "%d ", list->data ); free ( list ); list = save; } printf ( "\n" ); return 0; }
I'm here to prove you wrong.
•
•
Join Date: Sep 2005
Posts: 15
Reputation:
Solved Threads: 0
•
•
•
•
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:
So the random number part isn't a problem.C++ Syntax (Toggle Plain Text)
int i; for ( i = 0; i < 10; i++ ) list = list_insert ( list, rand() % 5 + 1 );
>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:
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.C++ Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *push_front ( struct node *list, int data ) { struct node *p = malloc ( sizeof *p ); if ( p != NULL ) { p->data = data; p->next = list; list = p; } return list; } int main ( void ) { struct node *list = NULL; struct node *save; int i; for ( i = 0; i < 10; i++ ) list = push_front ( list, rand() % 5 + 1 ); while ( list != NULL ) { save = list->next; printf ( "%d ", list->data ); free ( list ); list = save; } printf ( "\n" ); return 0; }
>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:
>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.
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)
#include <iostream> using namespace std; struct node { int data; node *next; }; struct node *push_front ( node *list, int data ) { node *p = new node; p->data = data; p->next = list; list = p; return list; } int main() { node *list = 0; node *save; for ( int i = 0; i < 10; i++ ) list = push_front ( list, rand() % 5 + 1 ); while ( list != 0 ) { save = list->next; cout<< list->data <<' '; delete list; list = save; } cout<<'\n'; }
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.
I'm here to prove you wrong.
![]() |
Similar Threads
- strcpy and linked lists (C++)
- Concordance, Linked Lists and classes (C++)
- Singly-Linked Lists: Ultimate (C++)
- Bug when creating linked lists in Dev C++ (C++)
- Linked Lists (C)
- C/ Need Help with Linked Lists please (C)
- stack of linked lists (C++)
Other Threads in the C++ Forum
- Previous Thread: new and malloc
- Next Thread: continued new and malloc
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






