Need Exercise to solve based on Linear Linked List - c

I have completed implementing Operation of Linear Linked List using C,
Now inorder to test my ability i need to solve some problems based on Linear Linked List, and there you people can help me by suggesting some problems/assignments ...
I think there is nothing wrong in asking this type of help from my community members .

Determine whether the linked list contains cycle or not.
In the circular linked list add new node at the end without traversing the list.
Reverse the list.
Print the nodes in the list in reverse order without reversing the list.
Make a list Circular linked list.
Sort the list.
Merge two sorted lists.

Related

Searching ordered and unordered linked lists

In a program I'm working on for a final project, I've got to implement search functions for ordered and unordered linked lists. In the assignment, it's made clear that there's an expectation for a search function for each type.
I've worked with linked lists in previous classes, I understand the difference between ordered and unordered, but I hit a wall in trying to figure out what the difference would be in searching them. In my mind, both should iterate through the list until the key value is found and then return it. How should these differ?
In case of order linked list time complexity can be reduced while performing search operation. You can implement skip list for this purpose. But if your linked list need to be a singly linked list strictly then there is no difference other than mentioned by #kaylum in his comment

how and why is circular doubly linked lists concatenation of complexity O(1)?

It is given as a fact in a certain standard book that concatenation of circular doubly linked lists is of complexity O(1) and it is not the case with single or doubly linked lists. Can someone please clarify why and how this is possible...
In a circular doubly linked list, you have head->prev pointing to the last element. So, all you have to do is to perform the concatenation using that node's address, so O(1). Whereas, in a simple singly or doubly linked list, you'll have to traverse the list to get the last element and perform the concatenation which looks like O(n) to me.

Queue in normal linked list

I want to merge N ordered linked lists into one ordered linked list.
However, I also want the individual ordered linked list to be retained. I am successful in doing so using an array of Node pointers with each array element as a node corresponding to first node of individual lists. However, with array size fixed, I cannot proceed with merging more than array size.
Now my question is, is there a way I can dynamically change array size. If not I am thinking of using Queue instead to hold the first node of individual lists in a queue linked list. Am I going right or wrong? Please give me some tips regarding the problem.
It sounds like you are looking for realloc, assuming you dynamically allocated your array in the first place.

Can we use Insertion sort on linked list without using any other data structure?

I have gone through following link
http://analgorithmaday.blogspot.in/2011/01/insertion-sort-using-linked-list.html
They are using another array (*arr) to do the sorting.
Is it possible, to do the insertion sort, on the linked list without using any other array or linked list?
Your reading of their algorithm is not correct -- the passed-in array is simply the source of the data and is not used for the insertion sort.
Insertion sort is a good algorithm for small vectors and short linked lists too. A good method for linked lists can be to insertion sort small chunks (10 - 50 elements) and then recursively merge sort the linked lists until one sorted list remains.
Insertion Sort is an in-place algorithm, which means that it does not need any auxiliary memory space to order an array or whatever.
If you use Insertion Sort on an array, you have to exchange the elements inside it while executing the algorithm, but if you have a linked list, you can exchange the pointers instead of the elements inside the list itself.

convert a binary search tree to doubly linklist using recursion

As mentioned in the Title , I have a Binary search tree. I want to convert it to sorted doubly linklist using recursion.
My code
for each node in tree
find max of left sub-tree and assign its right to present node ,present node left to max
find min of right sub-tree and assign its left to present node ,present node right to max
and now recursively do same thing to other nodes in BST .
but this solution is not efficient as it reaches each node more than one time .In my quest of optimized code i got a link from google greatTreeList sollution . I have searched the same in SO both sollutions are same and worked for me. I didnt understand the append function of the sollution as it contains code
join(alast,b)
join(blast,a)
For tree whose nodes are inserted in following order 10,5,9,6,8,7,12,11,13,14
can anyone please explain how
join(alast,b)
join(blast,a)
are linking node in each recursion call.
I think you are over thinking this actually quite easy task - extracting the data from a binary tree in order is as simple as doing a depth first traversal - this is the point of a binary tree that it very efficiently gives you the elements in the sorted order.
So what you need to do is a standard depth first walk of the tree and each time you find a node add it to you linked list.
This in order depth first recursion is fairly straight forward is pseudocode:
Traverse(Node N)
Traverse(N.left);
Add N to the linked list
Traverse(N.right);
I suggest you try this manually on you example so you see how it works.
In order to convert a binary search tree to a sorted doubly linked list, one typically performs an inorder depth-first traversal, building the list along the traversal.
Try to come up with code that performs an inorder depth-first traversal and prints the items of the binary tree in sorted order. From there, it should be easy to complete your task.
Expanding on Elemental's answer
Traverse(Node N)
Traverse(N.left);
Add N to the linked list
Traverse(N.right);
To add N to the linked list,
your linked list class or data structure should have an append() method or similar.
Use your imagination, but something like this:
def append(N):
new_tail = node(val=N, prev=self.tail, next=None)
self.tail.next = new_tail
self.tail = new_tail
of course you also need to add self.head = self.tail the first time you append.

Resources