SortedList in Linked Structures - Python

이세진·2022년 4월 3일
0

Computer Science

목록 보기
38/74

생성일: 2021년 10월 28일 오후 8:40

SortedType.py

class NodeType:
    """ Node Type """
    def __init__(self, item):
        self.info = item
        self.next = None

class SortedType:
    def __init__(self):
        self.listData = None
        self.length = 0
        self.currentPos = None

    def is_full(self):
        try:
            location = NodeType("test")
            return False
        except:
            return True

    def length_is(self):
        return self.length

    def make_empty(self):
        while self.listData != None:
            tempPtr = self.listData.next
            del self.listData
            self.listData = tempPtr
        self.length = 0

    def retrieve_item(self, item):
        location = self.listData
        found = False
        moreToSearch = location != None

        while moreToSearch and not found:
            if location.info < item:
                location = location.next
                moreToSearch = location != None
            elif location.info == item:
                found = True
            else:
                moreToSearch = False

        return found

    def insert_item(self, item):
        location = self.listData
        predLoc = None
        moreToSearch = location != None

        '''[3]'''
        while(moreToSearch):
            if (location.info < item):
                predLoc = location
                location = location.next
                moreToSearch = location != None
            else:
                moreToSearch = False
        newNode = NodeType(item)
        if(predLoc == None):
            newNode.next = self.listData
            self.listData = newNode
        else:
            newNode.next = location
            predLoc.next = newNode
        self.length += 1

    def delete_item(self, item):
        '''[4]'''
        location = self.listData
        if (item == self.listData.info):
            tempLocation = location
            self.listData = self.listData.next
        else:
            while(not(item == location.next.info)):
                location = location.next
            tempLocation = location.next
            location.next = location.next.next
        del tempLocation
        self.length -= 1

        
    def reset_list(self):
        self.currentPos = None

    def get_next_item(self):
        if self.currentPos == None:
            self.currentPos = self.listData
        item = self.currentPos.info
        self.currentPos = self.currentPos.next

        return item

    def __str__(self):
        self.reset_list()
        items = []
        for i in range(0, self.length):
            t = self.get_next_item()
            items.append(str(t))
        return " ".join(items)

testsorted.py

import os
from SortedType import *

if __name__ == '__main__':
    l = SortedType()
    f = open('./data.txt', 'r')
    line = f.readline()
    while line:
        i = int(line)
        l.insert_item(i)
        line = f.readline()
    f.close()
    
    print("Before:")
    print(l)
        
    print("After deleting 65:")
    a = 65
    l.delete_item(a)
    print(l)
    print()
    
    a = 3
    if l.retrieve_item(a) == True:
        print(str(a) + " is in the list.")
    else:
        print(str(a) + " is not in the list.")
        
    a = 77
    if l.retrieve_item(a) == True:
        print(str(a) + " is in the list.")
    else:
        print(str(a) + " is not in the list.")
profile
나중은 결코 오지 않는다.

0개의 댓글