Facebook
From abeer, 2 Months ago, written in Python.
This paste is a reply to dethcode from abeer - view diff
Embed
Download Paste or View Raw
Hits: 177
  1. class node:
  2.     def __init__(self, pdata):
  3.         self.data = pdata
  4.         self.pointer = None
  5.  
  6. class linkedlist:
  7.     def __init__(self):
  8.         self.head = None
  9.  
  10.     def insertfront(self, pdata):
  11.         newnode = node(pdata)
  12.         if self.head is None:
  13.             self.head = newnode
  14.             return
  15.         else:
  16.             newnode.pointer = self.head
  17.             self.head = newnode
  18.  
  19.     def insertindex(self, data, index):
  20.         newnode = node(data)
  21.         currentnode = self.head
  22.         pos = 0
  23.         if pos == index:
  24.             self.insertfront(data)
  25.         else:
  26.             while (currentnode != None and pos+1 != index):
  27.                 pos+=1
  28.                 currentnode = currentnode.pointer
  29.  
  30.         if currentnode != None:
  31.             newnode.pointer = currentnode.pointer
  32.             currentnode.pointer = newnode
  33.         else:
  34.             print("index not present")
  35.  
  36.     def insertEnd(self, data):
  37.         newnode = node(data)
  38.         if self.head is None:
  39.             self.insertfront(data)
  40.             return
  41.         else:
  42.             currentnode = self.head
  43.             while (currentnode.pointer):
  44.                 currentnode = currentnode.pointer
  45.  
  46.             currentnode.pointer = newnode
  47.  
  48.  
  49.     def Output(self):
  50.         currentnode = self.head
  51.         while (currentnode.pointer != None):
  52.             print(currentnode.data)
  53.             currentnode = currentnode.pointer
  54.  
  55.         print(currentnode.data)
  56.  
  57.     def Delfront(self):
  58.         if self.head == None:
  59.             return
  60.         else:
  61.             self.head = self.head.pointer
  62.  
  63.     def Dellast(self):
  64.         if self.head == None:
  65.             return
  66.         elif self.head.pointer == None:
  67.             self.Delfront()
  68.         else:
  69.             currentnode = self.head
  70.             while currentnode.pointer.pointer != None:
  71.                 currentnode = currentnode.pointer
  72.  
  73.             currentnode.pointer = None
  74.  
  75.     #def deleteatend(self):
  76.  
  77. print("af, means add to front of linked listn"
  78.       "al, means add to last of linked listn"
  79.       "ai, means add to index of linked listn"
  80.       "df, means delete from front of linked listn"
  81.       "dl, means delete from last of linked listn"
  82.       "di, means delete from index of linked listn"
  83.       "o, means output linked listn")
  84. x = ""
  85. llist = linkedlist()
  86. while x != "e":
  87.     x = input("What do you want to do to the linked listn>>>")
  88.     if x == "af":
  89.         y = input("Enter the data")
  90.         llist.insertfront(y)
  91.     elif x == "al":
  92.         y = input("Enter the data")
  93.         llist.insertEnd(y)
  94.     elif x == "ai":
  95.         y = input("Enter the data")
  96.         index = int(input("Enter the index location"))
  97.         llist.insertindex(y, index - 1)
  98.     elif x == "df":
  99.         llist.Delfront()
  100.     elif x == "dl":
  101.         llist.Dellast()
  102.     elif x == "o":
  103.         print("Llist is starting>")
  104.         llist.Output()
  105.         print("<Thats all folks")
  106.     else:
  107.         print("please input something appropriate")