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...
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...
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++
*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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT