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 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...
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...
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.
*LISP PROGRAM* 2. Write a function that, given a list of lists, returns the total length...
*LISP PROGRAM* 2. Write a function that, given a list of lists, returns the total length of all the lists. This problem can be solved two different ways. 3. Write a program that prompts the user to enter two numbers and then outputs the sum of the two numbers. 4.Write ALLODDP, a recursive function that returns T if all the numbers in a list are odd.
C++ language or Python. Linked Lists You are given a linked list that contains N integers....
C++ language or Python. Linked Lists You are given a linked list that contains N integers. You are to perform the following reverse operation on the list: Select all the subparts of the list that contain only even integers. For example, if the list is {1,2,8,9,12,16}, then the selected subparts will be {2,8}, {12,16}. Reverse the selected subpart such as {8,2} and {16,12}. The list should now be {1,8,2,9,16,12}. Your node definition should consist of 2 elements: the integer value...
Make two lists. List A should identify the things that represent the core cultures of organizations....
Make two lists. List A should identify the things that represent the core cultures of organizations. List B should id identify the tings that represent the observable cultures of the organization. For each item on the two lists , identify one or more indicators that you might use to describe this aspect of the culture for an actual organization.
Meta Language(ML) has two commonly used data structures(Lists and tuples). Distinguish between two of them and...
Meta Language(ML) has two commonly used data structures(Lists and tuples). Distinguish between two of them and rationalize your argument by the support of code snippet of each.        Lists Tuples How can we concatenate a string in Meta Language(ML)? Also write one coding example of string concatenation.                                                                                                                         What is meant by right-associative ? Elaborate it using an example.                                Write a condition in the given blank below using comparison operator to get the require output from this code.                                                                ...
what do you believe are the four strongest triggers for aggression? List them in the first...
what do you believe are the four strongest triggers for aggression? List them in the first column. Explain why you chose those four triggers. Explain how the three means of reducing violence in the could be used to reduce the impetus of the four triggers you listed. Name the violence reducers you would use and describe how you could create a helping behavior instead.
Understand the first two laws of thermodynamics, and apply them to living systems.
Understand the first two laws of thermodynamics, and apply them to living systems.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT