Mostafa_M
Mostafa_M

Reputation: 105

Trouble with own C++ list class implementation (insert function)

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

Upvotes: -1

Views: 173

Answers (2)

Mostafa_M
Mostafa_M

Reputation: 105

 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.

Upvotes: 0

Reviews Alot
Reviews Alot

Reputation: 19

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

Upvotes: -1

Related Questions