-1

I'm about to implement my own elementary data structures. Here is a class of list. I'm in struggle to fix the insert(int data) .

Idea: each element has a value and 3 pointer: pointer1: head: Points to the head cell pointer2: current: Points to current cell pointer3: next: Points to the structure of neighbor element

I already tried next = &date whenever we put a new element into the list. enter code here`

  class list{
  private:
      typedef struct element{
          int wert;
          element *next;};
          element *current;
          element *head;
  public://constructur. 
      list()
          {head=new element; current=head; head->next=0;}

/*A new element with value is beeing inserted */

void insert (int value){
    next= &value;} 
};
Mostafa_M
  • 103
  • 5
  • Remember, if you allocate with `new` you're responsible for the corresponding `delete`. – tadman Jul 27 '19 at 00:21
  • In C++ use `nullptr` instead of legacy things like `NULL`, or worse, `0`. – tadman Jul 27 '19 at 00:22
  • Hint: Convert `value` into an `element*` by using `new`. You're trying to assign an `int*` into an `element*`. This linked list is also missing a place to put the actual value. – tadman Jul 27 '19 at 00:22
  • There is no `next` member in your class. What exactly about this obvious compilation error is unclear to you? – Sam Varshavchik Jul 27 '19 at 00:22
  • When you got the instruction to do this, did you also get the tools to debug it? This can get terribly corrupted if you don't take precautions. – Ted Lyngmo Jul 27 '19 at 00:31
  • I did not get any instructions. I have Tools to debug it.just Need an idea based on mine. – Mostafa_M Jul 27 '19 at 00:42
  • @Mostafa_M My advice is to get an already working, debugged, and easy to follow linked list class written in C++, and study how it's put together. Trying this off the top of your head, ad-hoc coding, or "winging it" is not the way to understand how to properly put together such a class. – PaulMcKenzie Jul 27 '19 at 01:11
  • What is there to fix? (What symptoms are you seeing that tell you something is not working?) – JaMiT Jul 27 '19 at 04:32
  • Question is: How can I create a new element everytime when I insert a value. I'm new in Programming c++ structures. Please help ! there are a lot of Solutions but I try to implement it by my own way. – Mostafa_M Jul 29 '19 at 00:16

2 Answers2

0
 void insert (int value){
    struct element* new_node = (struct element*) malloc(sizeof(struct element));
    new_node->wert = value;
    new_node->next = head;
    head = new_node;
    }

Now it works.

Mostafa_M
  • 103
  • 5
  • 1
    Note: `struct element* new_node = (struct element*) malloc(sizeof(struct element));` could be `element* new_node = new element;` Much simpler and catches memory allocation errors for you that are currently being unchecked. In general, prefer `new` over `malloc` in C++ and [where possible don't use either](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new). Linked list is one place where I believe `new` can be suitable, but the `list` class needs to be smart enough to ensure clean-up is performed correctly. – user4581301 Jul 30 '19 at 02:43
-1

You are mixing together the list structure with the node structure. For the list you need something like this :

class list { 
    node* head;
}

Then for the notes in the list you need

class node {
    element* currentData;
    node* next;
}

So here the nodes make up the list structure while the data being stored in the list has type 'element'.