Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement a line editor using linked lists. You must implement your own linked l

ID: 3751450 • Letter: I

Question

Implement a line editor using linked lists.  You must implement your own linked list.

A document will be represented by a linked list. Each line in the document is a node in the linked list. Each line in the document is 80 characters. Users can insert, delete or modify lines in the document or print the entire document. Out of bounds print or delete requests are ignored.

User commands:

insertEnd "text" -- insert given text at the end of the document

insert 3 "text" --insert given text at the line indicated by index given

delete 3 --- delete line at index given

edit 3 "text" --- replace the line at the index given with the given text

print -- print the entire document, with line numbers

search "text" -- print the line number and line that contains the given text. print "not found" if it is not found

quit - quit/exit the program

Sample input 1:

insertEnd "now is the time"

insertEnd "for all good men"

insertEnd "to come to the aid of their country"

print

search "come to the aid"

quit

Sample output 1:

1 now is the time

2 for all good men

3 to come to the aid of their country

3 to come to the aid of their country

Sample input 2:

insertEnd "now is the time"

insertEnd "for all good men"

insertEnd "to come to the aid of their country"

print

edit 2 "for all good people"

print

quit

Sample output 2:

1 now is the time

2 for all good men

3 to come to the aid of their country

1 now is the time

2 for all good people

3 to come to the aid of their country

Sample input 3:

insertEnd "now is the time"

insertEnd "for all good people"

insertEnd "to come to the aid of their country"

delete 2

print

insert 2 "for all good people"

print

quit

Sample output 3:

1 now is the time

2 to come to the aid of their country

1 now is the time

2 for all good people

3 to come to the aid of their country

Explanation / Answer

Python3:

## Creating Structure
class node:
    def __init__(self,s):
        self.data=s
        self.next=None
      
## inserting at the end:
def insert_at_end(root,s):
    temp=root
    while(temp.next):
        temp=temp.next
    new_node=node(s)
    temp.next=new_node

## inserting at random position:
def insert_at_pos(root,pos,s):
    cnt=1
    temp=root
    new_node=node(s)
    while(cnt!=pos-1 or temp):
        temp=temp.next
        cnt+=1
    if(cnt!=pos-1):
        insert_at_end(root,s)
    new_node.next=temp.next
    temp.next=new_node

## Delete the node:
def delete(root,pos):
    temp=root.next
    prev=root
    cnt=2
    while(cnt!=pos):
        prev=temp
        temp=temp.next
        cnt+=1
    prev.next=temp.next
    del(temp)

## Edit the text:
def replace(root,pos,s):
    temp=root
    cnt=1
    while(cnt!=pos):
        cnt+=1
        temp=temp.next
    temp.data=s

## print the list:
def print_list(root):
    temp=root
    cnt=1
    while(temp):
        print(cnt,temp.data)
        cnt+=1
        temp=temp.next

## Search the text:
def search(root,s):
    temp=root
    flag=0
    cnt=1
    while(temp):
        if(s in temp.data):
            flag=1
            print(cnt,temp.data)
            break
        cnt+=1
        temp=temp.next
    if(flag==0):
        print("Not Found")
  
cnt=0
while(1):
    arr=list(input().strip().split(' '))
    if(arr[0]=='insertEnd'):
        if(cnt==0):
            root=node(arr[1])
            cnt+=1
        else:
            insert_at_end(root,arr[1])
    elif(arr[0]=='insert'):
        insert_at_pos(root,int(arr[1]),arr[2])
    elif(arr[0]=='delete'):
        delete(root,int(arr[1]))
    elif(arr[0]=='edit'):
        replace(root,int(arr[1]),s)
    elif(arr[0]=='print'):
        print_list(root)
    elif(arr[0]=='search'):
        search(root,arr[1])
    else:
        break

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote