Posts

Showing posts from 2010

Merging of linked lists

Image
In this article ,I will be discussing some problems based on merging of linked lists. Problem statement : We had 2 linked lists - a1-a2-a3.....aN and b1-b2-b3..........bN We want to merge it into a single linked list - a1-b1-a2-b2-a3-b3...........an-bn Solution : We would be using the linked list we had already developed in previous blogs. I am again showing the structure of this list below : There would be a simple function in this list which takes 2 nodes and merges them both into the main linked list.The given function is : The simple strategy here is to iterate through the 2 lists and keep on adding current node of of each list to the original linked list. Below is a test program to test it. One variation of the above problem could be to write a function in LinkedList class which accepts a linked list and merges it with itself. Problem statement : Merging of 2 sorted linked lists. Given 2 sorted single linked lists - first : 1-2-3-4-5-8-100-120 second : 5-7-11-34...

Some basic linked list problems

Image
In my earlier  post  , we had seen how to implement a basic linked list in C#. We also saw both stack and queue based additions to the linked list. In this post we are going to look at some of the basic linked list problems which will prepare us for more adventure ahead.We will start with basic node class. We would keep adding functions in the LinkedList class as we program. Right now the list is empty with only 2 nodes- head and current which both point to null.Below I am giving the code for simple insertion and printing of nodes,once they are added. Below I am giving code for some functions which can be added to this class. These are : Node Find Find a node in the list.It returns the node when it is found.If the corresponding node is not found null is returned. The next problem is to remove a node from the list. Here we would create a small helper function which would return a previous node. The given functions are below : The next problem is Reversing a list . ...

Simple linked list implementation in C#

Image
Linked list is the most basic data structure taught in the colleges.Though it looks simple,the kind of complex problems we can solve through it is just amazing.In languages like C,C++ its very easy to understand though pointers.Unfortunately or fortunately in C#,we don't need pointers to learn about linked list though internally,its all pointers all the way to the memory addresses.In this post I will try to explain how a link list can be implemented in C#. Ok,enough of technical jargon.Lets come straight to the point. Definition : A linked list is a simple chain of nodes where a node points to next node and so on till the next node doesn't points to anything. Now what is a node? A node is a simple object which contains some data and a reference to next node.Lets code both our node and linked list class. The Node class is self explanatory. As we can see the Linked List class just contains a head node and all the nodes are added to this node. A little bit of explanat...