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

  Subject-customer behavior

Social media has been described as the new hybrid element of the promotion mix. When considering marketing and consumption communities and communication within groups,

  Euro area central governments

ECB expands purchases to include bonds issued by euro area central governments, agencies and European institutions

  Rational model of group decision makingdescribe the

rational model of group decision makingdescribe the rational model of group decision making in terms of a real life

  What can be done to change the perceptions regarding female

What can be done to change the perceptions regarding female-only correctional institutions? Are these issues actually myth?

  Monitoring employee motivationwhat is the most effective

monitoring employee motivationwhat is the most effective way for companies to monitor employee motivation satisfaction

  Descriptive statistics and basic properties of variables

Explain five descriptive statistics used to explain the basic properties of variables. Which ones are used in a corporate office setting?

  Geographic segmentation psychographic segmentation

Explain each of the following segments of the market. Provide examples of how each segment can influence customer choices and trends.

  Managing dynamic enviroment

For this assignment, you should create a plan to hire an employee for a position within your organization to support a recent change. In this plan, you should include the following:

  Economics and financial consequences

A. What are the economics and financial consequences of NX

  Question about economies of scale

Describe to this young entrepreneur may benefit from economies of scale. Also explain some dis-economies of scale.

  What type of remedy in equity makes the defendant

What type of remedy in equity makes the defendant to stop whatever they are doing that is bothering the plaintiff?

  What is the average cost of the firm

a. What is the average cost of the firm? b. What can you say about the marginal cost of the firm?

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