i have this program and i need to count how many times each number is repeated and then delete the repeated i dont how to count them and then delete them can someone help??

#include <iostream>

using namespace std;

struct node 
{
  int item;
  node *next;
};

struct node *push_front ( node *cur, int item )
{
  node *p = new node;

  p->item = item;
  p->next = cur;
  cur = p;

  return cur;
}


int main()
{
  node *cur = 0;
  node *save;

  
  for ( int i = 0; i < 10; i++ )
    cur = push_front ( cur, rand() % 5 + 1 );
  
 
  while ( cur != 0 )
  {
    save = cur->next;
    cout<< cur->item <<' ';
    delete cur;
    cur = save;
	
 
  
  }

  cout<<'\n';
}

Recommended Answers

All 2 Replies

http://www.daniweb.com/techtalkforums/showthread.php?t=30332
Read it (specifically, read my last post with a lot of commented code). Pay particular attention to the cleanup automation, and look at how I use the transverse pointer. You can use the same method to delete specific nodes.

If you always set the 'next' pointer to 'NULL' on creating a node, you'll always know where the end is, and you can easily transverse (that is, move through) the entire linked list and count each number as it occurs.

Then, you've only got numbers 1-5. Do you know what

int num_Tally[5];

means?
Or, you could just have five ints, one for each number, and increment them as you move from head to tail.

This is a simple problem; don't torture yourself or give up too quickly. If you must, draw pictures first of three or four nodes of the linked list, how they connect, and what they contain. Then, think of how you want to approach the problem.

In this case, it's going from the head to the end, seeing what integer value each node contains and counting them.
I reccomend you have two transverse pointers, because it will simplify the thought process for deletions.

If you encounter a duplicate number, you increment the appropriate count, point one transverse at the node to be deleted, keep the other transverse at the previous node, and set attach the previous node's "next" pointer to the to-be-deleted node's "next".
This keeps the linked list intact. Once that's done, delete the node, and set your transverse sensibly.

>can someone help??
I think I'm done trying to help you. You seem completely incapable of doing anything on your own.

p.s. The next thread you start with this same question will get deleted.

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.