Question

In: Computer Science

in python please Q1) Create a Singly link list and write Python Programs for the following...

in python please

Q1) Create a Singly link list and write Python Programs for the following tasks: a. Delete the first node/item from the beginning of the link list b. Insert a node/item at the end of the link list c. Delete a node/item from a specific position in the link list

Q2) Create a Singly link list and write a Python Program for the following tasks: a. Search a specific item in the linked list and return true if the item is found and display the node position otherwise return false. b. Reverse the link list.

Solutions

Expert Solution

Python code

Q1)

class Node:
def __init__(self, data=None):
self.data = data
self.next = None

class PLinkedList:
def __init__(self):
self.head = None

# Function to delete node at first
def DeleteAtFirst(self):
if ( self.head is None):
print('No node')
else:
temp=self.head
self.head=self.head.next
temp=None
print('Deleted')
  
# Function to insert newnode at end
def InsertAtEnd(self, newdata):
NewNode = Node(newdata)
if self.head is None:#no node in the list
self.head = NewNode
return
temp = self.head
while(temp.next):#traverse the list up to the end of the node
temp = temp.next
temp.next=NewNode

# Function to remove node at given poistion
def DeleteNodeAtpos(self, pos):
temp = self.head
count=0 # finding position
if (self.head is not None and pos==1):# first node, positon=1
self.head = self.head.next #assign first node to next
temp = None
print('Deleted')
return
prev=self.head
flag=0
while (temp is not None):
count=count+1
if count == pos:
prev.next=temp.next
temp=None
print('Deleted')
flag=1
break
prev = temp
temp = temp.next
if flag==0:
print('No node at this position')

# Function to display the linked list

def Listdisplay(self):
temp = self.head
while (temp):
print(temp.data)
temp = temp.next

Plist = PLinkedList()
Plist.InsertAtEnd(5)
Plist.InsertAtEnd(6)
Plist.InsertAtEnd(7)
Plist.Listdisplay()
Plist.DeleteAtFirst()
Plist.Listdisplay()
Plist.DeleteNodeAtpos(2)
Plist.Listdisplay()
Plist.DeleteNodeAtpos(2)


output

5
6
7
Deleted
6
7
Deleted
6
No node at this position

screenshort of output

Q2)

class Node:
def __init__(self, data=None):
self.data = data
self.next = None

class PLinkedList:
def __init__(self):
self.head = None
  
  
# Function to insert newnode at end
def InsertAtEnd(self, newdata):
NewNode = Node(newdata)
if self.head is None:#no node in the list
self.head = NewNode
return
temp = self.head
while(temp.next):#traverse the list up to the end of the node
temp = temp.next
temp.next=NewNode
  
# This Function checks whether the value present in the linked list
def Listsearch(self, val):
  
# Initialize temp to head
temp = self.head
count=0
# loop till temp not equal to None
while temp != None:
count=count+1
if temp.data == val:
print('Found at position',count)
return True # data found
  
temp = temp.next
  
return False # Data Not found

# Function to reverse the linked list
def Reverselist(self):
prev = None
temp = self.head
while(temp is not None):
next = temp.next
temp.next = prev
prev = temp
temp = next
self.head = prev
  
# Function to display the linked list
def Listdisplay(self):
temp = self.head
while (temp):
print(temp.data)
temp = temp.next

Plist = PLinkedList()
Plist.InsertAtEnd(5)
Plist.InsertAtEnd(6)
Plist.InsertAtEnd(7)
Plist.Listdisplay()
if Plist.Listsearch(6)==False:
print('Not Found')
Plist.Reverselist()
print('List after reverse')
Plist.Listdisplay()


screenshort of output

Q1

Q2


Related Solutions

Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Singly Linked List. 5. Insert a new node at the end of a Singly Linked List 6. Insert a new node after the value 5 of Singly Linked List 7. Delete the node with value 6. 8. Search an existing element in...
Problem Description: Using Python write a Singly‐Linked List (with only the Head pointer) that supports the...
Problem Description: Using Python write a Singly‐Linked List (with only the Head pointer) that supports the following operations: 1. Adding an element at the middle of the list. 2. Removing the middle element of the list (and return the data). 3. Adding an element at a given index. 4. Removing an element at a given index (and return the data). For #1 and #2, ignore the operations if the length of the list is an even number. For #3, if...
Python: High school assignment, please keep simple In python: Use the following initializer list to create...
Python: High school assignment, please keep simple In python: Use the following initializer list to create an array: twainQuotes = ["I have never let my schooling interfere with my education.", "Get your facts first, and then you can distort them as much as you please.", "If you tell the truth, you don't have to remember anything.", "The secret of getting ahead is getting started.", "Age is an issue of mind over matter. If you don't mind, it doesn't matter. "]...
In python: implement a singly linked list with following functions: - add_head(e) - add_tail(e) - find_3rd_to_last()...
In python: implement a singly linked list with following functions: - add_head(e) - add_tail(e) - find_3rd_to_last() - returns element located at third-to-last in the list - reverse() - reveres the linked list, note, this is not just printing elements in reverse order, this is actually reversing the list
Please write a python code for the following. Use dictionaries and list comprehensions to implement the...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the functions defined below. You are expected to re-use these functions in implementing other functions in the file. Include a triple-quoted string at the bottom displaying your output. Here is the starter outline for the homework: a. def count_character(text, char): """ Count the number of times a character occurs in some text. Do not use the count() method. """ return 0 b. def count_sentences(text): """...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the functions defined below. You are expected to re-use these functions in implementing other functions in the file. Include a triple-quoted string at the bottom displaying your output. Here is the starter outline for the homework: g. def big_words(text, min_length=10): """ Return a list of big words whose length is at least min_length """ return [] h. def common_words(text, min_frequency=10): """ Return words occurring at...
Write a template class that implements an extended queue (use singly Linked List) in c++ please...
Write a template class that implements an extended queue (use singly Linked List) in c++ please create 3 classes please create 3 classes please create 3 classes please create 3 classes please create 3 classes Ex: ExtendedQueue int_queue; ExtendedQueue double_queue; ExtendedQueue char_queue; –Write a program to test this template class. you have to use inheritance so you will create 3 classes : so you will create 3 classes : so you will create 3 classes : so you will create...
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
PYTHON: Describe a recursive algorithm that counts the number of nodes in a singly linked list.
PYTHON: Describe a recursive algorithm that counts the number of nodes in a singly linked list.
Please use Python to create a method for a linked list that returns the index of...
Please use Python to create a method for a linked list that returns the index of a lookup value within the linked lust
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT