Linked List using C++ part 2
Submitted by moazkhan on Wednesday, August 13, 2014 - 17:11.
Data Structure using C++
In this tutorial, you will learn implementation of following functions 1. Constructor 2. Checkempty() 3. Traversal 4. Insertion at beginning Along with this tutorial, C++ code is given which has structure node and class Linklist containing these codes with a main to help you in understanding how these functions are being used. What is the code of constructor? The code of the constructor utilizes the fact that we want to make a check empty function which returns 1 if linked list has no element and 0 if it has elements. So we simply make the head point to NULL. It means that if a new linked list is made, its head points to NULL and this can be used for checkempty()- Linklist()
- {
- head=NULL;
- }
- Bool checkempty()
- { if (head==NULL)
- return 1;
- else
- return 0;
- }
- void Traverse()
- {
- if (checkempty())
- {
- cout<<"List is empty";
- }
- else
- {
- Node * curr;
- curr=head;
- while(curr!=NULL)
- {
- cout<<curr->data;
- cout<<endl;
- curr=curr->next;
- }
- }
- }
- void InsertAtStart(int val)
- {
- Node *nnode=new Node;
- nnode->data=val;
- nnode->next=head;
- head=nnode;
- }
Add new comment
- 1069 views