Question

In: Computer Science

concatenate(lst1, lst2 ) Given two (possibly empty) unordered lists, concatenate them such that the first list...

concatenate(lst1, lst2 )

Given two (possibly empty) unordered lists, concatenate them such that the first list comes first.Example: When the first input unordered list lst1 is [1, 2, 3] and the second lst2 is [7,8,9], the outputunordered list is [1,2,3,7,8,9].

Use codes:

class Node:
def __init__(self, data):
self.data = data
self.next = None

def getData(self):
return self.data

def getNext(self):
return self.next

def setData(self, data):
self.data = data

def setNext(self, node):
self.next = node

class UnorderedList:
def __init__(self):
self.head = None

def add(self, data):
newNode = Node(data)
newNode.setNext(self.head)
self.head = newNode

def length(self):
cur = self.head
count = 0
while cur:
cur = cur.getNext()
count += 1
return count

def isEmpty(self):
return self.head == None

def search(self, data):
cur = self.head
while cur:
if cur.getData() == data:
return True
cur = cur.getNext()
return False

def remove(self, data):
if self.head.getData() == data:
self.head = self.head.getNext()
else:
cur = self.head
while cur.getNext() and cur.getNext().getData() != data:
cur = cur.getNext()
if cur.getNext():
cur.setNext(cur.getNext().getNext())

Solutions

Expert Solution

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

    def getData(self):
        return self.data

    def getNext(self):
        return self.next

    def setData(self, data):
        self.data = data

    def setNext(self, node):
        self.next = node


class UnorderedList:
    def __init__(self):
        self.head = None

    def add(self, data):
        newNode = Node(data)
        newNode.setNext(self.head)
        self.head = newNode

    def length(self):
        cur = self.head
        count = 0
        while cur:
            cur = cur.getNext()
            count += 1
        return count

    def isEmpty(self):
        return self.head == None

    def search(self, data):
        cur = self.head
        while cur:
            if cur.getData() == data:
                return True
            cur = cur.getNext()
        return False

    def remove(self, data):
        if self.head.getData() == data:
            self.head = self.head.getNext()
        else:
            cur = self.head
            while cur.getNext() and cur.getNext().getData() != data:
                cur = cur.getNext()
                if cur.getNext():
                    cur.setNext(cur.getNext().getNext())
                  
    def display(self):
        curr = self.head
        while curr != None:
            print(curr.getData(), end = " ")
            curr = curr.getNext()
        print()


def concatenate(lst1, lst2):
    first = lst1.head
    second = lst2.head
  
    if first == None:
        lst1.head = lst2.head
  
    while first.getNext() != None:
        first = first.getNext()
      
    first.setNext(lst2.head)

      
  


lst1 = UnorderedList()
lst1.add(3)
lst1.add(2)
lst1.add(1)

lst2 = UnorderedList()
lst2.add(9)
lst2.add(8)
lst2.add(7)

concatenate(lst1, lst2)

lst1.display()


Related Solutions

Given two sorted linked lists, merge them into a third sorted linked list. If an element...
Given two sorted linked lists, merge them into a third sorted linked list. If an element is present in both the lists, it should occur only once in the third list. Code needed in java.
Q: Let’s say you have an unordered list of numbers and you wanted to put them...
Q: Let’s say you have an unordered list of numbers and you wanted to put them in order from lowest to highest value. How would you do that? You’re probably thinking that you would just look at all the numbers, find the lowest number and put it at the beginning of your list. Then you would find the next largest number and put it in the second spot in the list, and so on until you’ve ordered the entire list...
Given an unordered list (defined using the UnorderedList class above), transform it into a Python list.When...
Given an unordered list (defined using the UnorderedList class above), transform it into a Python list.When the input list is empty, the output Python list should be empty as well.Example: When the input unordered list is [1,2,3], the output Python list should be [1,2,3] . Use the following codes: class Node: def __init__(self, data): self.data = data self.next = None def getData(self): return self.data def getNext(self): return self.next def setData(self, data): self.data = data def setNext(self, node): self.next = node...
Given a list of items, write a program that generates a list of lists of the...
Given a list of items, write a program that generates a list of lists of the following form: [a,b,c,...,z]⇒[[z], [y,z], [x,y,z], ... , [a,b, ... ,y,z]] Hint: Slicing is your friend. please write a python program
create a function to merge and concatenate two linked list l1 =[1,2,4] and l2=[1,3,4]. dont modify...
create a function to merge and concatenate two linked list l1 =[1,2,4] and l2=[1,3,4]. dont modify l2 (the nodes inserted in l1 should be copies of l2 class Node(object): def __init__(self, data): self.data = data self.next = None    class LinkedList(object): def print_list(self): cur_node = self.head while cur_node: print(cur_node.data) cur_node = cur_node.next def __init__(self): self.head = None;    def append(self, data): new_node = Node(data) if self.head is None: self.head = new_node return last_node = self.head while last_node.next: last_node = last_node.next...
Create two lists; one contains 5 elements and the other one is empty. Write a python...
Create two lists; one contains 5 elements and the other one is empty. Write a python program that iterates through the first one, pops the elements from the end of it, then it pushes them one by one to the empty list. As your program iterates through the first list, clean it before processing the data, meaning if any element is like a special character ( , . ; : ) it must be discarded and not gets pushed to...
Given two lists, write python code to print “True” if the two lists have at least...
Given two lists, write python code to print “True” if the two lists have at least one common element. For example, x = [1,2,3], y=[3,4,5], then the program should print “True” since there is a common element 3.
The author lists three types of waste. List the three with the given definitions. Give a...
The author lists three types of waste. List the three with the given definitions. Give a real world example of each type of waste.
In DrRacket Write a function, removeAll, which takes two lists, list-a and list-b and returns a...
In DrRacket Write a function, removeAll, which takes two lists, list-a and list-b and returns a list containing only the items in list-a that are not also in list-b. E.g., (remove-all '(a b b c c d) '(a c a)) -> '(b b d)
The intersection of two lists L1 and L2, L1 ∩ L2, is defined as the list...
The intersection of two lists L1 and L2, L1 ∩ L2, is defined as the list containing elements in both L1 and L2 only. Given two sorted lists L1 and L2, write a function, called intersection, to compute L1 ∩ L2 using only the basic list operations. The intersection function is defined as follows template list intersection( const list & L1, const list & L2); C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT