Search This Blog

C++: Singly Linked List using Template (Data Structure)

A linked list is a data structure in which the objects are arranged in a linear order. Unlike an array, in which the linear order is determined by the array indices, the order in a linked list is determined by a pointer in each object. It has a group of nodes which together represents a sequence. Under the simplest form, each node is composed of data and a reference (in other words, a link) to the next node in the sequence. Adavantages:➧ Linked lists are a dynamic data structure, which can grow and be pruned, allocating and deallocating memory while the program is running. ➧ Insertion and deletion node operations are easily implemented in a linked list.➧ Dynamic data structures such as stacks and queues can be implemented using a linked list.➧ There is no need to define an initial size for a linked list. ➧ Items can be added or removed from the middle of list. ➧ Backtracking is possible in two way linked list.

We have used LinkedList(const LinkedList<T> & ll) = delete;andLinkedList& operator=(LinkedList const&) = delete;Because they don't allow compiler to default generate it and any invocation of those functions make the program ill-formed (aka compiler will print an error saying the function has been deleted, thus not callable). Purely compile time thing. We have put it there because the class does not implement rule of three, and thus those functions disallow any ownership transfer.Rule of three

We have also written void display(std::ostream& out = std::cout) const and friend std::ostream & operator<<(std::ostream & os, const LinkedList<U> & ll);means the friend declaration here firstly declares a non-member function, and makes it friend of the class, meaning it can access the private and protected members of the class LinkedList. Note: The standard way of printing things in C++ is to use operator<< so we can print a list using it.

Comments

Post a Comment

Popular Posts

Computer programming is one of the fastest growing field. If you are a programmer then you can be hired by a top tech company or you can also work as a freelancer and earn decent amount. So the question arises, how to learn to code ? Initially it was very difficult to learn programming. As there was no internet and you have to relay on books or your colleagues’ code. Now you can find tons of material on the internet. You can learn any computer programming language you want. I am going to tell you some sites which you should visit regularly.

Huffman Code compress data very effectively: saving of 20% to 90% are typical, depending on the characteristics of the data being compressed.Huffman's greedy algorithm uses a table giving how often each character occurs (i.e., its frequency) to build up an optimal way of representing each character as a binary string. This algorithm constructs an optimal prefix code called a Huffman Code.

What is namespace?"In general, a namespace is a container for a set of identifiers Namespaces provide a level of direction to specific identifiers, thus making it possible to distinguish between identifiers with the same exact name. " Or According to Wikipedia “In computer programming namespaces are typically employed for the purpose of grouping symbols and identifiers around a particular functionality and to avoid name collisionsbetween multiple identifiers that share the same name.”