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

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.

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

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/techtalkforums/showthread.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:

int i;

for ( i = 0; i < 10; i++ )
  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:

#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;
}

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.

>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:

int i;

for ( i = 0; i < 10; i++ )
  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:

#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;
}

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...

>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:

#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';
}

>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. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.