hey guys ! i am unable to understand how to implement the function insert at head and insert at tail ..

class Book {
public:
    char * title;
    float price;
    // constructor for class Book
    Book ()
    {
        char* title = new char[20];
        strcpy(title,"untitled");
        price = 0 ;
    }
    void SetPrice(float p)
    {
        price = p ;
    }
    float GetPrice ()
    {
        return price ;
    }
    void SetTitle (char ch[] )
    {
        strcpy(title,ch );
    }
    char GetTitle ()
    {
        return *title ;
    }
    ~Book ()
    {
        delete[] title ;
    }

};

class Node {
public:
  Book s;
  Node * next;
  // constructor for class Node
  Node ()
  {
      s.SetPrice(0);
      s.GetPrice();
      s.SetTitle("untitled");
      s.GetTitle();
      next = nullptr ;

  }
  void setNodeBook (Book b)
  {
      s.SetPrice(b.price);
      s.SetTitle(b.title);
  }
  void setNodeNext(Node *n)
  {
      n = next ;
  }
};

class List {
private:
   Node * head;
   Node * tail;
public:
 // Add getters and setters here

    List()               // constructor for a list
    {
        head = nullptr ;
        tail = nullptr ;
    }
    void SetHead (Node *h)
    {
        head = h ;
    }
    void SetTail (Node *t)
    {
        tail = t ;
    }
    Node* GetHead ()
    {
        return head ;
    }
    Node* GetTail ()
    {
        return tail ;
    }
   void InsertAtHead(Book s)
   {
       Node* n = new Node ;
       if (n == nullptr )
       {
           cout << " out of memory " << endl;
           exit(1);
       }
       else
       {
           head->s.SetPrice(s.price);
           head->s.SetTitle(s.title);
           n->next = head ;


       }
   }
   void InsertAtTail(Book s)
   {
       Node* n = new Node ;
       if (n == nullptr)
       {
           cout << " out of memory " << endl;
           exit(1);
       }
       else
       {
           tail->s.SetPrice(s.price);
           tail->s.SetTitle(s.title);
           n->next = tail ;
       }
   }
   Book RemoveFromHead()
   {


   }
   Book RemoveFromTail();
   Book ViewBookFromHead();
   Book ViewBookFromTail();
   void Print();
   //Add Your function here
};

Normally you would have an insert function which takes care of head node. Not sure why would you want to insert at tail directly. Also you can have insert_at or insert_after, insert_before functions which also handle adding a node to an empty list. Take a look at this tutorial here Linked List Tutorial

Edit: Forgot to mention that you don't need to check for NULL ptr after New. If it fails to allocate memory it will throw a bad_alloc exception . And also don't use exit in the middle of your code

Edited 3 Years Ago by Agni

void insertAtHead(void* p)
{
    void* ptmp = head;
    head = p;
    p->next = ptmp;
}
void insertAtTail(void* p)
{
    void* ptmp = tail;
    ptmp->next = p;
    tail = p;
}

Of course, this is the simplified view. If you don't have a pointer to the tail member, then you need to walk the list from the head to find it (where p->next == NULL).

This article has been dead for over six months. Start a new discussion instead.