Question

In: Computer Science

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

Solutions

Expert Solution

Program :

Implementation of a single linked list in python.

see the program screenshot for proper indentation.

main.py

#Node class with data and link part of a node in the linked list
class Node :
    def __init__(self, data) :
        self.data = data
        self.link = None
      
      
#linked list class containing a head of the linked list
class LinkedList :
    def __init__(self) :
        self.head = None  #stating node
    
    #display method for the linked list
    def display(self) :
        if self.head is None:
            print("List is empty")
            return
        else:
            print("\n[",end="")
            cur = self.head    #cur - current node
            while cur is not None:
                print(cur.data , end=" ")
                cur = cur.link
                
            print("]\n",end="")
         
    #add_head : add element at the head or staring of the linked list        
    def add_head(self,e) :
        new_node = Node(e)  #creating a new node
        new_node.link = self.head  #setting new_node link to head
        self.head = new_node  #changing head to new node
    
    #add_tail : add element at the end of the linked list
    def add_tail(self,e) :
        new_node = Node(e)  #new node
        #if head is None i.e. list is empty then change head to new node and return
        if self.head is None:
            self.head = new_node
            return
        #otherwise traverse the list upto the last element
        cur = self.head
        while(cur.link is not None) :
            cur = cur.link
        
        #then add the new node in the link of the last element in the linked list
        cur.link = new_node
      
      
    #find_3rd_to_last  : finds the 3rd element from the last of the linked list   
    def find_3rd_to_last(self) :
        #if list size is less than 3 then return None
        if self.head is None or self.head.link is None or self.head.link.link is None :
            return None
        
        #otherwise point to the first and the 3rd element in the list
        pointerA = self.head; #first element
        pointerB = self.head.link.link #third element from the starting
        
        #traverse till pointerB reaches the end of the list 
        #then pointerA  will be pointing to the 3rd last element in the list
        while(pointerB.link is not None) :
            pointerA = pointerA.link
            pointerB = pointerB.link
        
        return pointerA
    
    #reverse : actual reverse of the liked list
    def reverse(self) :
        #if array size is <= 1 then return the head
        if self.head is None or self.head.link is None :
            return self.head
        
        #otherwise take three nodes prvious, current, next
        prev_node = self.head
        cur_node = prev_node.link
        next_node = cur_node.link
        self.head.link = None  #make the head point to None at starting
        
        while(cur_node is not None ) :
            cur_node.link = prev_node   #current.link = previous
            prev_node = cur_node        #previous = current
            cur_node = next_node        #current = next
            if next_node is not None :  #next = next.link
                next_node = next_node.link
        
        #  head->1->2->3->4->5->None
        #  None<-1<-2<-3<-4<-5<-head
        self.head = prev_node  #previous will be having the last node at the end so head will be replaced by the previous
        
        
#main program    
#using this linked list in the program
linked_list = LinkedList()
linked_list.add_head(2)
linked_list.add_tail(3)
linked_list.add_tail(10)
linked_list.add_head(1)
linked_list.add_tail(20)
linked_list.add_tail(30)

print("Dispalying the created list")
linked_list.display()

#taking the 3rd element from the last in node
node = linked_list.find_3rd_to_last()
print("3rd to last element : ",node.data)  #print third element from the last
    
#applying the reverse method on the list
linked_list.reverse()
#displaying the list after reversing iter
print("After reverse  : ")
linked_list.display()


    

Output :

Program screenshots :


Related Solutions

In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
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.
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
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...
Write a program to implement linked list data structure that will have following functions: a. Append...
Write a program to implement linked list data structure that will have following functions: a. Append a node in the list b. Insert a node in the list c. Delete a node from the list d. Display list e. Find maximum value in the list f. Find how many times a value exists in the list. g. Search Portion of the code is give below. You have to write code for the items (e, f, g) Program: #include<stdlib.h> #include<stdio.h> #include<iostream>...
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...
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
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...
What will be the final linked-list after executing the following method on the given input singly...
What will be the final linked-list after executing the following method on the given input singly linked-list? Consider that the singly linked-list does not have a tail reference. Input: 1->2->3->4->5->6->7->8->null                                                    void method(list){ if(list.head == null) return; Node slow_ref = list.head; Node fast_ref = list.head; Node prevS = null; Node prevF = null; while(fast_ref != null && fast_ref.next != null){ prevS = slow_ref; slow_ref = slow_ref.next; prevF = fast_ref; fast_ref = fast_ref.next.next; } prevS.next = slow_ref.next; prevF.next.next = slow_ref; slow_ref.next...
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT