0

So I have the following structure:

typedef struct listElement
{
   element value;
   struct listElement;
} listElement, *List;

element is not a known type, meaning I don't know exactly what data type I'm dealing with, wether they're integers or or floats or strings. The goal is to make a function that eletes the listElements that are redundant more than twice (meaning a value can only appear 0 times, once or twice, not more) I've already made a function that uses bruteforce, with a nested loop, but that's a cluster**** as I'm dealing with a large number of elements in my list. (Going through every element and comparing it to the rest of the elements in the list) I was wondering if there was a better solution that uses less isntructions and has a lower complexity.

Turismo98
  • 149
  • 1
  • 1
  • 13
  • 4
    The structure declaration is invalid. I think you mean the data member struct listElement; *next; – Vlad from Moscow Mar 06 '18 at 12:02
  • If you have to validate elements against the list, then it sounds like you need a BST, not a linked list. – Mark Benningfield Mar 06 '18 at 12:06
  • 1
    `struct listElement *next;` Maybe?? (since it is a list...) For `*List` you will want to review: [Is it a good idea to **typedef** pointers?](http://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers). – David C. Rankin Mar 06 '18 at 22:53

1 Answers1

2

You can use a hash table and map elements to their count.

if hashTable[element] (count for this particular element) returns 2, then delete the current element.

Pavel
  • 1
  • 3
  • 17
  • 51
  • AFAIK map is exclusive to C++, I'm working on a program that uses pure C. – Turismo98 Mar 06 '18 at 22:47
  • @Turismo98, I was not talking about std::map, I was talking about mapping one type of elements to another with a hash table. Hash table is a standard data structure in computer science and can be implemented in any language. Look up on the internet how you can implement a hash table, it is not that hard. – Pavel Mar 07 '18 at 07:00