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. "]...
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...
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()
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
Q1) In the implementation of Singly linked list we had an integer variable called size that...
Q1) In the implementation of Singly linked list we had an integer variable called size that keeps track of how many elements in the list. Also, we have a reference “tail” that points to the last node in the list. You are asked to re-implement the concept of singly linked list without using variable size, and without using reference “tail”. a) What are the methods of the main operation of singly linked list that need to be changed? Rewrite them...
Please complete the following list of assignments and upload using this link. Please limit the number...
Please complete the following list of assignments and upload using this link. Please limit the number of files you create to a max of 2, one excel and one word document. P 1-1 FASB Statement of Financial Accounting Concepts No. 2 indicates several qualitative characteristics of useful accounting information. Following is a list of some of these qualities, as well as a list of statements and phrases describing the qualities. a. Benefits > costs b. Decision usefulness c. Relevance d....
Please support with a python response. The prompt was: To locate a link on a webpage,...
Please support with a python response. The prompt was: To locate a link on a webpage, we can search for the following three things in order: - First, look for the three character string '<a ' - Next, look for the following to close to the first part '>': Those enclose the URL - Finally, look for three characters to close the element: '</a': which marks the end of the link text The anchor has two parts we are interested...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT