A Blog by: Sachin Kumar Saxena
Pages
- Home
- Computer Graphics
- Digital Image Processing
- Top 25 Websites to Practice Aptitude and Quantitat...
- Principles of Soft Computing
- Artificial Intelligence
- ML: Programming Language for PPL subject
- Data Structure
- Principle of Programming Language
- Design and Analysis of Algorithm
- General Awareness in Computer Science: Questions a...
- Discrete Structure & Theory of Logic
- Project Proposal for BTech (CS)
- Graphics and Animation
- ROE088 VREHC (Values, Relationship & Ethical Human Conduct-For A Happy & Harmonious Society)
- Cyber Security
- COMPUTER SYSTEM SECURITY
- Web Technology
- Parallel Computing
- Linked List: Data Structure
- Advance Matlab
- Cryptography and Network Security
- Computer Based Numerical & Statistical Techniques
- Python for Beginners
- LISt Programming (LISP)
- Research Methodology: Advance Research Studies in Computer Engineering
- Rural Development: Administration and Planning
- Digital and Social Media Marketing KOE094
- Data Compression KCS064
- Theory behind Deep Learning
Tuesday, 25 December 2018
Monday, 19 November 2018
Linked List: Data Structure
Linked List: Data Structure
Access our official website:
http://examworld.co.in/data-structure-questions-and-answers-assignment-3/
Q.1. Write an algorithm to find the location of an element in the given Linked List. Is the binary search will be suitable for this search? Explain the reason.
Sol. As Linked List does not have sequence of element in linear format so one will have to Generate a while loop by traversing all nodes comparing the data with K which is the element to search in Linked Link.
Algorithm SEARCH_Node_Linked List (K, Head) // Name of the Function
Begin t=0 // Variable initialize which will be updated only when K is found
current =head // Initially pointer points to head of the Linked List
while (current !=null) //Till end of LL
{
if(current ->info=K // Data of the node
{ t=1 break }
current=current->next
}
if(t=1)
print ”Element found”
else
print “Not found" End. //End of the Algorithm
Use of binary Search in Linked List: it is worthless to use Binary Search in LL in order to find element as time complexity will be O(n), because it is tedious task to find out the middle node of any LL which is pre-requisiteness of Binary Search.
Q.2. Write down an algorithm to delete a node from doubly Linked List.
Sol. Following is the representation of doubly Linked List consisting the header, Next Node, Previous Node address in front and rear part of each node.
Image Courtesy: https://www.geeksforgeeks.org/delete-a-node-in-a-doubly-linked-list/
Algorithm to delete node from any position
%% Input : head {Pointer to the first node of the list}
last {Pointer to the last node of the list}
N {Position to be deleted from list}
Begin:
current ← head;
For i ← 1 to N and current != NULL do
current ← current.next;
End for
If (N == 1) then
deleteFromBeginning()
End if
Else if (current == last) then
deleteFromEnd()
End if
Else if (current != NULL) then
current.prev.next ← current.next
If (current.next != NULL) then
current.next.prev ← current.prev;
End if
unalloc (current)
write ('Node deleted successfully from ', N, ' position')
End if
Else then
write ('Invalid position')
End if
End
In the above algorithm, if node to be deleted is first node then delete first node algorithm will be invoked, in case last node pointer last node will be deleted, now for the any node:
current.prev.next ← current.next
If (current.next != NULL) then
current.next.prev ← current.prev;
Address of next node of current will be stored in previous node, in next step; address of previous node will be assign in next node of current node.
Q.3. Write down the algorithm to find out the middle node in singly linked list.
Sol. There are two methods to solve this question:
Method A: Blind Fold Method
Traverse the entire linked list and count the total number of nodes. Now traverse the list again till count/2 and return the node at count/2. This is very naïve and foolish idea to find the middle node.
Algorithm for Method A:
Middle_Node_Linked List (Count, Head) // Name of the Function
Begin t=0, Count=0; // Variable initialize to find total nodes in LL
current =head // Initially pointer points to head of the Linked List
while (current !=null) //Till end of LL
{
Count++// Data of the node
current=current->next
}
If(current==Null)
Return No node //No Node is there in LL
If(current==Head)
Return Head //first Node is middle node
Else
For i ← 1 to Count/2 and current != NULL do
current ← current.next;
End for
//End of the Algorithm
Method B: Two concurrent pointers methods
Traverse linked list using pointer “a” and pointer “b”. Move pointer “a” by one and pointer “b” by two. When the pointer “a” reaches end slow pointer will reach middle of the linked list. Therefore we will find the middle node.
Algorithm for Method B:
Node *slow_ptr = head;
Node *fast_ptr = head;
if (head!=NULL)
{
while (fast_ptr != NULL && fast_ptr->next != NULL)
{
fast_ptr = fast_ptr->next->next;
slow_ptr = slow_ptr->next;
}
printf("The middle element is [%d]\n\n", slow_ptr->data);
}
Support Video: https://www.youtube.com/watch?v=Uk-PkL5WMMY
Q.4. Write down the algorithm to add two linked lists. For example node1=561 and node2=246 then output should be num=807
Sol.
Steps to be taken place in order to write algorithm:
Step 1: Count the size of both Linked Lists.
Step 2: if size is same then perform the addition else find out the difference of both LLs.
Step 3: take care of Carry if there is while adding nodes value and add this carry in next nodes addition as per basic Decimal Addition rule.
Step 4: Put the values in separate Stacks in then add these values.
Step 5: output the final addition of two LLs.
Algorithm:
----------------------------------------------------------------------------------------------------------------
Node* addUsingReverse(Node*h1, Node*h2)
{
reverseLL(&h1);
reverseLL(&h2);
Node*a = h1;
Node*b = h2;
int carry = 0;
Node* c = NULL;
while(a != NULL || b!= NULL)
{
int sum = carry;
if(a != NULL)
sum += a->data;
if(b != NULL)
sum += b->data;
carry = sum / 10;
sum = sum % 10;
appendNode(&c, sum);
if(a != NULL)
a = a->link;
if(b != NULL)
b = b->link;
}
if(carry != 0)
appendNode(&c, carry);
reverseLL(&h1);
reverseLL(&h2);
reverseLL(&c);
return c;
}
------------------------------------------------------------------------------------------------------------
Courtesy by: http://www.ritambhara.in/add-numbers-given-in-the-form-of-linked-list/
Q.5. Write down the algorithm to Sort the elements of doubly Linked List.
Sol. For the sorting of elements in LL we can initialize the temp1 and tem2 variable and also third variable T.
Algorithm Sort_Doubly_Linked_List () // Name of the Function
Begin temp1=head // initialize from header node
Repeat 3 and 4 till while (temp1 !=null) // Initially pointer points to head of the Linked List
If(temp1->info > temp2->info) //compare the values of nodes
do (i), (ii), (iii) and (iv)
T=temp1->info //swapping the values of temp1 and temp2
temp1->info > temp2->info
iii. temp2->info > T
temp2=temp2-> next;
temp1=temp1->next;
//End of the Algorithm
******* Best of Luck *********
WITH REGARDS
SACHIN KUMAR SAXENA
With you, In Christ, In His Mission
Assistant Professor, CSE DEPARTMENT,
College of Engineering Roorkee, Roorkee,
Uttarakhand
+91-8909603708
Saturday, 9 June 2018
UGC NET COMPUTER SCIENCE SAMPLE PAPERS WITH SOLUTIONS
To check your score attempt the following sample papers:
Access our new website:
www.examworld.co.in
Wednesday, 6 June 2018
Learn Advance Matlab 2017a Tutorial
Running codes of Matlab for B.Tech,. M.Tech. and BCA Mini/Major Projects and Ph.D. thesis are available here....
- Introduction to Matlab
- Working on Matlab
- Graph Plotting by user with popupmenu given inputs(lower, upper limits, Range) by user
- Calculator for conversion of dimensions
- Image Processing: separating objects of a specific color from an image
- GUI Radiobutton and Checkboxes : Booking movie ticket by user using
- Webcam: taking pic with webcam in GUI
- GUI: Sin/Cos plot program
- Image conversion: rgb to gray or black-white scale in GUI
- Fuzzy Logic: read inputs from user then produce output to predict rainfall
- OPTIMIZATION BY NORTH WEST CORNER RULE
- OPTIMAL SOLUTION OFEQUATION SUBJECT TO CONSTRAINTS
Subscribe to:
Posts (Atom)
GATE Notes
-
Syllabus: Click here for AKTU Link Blow Up VREHC ROE088 Question Papers Database Click Here for complete notes ROE088 VREHC (Values, Relat...
-
To Download Data Structure Study Material click on appropriate link :- Handwritten Notes: Data Structure Handwritten Notes Unit I Data Stru...
-
or Click Here Proposed Syllabus Class Notes DISCRETE STRUCTURES & THEORY OF LOGIC AKTU Sample Paper 1 ...
-
Title of the document Assignment 1 Artificial Intelligence Syllabus ...
-
Principle of Programming Language This subject is focused on fundamental values of every software programming language in comput...
-
* CBNST NOTES * NUMERICAL TECHNIQUE LAB PROGRAMS Data Compression
-
To Download Study Material click on appropriate link :- Unit 1 Intro to DAA 2 Intro to DAA 3 Analysis of Algorithms 2 Algorithm ...
-
Click on above link to Download Matlab 2012 Software 32/64 bit..... Running codes of Matlab for B.Tech,. M.Tech. and BCA Mini/Major...
Intro to Soft Computing Objective Questions
Question Statement Solution with step wise marking "Select a 4-input neuron weighs 1, 2, 3, 4. The transfer function here...