Linked Lists using C++
Submitted by moazkhan on Tuesday, August 12, 2014 - 01:53.
Linked Lists using C++
In this tutorial, you will learn 1. Different operations of linked list. 2. Implementation of linked list. 3. Why some of the basic functions are available What are the different operations in linked list? Following are the operations linked list is supposed to perform. 1. Traversal: There must be a way to access the elements of a linked list. In linked list, we have data and a pointer pointing to next element. We start from first element and using the pointer pointing to next element we go to the next element and keep doing it until we reach the desired element. 2. Insertion: As linked list is a data structure so its task is obviously to store data. You make a function inert which has to insert data. 3. Deletion: In arrays, deleting data requires a lot of resources. Fortunately, in linked list this can be done by simply changing pointers. 4. Destructor: Making a destructor of linked list is perhaps mandatory. Because frequently there is need to delete . So to free any space already used by list, it must be freed by destructor. 5. Check empty: Check empty function checks if the linked list is already empty. It has its uses in other functions. E.g. in traversal if linked list is empty, it stops the traversal. 6. Copy Constructor: Making a copy constructor is difficult for linked list but it is required when you make a function which takes a linked list as input also when you want to return a linked list. What is implementation of a linked list? First, we should make a node.- struct Node
- { int data;
- Node*next;
- Node()
- {next=NULL;}
- };
- class Linklist
- {
- private:
- Node* head;
- bool checkempty();
- public:
- Linklist();
- Linklist(const Linklist &L);
- void Traverse();
- void InsertAtStart(int val);
- void RemoveFromStart();
- void InsertAtEnd(int val);
- void RemoveFromEnd();
- ~Linklist();
- };
Comments
Add new comment
- Add new comment
- 369 views