Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python Program: Define a method to append another list to your originallist. output result: mylist1 = [2,3,4,5] mylist2 = [6,7,8,9] Append mylist2 to mylist1 sot

Python Program:

Define a method to append another list to your originallist.

output result:

mylist1 = [2,3,4,5]

mylist2 = [6,7,8,9]

Append mylist2 to mylist1 sot that mylist1 =[2,3,4,5,6,7,8,9]

MY CODE: Not correct, but I know you have to use addLastfunction to add elements to the end of mylist1 by looping throughmylist2.

import mathclass Node:    def __init__(self, e):        self.element = e        self.next = None# class definition of linklistclass LinkedList:    def __init__(self):        self.head = None        self.tail = None        self.size = 0    def addFirst(self, e):        newNode = Node(e)        newNode.next = self.head        self.head = newNode        self.size = self.size + 1    def addLast(self, e):        newNode = Node(e)        if self.tail == None:            self.head = self.tail = newNode        else:            self.tail.next = newNode            self.tail = self.tail.next        self.size += 1    def add(self, e):        self.addLast(e)    def insert(self, index, e):        newNode = Node(e)        if index == 0:            self.addFirst(e)        elif index >= self.size:            self.addLast(e)        else:            current = self.head            for i in range (1, index):                current = current.next            temp = current.next            current.next = newNode            (current.next).next = temp            self.size += 1    def removeFirst(self):        if self.size == 0:            return None        else:            temp = self.head            self.head = self.head.next            self.size -= 1            if self.head == None:                self.tail = None            return temp.element    def removeLast(self):        if self.size == 0:            return None        elif self.size == 1:            temp = self.head            self.head = self.tail = None            self.size = 0            return temp.element        else:            current = self.head            for i in range(self.size - 2):                current = current.next            temp = self.tail            self.tail = current            self.tail.next = None            self.size -= 1            return temp.element    def removeAt(self, index):        if index < 0 or index >= self.size:            return None        elif index == 0:            return self.removeFirst()        elif index == self.size - 1:            return self.removeLast()        else:            previous = self.head            for i in range(1, index):                previous = previous.next            current = previous.next            previous.next = current.next            self.size -= 1            return current.element    def getFirst(self):        if self.size == 0:            return None        else:            return self.head.element    def getLast(self):        if self.size == 0:            return None        else:            return self.tail.element    def indexOf(self, e):        current = self.head        i = 0        while current != None:            if current.element == e:                return i            i += 1            current = current.next        return -1    def clear(self):        self.head = None        self.tail = None        self.size = 0    def contains(self, e):        if self.indexOf(e) >= 0:            return True        else:            return False    def remove(self,e):        index = self.indexOf(e)        if index == -1:            return False        else:            self.removeAt(index)        return True    def toString(self):        result = "["        current = self.head        for i in range(self.size):            result += str(current.element)            current = current.next            if current != None:                result += ", "            else:                result += "]"        return result    def set(self, index, e):        if index < 0 or index >= self.size:            print("Index is out of range")            return None        current = self.head        for i in range(index):            current = current.next        current.element = e    def isEmpty(self):        if self.size == 0:            return True        else:            return False    def getSize(self):        return self.size    def get(self, index):        if index < 0 or index >= self.size:            return None        current = self.head        for i in range(index):            current = current.next        return current.element    def cumulative_total(self):        total = 0        current = self.head        for i in range(self.size):            total = int(current.element)+total            current = current.next        return total    def compare_cumulative_total(self,l):        if self.cumulative_total() > l.cumulative_total():            return "list 1 is greater"        else:            return "list 2 is greater"    def ListAppend(self,List1):        def main():    mylist1 = LinkedList()    numOfElements = int(input("How many elements you want to add "))    for i in range(numOfElements):        print("Enter Element ", i+1," ")        element = input()        mylist1.add(element)    #print("sum of mylist1 is",sum.mylist1(element))    print("Linked List mylist1 is as follows ", mylist1.toString())    print(" ")    mylist2 = LinkedList()    listElements = input("Enter the elements you need to add seperated by white space ")    lista = listElements.split()    print("Lista = ", lista, "")    for e in lista:        mylist2.add(e)    print("Linked List mylist2 is as follows: ", mylist2.toString(), "")    print("List 1 total: ", mylist1.cumulative_total())    print("List 2 total: ", mylist2.cumulative_total())    print(mylist1.compare_cumulative_total(mylist2))    list1 = mylist1.toString()    list2 = mylist2.toString()    print("Appending is as followed for both lists:", mylist1.ListAppend(mylist1,mylist2))main()

Step by Step Solution

3.36 Rating (165 Votes )

There are 3 Steps involved in it

Step: 1

class list1 def initselfmylist1 selfmyli... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Programming questions