converting an array to linked list

Solutions on MaxInterview for converting an array to linked list by the best coders in the world

showing results for - "converting an array to linked list"
Gabriel
20 Jun 2017
1 LinkedList ll = new LinkedList(Arrays.asList(sa));
Joaquín
09 Feb 2020
1class MaxPriority():
2    def __init__(self):
3        self.heap = []
4        self.tempNumber = []
5        self.tempWord = []
6        self.counter = int(0)
7        self.maxsize = int(0)
8        self.size = int(0)
9
10    def root(self,index):
11        if index == 0:
12            return True
13        return False
14    
15    def leaf(self,index):
16        if index >= self.size//2 and index <= self.size:
17            return True
18        return False
19
20    def getParent(self, index):
21        if self.root(index):
22            return None
23        return (index-1)//2
24
25    def leftChild(self,index):
26        if self.leaf(index):
27            return None
28        return (index*2)+1
29
30    def rightChild(self,index):
31        if self.leaf(index):
32            return None
33        elif (index*2)+2 >= self.size:
34                return None
35        return (index*2)+2
36
37    def swap(self,src,dest):
38        self.heap[src], self.heap[dest] = self.heap[dest], self.heap[src]
39
40    def heapifyUp(self):
41        temp = self.size-1
42        if self.size>1:
43            while(self.heap[self.getParent(temp)] < self.heap[temp]):
44                self.swap(temp,self.getParent(temp))
45                temp = self.getParent(temp)
46                if self.getParent(temp) == None:
47                    break
48
49    def heapifyDown(self):
50        temp = 0
51        if self.size == 2:
52            if self.rightChild(temp) != None:
53                if self.heap[self.leftChild(temp)] > self.heap[self.rightChild(temp)]:
54                    self.swap(temp,self.leftChild(temp))
55                    temp = self.leftChild(temp)
56                else:
57                    self.swap(temp,self.rightChild(temp))
58                    temp = self.rightChild(temp)
59            else:
60                if self.heap[self.leftChild(temp)] > self.heap[temp]:
61                    self.swap(temp,self.leftChild(temp))
62        elif self.size>=3:
63            while(self.heap[temp] < self.heap[self.leftChild(temp)] or self.heap[temp] < self.heap[self.rightChild(temp)]):
64                if self.heap[self.leftChild(temp)] > self.heap[self.rightChild(temp)]:
65                    self.swap(temp,self.leftChild(temp))
66                    temp = self.leftChild(temp)
67                else:
68                    self.swap(temp,self.rightChild(temp))
69                    temp = self.rightChild(temp)
70                if self.leftChild(temp) == None or self.rightChild(temp) == None:
71                    break
72    
73    def add(self,word,number):
74        self.tempWord.append(word)
75        self.tempNumber.append(number)
76        self.size+=1
77        if self.size > self.maxsize:
78            self.heap.append(number)
79            self.maxsize+=1
80        else:
81            self.heap[self.size-1] = number
82        self.heapifyUp()
83
84    def delete(self):
85        self.heap[0] = self.heap[self.size-1]
86        self.size-=1
87        self.heapifyDown()
88
89    def selesaikan(self):
90        if self.size == 0:
91            print("Priority is empty !!")
92        elif self.size == 1:
93            print("Only one priority !!")
94        else:
95            self.counter=self.size
96            print("\n== URUTAN PRIORITAS TUGAS TOTO BERDASARKAN MAX HEAP ==")
97            for x in range (0,self.size):
98                for i in range(0,1):
99                    for j in range(0,self.counter):
100                        if(str(self.heap[i]) == str(self.tempNumber[j])):
101                            print("-> Prioritas " + str(x+1) + " = " + str(self.tempWord[j]))
102                self.delete()
103
104    def peek(self):
105        if self.size>0:
106            return self.heap[0]
107        return None
108
109tugas = MaxPriority()
110print("\n== MEMPRORITASKAN TUGAS TOTO BERDASARKAN MAX HEAP ==")
111n_data = int(input("-> Masukkan banyak data yang akan disusun : "))
112
113for i in range (0,n_data):
114    name= input("\n1) Nama tugas : ")
115    priorityy = int(input("2) Prioritas : "))
116    print("-> Data berhasil disimpan!!")
117    tugas.add(name,priorityy)
118
119tugas.selesaikan()
120
121
queries leading to this page
how to convert an array into a linked listconvert linked list to array in javahow to make linked list into arraypython linked list to arrayhow to convert a linked list to a list in pythonconvert array of list to linked list pyhtonhow to convert array to linkedlist in javalinked list to an arrayjava convert a string to arraylistconvert string to linked list javaconvert linkedlist to array javalinked list to arrayconvert a list to linked listconvert array of nodes to linked listconvert list into linked listjava linkedlist importarray to string and array to linked list javalinkedlist to array string in javaconverting array to linked list pythonconvert array into a linked list pythoncan i convert array to linked list in javaimport linked list javaconvert a linked list into arraypython convert linked list to arrayturn array into linked listconvert array to linkedlisyconvert array linkedlist javajava convert array to linked listconvert string to linked liststring to linkedlist javalinked list import javaconvert arrays to linked listarray to linkedlist javaturn list into linked list pythonimport linkedlisthow to convert a linked list to an array in javajava linked list importturn array into linkedlist javaconvert array into a linked listhow to implement array into linked listjava how to convert array to linkedlistconverting an array to linked listlinkedlist importhow to convert array to linked listlinked list to string array javaturn array into linked list pythonconvert linked list to array pconvert int array to linkedlist javalinkedlist from array javaload array into linked listarray to linked list javaarray to linkedlistjava array to linkedlistarray to linkedlist pythonhow to make an array to linkedlist in javalinkedlist to string javaconvert linked list to arraylsitjava linked list from arrayhow to convert a list to a linked list pythonjava string array to linked listwhy to convert linked list to arraysconvert linkedlist to arrayhow to convert array to linked list pythonarray to linked list pythonjava convert arrays to linked listsconvert array of nodes to linked list pyhtonarray to linked listconvert a string array to a linkedlist javaconvert linked list to arrayconvert linked list to array javahow to convert array to linked list in javaconvert array to linked list javalinked list converted to arrays convert array to linked list pythonconvert linked list to array c 2b 2bconvert array to linked listlinked list to array in c 2b 2bstring array into linkedlist javaconverting an array to linked list