Question

In: Computer Science

Write an algorithm in pseudo code to find one element and delete it in a doubly...

Write an algorithm in pseudo code to find one element and delete it in a doubly linked list. Your algorithm will print the original list, request the user to put in an element to be deleted, then print the final list after the deletion is done. If the element doesn’t exist in the list, print "XXX is not in the list" where "XXX" should be the one you received from the user.

Use the following as your test cases to evaluate whether your algorithm works correctly:

if you have a linked list: 3<->2<->0,

with user input 5, your algorithm should print "5 is not in the list".

with user input 2, your algorithm should print  "3<->0".

if your linked list is empty, with user input 5, your algorithm should print "5 is not in the list".

Solutions

Expert Solution

Algorithm:
   Let structure of DLL node be:
   struct node{
       int data;
       struct node *left,*right;
   }

   Search linearly for the element in the doubly linked list whenever we found it assign prev = node -> left and next = node -> right and perform following operations.
   prev -> right = next
   next -> left = prev
   then free the node.

Pseduocode:
   removeElement(int value){
       curr = head
       //Traverse through DLL
       while(curr){
           if(curr -> data == value){
               break
           }
           curr = curr -> right
       }

       if(curr -> right is NULL){
           //We haven't found the element
           print("%d value is not in the list",value);
           return
       }
       //We have found the node with value
       prev = curr -> left
       next = curr -> right
       //Handle edge cases that is node at end or start
       if(prev is NULL){
           head = head -> right;
       }
       if(next is NULL){
           curr -> next = NULL;
       }
       //Both exist
       if(prev and next){
           prev -> right = next
           next -> left = prev
           free(curr)
       }
       //Printing the DLL
       curr = head
       while(curr){
           print(curr -> data,"<->")
           curr = curr -> right
       }
   }
Sample Input:
   DLL = 3<->2<->0
   removeElement(5) -> "5 is not in the list"
   removeElement(2) -> 3<->0


Related Solutions

Write a pseudo code for an O (n7log3n) algorithm. Please write in C++.
Write a pseudo code for an O (n7log3n) algorithm. Please write in C++.
Write an algorithm to delete an element from a hash table that uses linear probing as...
Write an algorithm to delete an element from a hash table that uses linear probing as its clash resolution strategy. Analyze your algorithm and show the results using order notation?
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of...
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of integers. Assume that the List type has members: int List.length returns the length of the list. void List.push(T n) pushes an element n to the front of the list T List.pop() pops an element from the front of the list. List$$ List$$.concat(List$$ other) returns the concatenation of this list with other. Explain in plain English the reasoning behind your algorithm. Power Lists should be...
Develop an algorithm for INSERTION SORT. Give the pseudo-code version. Convert your pseudo-code into a Java...
Develop an algorithm for INSERTION SORT. Give the pseudo-code version. Convert your pseudo-code into a Java program.
In pseudo-code, design an algorithm for finding baking a cake.
In pseudo-code, design an algorithm for finding baking a cake.
write a recursive algorithm to find the maximum element in an array of n elements and...
write a recursive algorithm to find the maximum element in an array of n elements and analyze its time efficiency. (I am using c++ programming language)
*Please give the answers in pseudo code 1) Design an algorithm that will receive two integer...
*Please give the answers in pseudo code 1) Design an algorithm that will receive two integer items from a terminal operator, and display to the screen their sum, difference, product and quotient. Note that the quotient calculation (first integer divided by second integer) is only to be performed if the second integer does not equal zero. 2) Design an algorithm that will read two numbers and an integer code from the screen. The value of the integer code should be...
Write code on python for Exponential Cooling Schedule Techniques of the SA algorithm to find the...
Write code on python for Exponential Cooling Schedule Techniques of the SA algorithm to find the shortest tour to visit all the cities according to TSP??
Write a c++ member function that sequentially searches for a specific element in a doubly linked...
Write a c++ member function that sequentially searches for a specific element in a doubly linked list. return the position if found or -1 is the element cannot be found.
Write a c++ member function that attempts to insert a NON DUPLICATE element to a doubly...
Write a c++ member function that attempts to insert a NON DUPLICATE element to a doubly linked list, After the attempted insertion return the SIZE of the doubly linked list whether or not the insertion was successful.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT