Linked list insert element problem

Assignment Help Business Management
Reference no: EM131981398

Linked List Insert Element Problem

Implement a method, insert_first which takes a linked list as a parameter and elem, and returns a new list, where elem was inserted as a first node. 

def insert_first(s, elem):

  '''

  >>> insert_first(link(3, link(2, link(1, "empty"))),100)

  [100, [3, [2, [1, 'empty']]]]

  '''

Implement a method, insert_last which takes a linked list as a parameter and elem, and returns a new list, where elem was inserted as a last node. 

def insert_last(s, elem):

  '''

  >>> lst = link(2, link(1, empty))

  >>> insert_last(lst,100)

  [2, [1, [100, 'empty']]]

  '''

Implement insert_at which takes a linked-list, an element elem and an insertion index idx and returns the list with the element inserted at the correct location.

def insert_at(s, elem, idx):

  '''

  >>> lst = link(3, link(2, link(1, empty)))

  >>> insert_at(lst, 4, 0)

  [4, [3, [2, [1, 'empty']]]]

 

  >>> insert_at(lst, 4, 2)

  [3, [2, [4, [1, 'empty']]]]

  ''

IMPORTANT Information:

empty = 'empty'

def is_link(s):

  """s is a linked list if it is empty or a (first, rest) pair."""

  return s == empty or (len(s) == 2 and is_link(s[1]))

def link(first, rest):

  """Construct a linked list from its first element and the rest."""

  assert is_link(rest), "rest must be a linked list."

  return [first, rest]

def first(s):

  """Return the first element of a linked list s."""

  assert is_link(s), "first only applies to linked lists."

  assert s != empty, "empty linked list has no first element."

  return s[0]

def rest(s):

  """Return the rest of the elements of a linked list s."""

  assert is_link(s), "rest only applies to linked lists."

  assert s != empty, "empty linked list has no rest."

  return s[1]

# define length

def len_link(s):

  """Return the length of linked list s."""

  length = 0

  while s != empty:

    s, length = rest(s), length + 1

  return length

# getitem

def getitem(s, i):

  """Return the element at index i of linked list s."""

  while i > 0:

    s, i = rest(s), i - 1

  return first(s)

Reference no: EM131981398

Questions Cloud

What is ethical responsibility of an employer to employees : What is the ethical responsibility of an employer to employees who lack basic literacy and numeracy skills? Should companies be required by law to provide.
Information systems analysis and design : COIT20248 – Information Systems Analysis & Design - Website Design and Modelling - The explanation of your assumptions can help the reader understand
Windows editions in terms of startup problems : Why is Windows 10 considered to be a more stable system than previous Windows editions in terms of startup problems.
Compare the positive and negative aspects of dac and rbac : Compare the positive and negative aspects of employing a MAC, DAC, and RBAC. Suggest methods to mitigate the negative aspects for MAC, DAC, and RBAC.
Linked list insert element problem : Implement a method, insert_first which takes a linked list as a parameter and elem, and returns a new list, where elem was inserted as a first node.
Discuss about the relationship between business and society : Prepare and present a video that is a maximum of five to seven (5-7) minutes OR write a four to six (4-6) page paper in which you:
What internal policies do you plan to implement : What internal policies do you plan to implement based on evidence-based practice approaches to ensure your organization meets these standards?
Linked list delete element problem : Implement a method, remove_first which takes a linked list as a parameter and removes the first element. Make sure to check all possible cases
Calculate the firm return on equity : Calculate the firm return on equity. is the percentage return?

Reviews

Write a Review

Business Management Questions & Answers

  Relationship between inflation and unemployment

Would a time series analysis be a viable statistical method to review the relationship between inflation and unemployment rates relative to the Phillips Curve?

  Explain what should the carters cover in program

Explain what should the Carters cover in their new employee induction program and how should they covey this information?

  Local data processing center

A computer operator at the local data processing center decides to visit work on a Monday evening. She has a key to the outside door, and since there is no key required for the computer room, she simply walks into the computer room.

  Employee relations - conflict resolution

From an Employee Relations Specialist perspective, what recommendations do you provide management - what strategies would you suggest for this situation?

  Plan a debate on the merits of each operation

Divide into two groups-one that supports buying a franchised operation and one that favors starting an independent. nonfran. chisel business.

  Describe the purposes and capabilities

Describe the purposes and capabilities of three significant modern-day robot projects, such as bigdog, cog, and asimo.

  Type of defensiveness confrontation

Identify how Jeff's manager Spencer, could have prepared better for the performance meeting. Explain how you would have changed the way the meeting was handled and why. As a manager, describe the steps you would take to try to avoid this type of de..

  E-commerce example like amazon

Using an e-commerce example like Amazon, discuss how e-commerce has changed the way that you comparison shop for features and the best price

  Essay on business processes

Write 500 word essay on business processes-Industry Analysis In this Case Study, you will research an airline company and give a brief overview of the company, identify the industry it is a part of, and complete a Five Forces Industry Analysis

  Organizational architecture and corporate culture

Discuss how organizational architecture and corporate culture are related. Use an example of a real-life firm and discuss how its corporate culture blends with its organizational architecture.

  Analyze what are the benefits of being a member of the wto

Analyze What are the benefits of being a member of WTO? How does it settle disputes? Choose a recent dispute between 2 countries and discuss it with your peers.

  Qualities of great leaders

Those who had the qualities of 'great leaders' were people who ensured the company succeeded and thrived after they had left by mentoring and empowering staff while they were at the company and ensuring the right people were hired to carry the bal..

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd