OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Insertion function for linklist is not working

  • Thread starter Thread starter user3257213
  • Start date Start date
U

user3257213

Guest
My code:

Code:
def __init__(self):
    self.data = ""
    self.NextPointer = -1


def InsertLinkList(LinkList, StartPointer, FreePointer):

    if FreePointer < len(LinkList):

        data = int(input("Enter the data"))
        newNode = FreePointer
        LinkList[FreePointer].data = data
        LinkList[FreePointer].NextPointer = -1
        print(LinkList)
        FreePointer = LinkList[FreePointer].NextPointer

        if StartPointer == -1:
            StartPointer = newNode

        else:
            currentPointer = StartPointer
            while currentPointer != -1:
                lastNode = currentPointer
                currentPointer = LinkList[currentPointer].NextPointer
            LinkList[lastNode].NextPointer = newNode

    else:
        print("List is full")

    return LinkList, StartPointer, FreePointer


def PrintAll(LinkList):
    for i in range(10):
         print(LinkList[i].data)


LinkList = [Node() for i in range(10)]
StartPointer = -1
FreePointer = 0

for i in range(10):

    LinkList, StartPointer, FreePointer = InsertLinkList(LinkList, StartPointer, FreePointer)

PrintAll(LinkList)

Hello everyone,

I recently uploaded my code for link list insertion, but I am encountering some issues with its functionality. The insertion process is not happening as expected, and I am in need of assistance in understanding and resolving this problem.

If anyone has expertise in working with link lists and is willing to lend a hand, I would greatly appreciate your help.

My OutPut LOOks like

Enter the data1 Enter the data2 Enter the data3 Enter the data4 Enter the data5 Enter the data6 Enter the data7 Enter the data8 Enter the data9 Enter the data11 1

11
<p>My code:</p>
<pre><code>def __init__(self):
self.data = ""
self.NextPointer = -1


def InsertLinkList(LinkList, StartPointer, FreePointer):

if FreePointer < len(LinkList):

data = int(input("Enter the data"))
newNode = FreePointer
LinkList[FreePointer].data = data
LinkList[FreePointer].NextPointer = -1
print(LinkList)
FreePointer = LinkList[FreePointer].NextPointer

if StartPointer == -1:
StartPointer = newNode

else:
currentPointer = StartPointer
while currentPointer != -1:
lastNode = currentPointer
currentPointer = LinkList[currentPointer].NextPointer
LinkList[lastNode].NextPointer = newNode

else:
print("List is full")

return LinkList, StartPointer, FreePointer


def PrintAll(LinkList):
for i in range(10):
print(LinkList.data)


LinkList = [Node() for i in range(10)]
StartPointer = -1
FreePointer = 0

for i in range(10):

LinkList, StartPointer, FreePointer = InsertLinkList(LinkList, StartPointer, FreePointer)

PrintAll(LinkList)
</code></pre>
<p>Hello everyone,</p>
<p>I recently uploaded my code for link list insertion, but I am encountering some issues with its functionality. The insertion process is not happening as expected, and I am in need of assistance in understanding and resolving this problem.</p>
<p>If anyone has expertise in working with link lists and is willing to lend a hand, I would greatly appreciate your help.</p>
<p>My OutPut LOOks like</p>
<p>Enter the data1
Enter the data2
Enter the data3
Enter the data4
Enter the data5
Enter the data6
Enter the data7
Enter the data8
Enter the data9
Enter the data11
1</p>
<p>11</p>
 

Latest posts

Top