Hello,
I'm very glad to write this blog.
now, i will implementing a singly linked list with a c++ language.
let's go.
source code:
#include <iostream> using namespace std; class node //a node in a list { int data; // item value node* next; // link a node friend class List; //provide class List access }; class List // class linked list { private: node* head; // pointer to the head of a list public: List() { head=NULL; //constractor } ~List() //destractor { while(!isEmpty()) { remove_front(); } } bool isEmpty() { return head==NULL; //is empty! } int get_front_item() { return head->data; //get front item } void add_front(int n) { node* temp=new node; temp->data=n; temp->next=head; head=temp; //add item from front } void remove_front() { node* temp=head; head=temp->next; delete temp; //remove item from front } }; int main() { List L; // object from class List L.add_front(1); L.add_front(2); L.add_front(3); // insert 5 element L.add_front(4); L.add_front(5); cout<<L.get_front_item()<<endl; // should print number 5 L.remove_front(); // now I remove number 5 cout<<L.get_front_item()<<endl; // should print number 4 return 0; }
No comments:
Post a Comment