UnsortedList in Linked Structures - Python

이세진·2022년 4월 3일
0

Computer Science

목록 보기
37/74

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

UnsortedType.py

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

class UnsortedType:
    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 item == location.info:
                found = True
            else:
                location = location.next
                moreToSearch = location != None
        return found

    def insert_item(self, item):
        '''[1]'''
        location = NodeType(item)
        location.next = self.listData
        self.listData = location
        self.length += 1

    def delete_item(self, item):
        '''[2]'''
        location = self.listData
        if(item == self.listData.info):
            tempLocation = self.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
        else:
            self.currentPos = self.currentPos.next
        return self.currentPos.info

    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)

testunsorted.py

import os
from UnsortedType import *

if __name__ == '__main__':
    l = UnsortedType()
    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 = 2
    if l.retrieve_item(a) == True:
        print(str(a) + " is in the list.")
    else:
        print(str(a) + " is not in the list.")
profile
나중은 결코 오지 않는다.

0개의 댓글