hello sir i'm a new user,i just want to ask if you have sample programs which deals on linked list....if possible not the add.edit,delete program

hello sir i'm a new user,i just want to ask if you have sample programs which deals on linked list....if possible not the add.edit,delete program

Here is a simple one. I can't write without classes, anyway.

#include <iostream>

struct listelem {
  char data;//any type here
  listelem* next;
};

class list {
private:
   listelem *h; 
public:
   list() { h=0; }//0 means empty list
   ~list() { release(); }
   void prepend(char c);//add to the start of the list
   void del() { listelem*temp=h;
                     h = h -> next;
                     delete temp; }
   listelem* first() {return h; }
   void print();
   void release();
};

void list::prepend(char c)
{
   listelem *temp = new listelem;
   
   temp -> next = h;
   temp -> data = c; h = temp;
   h = temp;
}

void list::print() {
  listelem *temp = h;

  while(temp != 0) {
      cout<<temp -> data << " -> ";
      temp = temp->next;
  }
  cout<<"\n###\n";
}

void list::release()
{
   while(h!=0)
       del();
}

If you want, I can post difficult one with templates.

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