Facebook
From abeer, 2 Months ago, written in Python.
This paste is a reply to dethcode from abeer - go back
Embed
Viewing differences between dethcode and Correct dethcode
class node:
    def __init__(self, pdata):
        self.data = pdata
        self.pointer = None

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

    def insertfront(self, pdata):
        newnode = node(pdata)
        if self.head is None:
            self.head = newnode
            return
        else:
            newnode.pointer = self.head
            self.head = newnode

    def insertindex(self, data, index):
        newnode = node(data)
        currentnode = self.head
        pos = 0
        if pos == index:
            self.insertfront(data)
        else:
            while (currentnode != None and pos+1 != index):
                pos+=1
                currentnode = currentnode.pointer

        if currentnode != None:
            newnode.pointer = currentnode.pointer
            currentnode.pointer = newnode
        else:
            print("index print("index not present")

present")

    def insertEnd(self, data):
        newnode = node(data)
        if self.head is None:
            self.insertfront(data)
            return
        else:
            currentnode = self.head
            while (currentnode.pointer):
                currentnode = currentnode.pointer

            currentnode.pointer = newnode


    def Output(self):
        currentnode = self.head
        while (currentnode.pointer != None):
            print(currentnode.data)
            currentnode = currentnode.pointer

        print(currentnode.data)

    def Delfront(self):
        if self.head == None:
            return
        else:
            self.head = self.head.pointer

    def Dellast(self):
        if self.head == None:
            return
        elif self.head.pointer == None:
            self.Delfront()
        else:
            currentnode = self.head
            while currentnode.pointer.pointer != None:
                currentnode = currentnode.pointer

            currentnode.pointer = None

    #def deleteatend(self):

print("af, print("af, means add to front of linked listn"
      "al, 
listn"
      "al, 
means add to last of linked listn"
      "ai, 
listn"
      "ai, 
means add to index of linked listn"
      "df, 
listn"
      "df, 
means delete from front of linked listn"
      "dl, 
listn"
      "dl, 
means delete from last of linked listn"
      "di, 
listn"
      "di, 
means delete from index of linked listn"
      "o, 
listn"
      "o, 
means output linked listn")
listn")
x = ""
""
llist = linkedlist()
while x != "e":
    
"e":
    
x = input("What input("What do you want to do to the linked listn>>>")
    
listn>>>")
    
if x == "af":
        
"af":
        
y = input("Enter input("Enter the data")
data")
        llist.insertfront(y)
    elif x == "al":
        
"al":
        
y = input("Enter input("Enter the data")
data")
        llist.insertEnd(y)
    elif x == "ai":
        
"ai":
        
y = input("Enter input("Enter the data")
        
data")
        
index = int(input("Enter int(input("Enter the index location"))
location"))
        llist.insertindex(y, index - 1)
    elif x == "df":
"df":
        llist.Delfront()
    elif x == "dl":
"dl":
        llist.Dellast()
    elif x == "o":
        print("Llist 
"o":
        print("Llist 
is starting>")
starting>")
        llist.Output()
        print("<Thats print(&quot;&lt;Thats all folks")
folks&quot;)
    else:
        print("please print(&quot;please input something appropriate")appropriate&quot;)