Computer Science, asked by Chiragrock5463, 9 months ago

Write a program to read elements of a list.
a) The program should ask for the position of
the element to be deleted from the list. Write
a function to delete the element at the desired
position in the list.
b) The program should ask for the value of the
element to be deleted from the list. Write a
function to delete the element of this value
from the list.

Answers

Answered by ShivyanshiRai
2

Answer:

a).int deleteElement(int arr[], int n, int x)

{

// Search x in array

int i;

for (i=0; i<n; i++)

if (arr[i] == x)

break;

// If x found in array

if (i < n)

{

// reduce size of array and move all

// elements on space ahead

n = n - 1;

for (int j=i; j<n; j++)

arr[j] = arr[j+1];

}

return n;

}

b).# Python3 program to remove the index

# element from the list

# using traversal

def remove(list1, pos):

newlist = []

# traverse in the list

for x in range(len(list1)):

# if index not equal to pos

if x != pos:

newlist.append(list1[x])

print(*newlist)

# driver code

list1 = [10, 20, 30, 40, 50]

pos = 2

remove(list1, pos)

Answered by Equestriadash
23

The following codes are written in Python.

Source code:

a)

l = eval(input("Enter the elements: "))

print(l, "is your given list.")

x = int(input("Enter the position of the element you want to delete: "))

del l[x]

print(l, "is your given list.")

b)

l = eval(input("Enter the elements: "))

print(l, "is your given list.")

x = eval(input("Enter the element you want to delete: "))

l.remove(x)

print(l)

del is a method that deletes elements, while remove() is a function that removes the specific value from the list, it doesn't require index positions.

Similar questions