단일 연결 리스트는 일련의 노드로 구성된 공통 데이터 구조이며, 각 노드에는 요소와 다음 노드에 대한 포인터가 포함됩니다. 클래스를 사용하여 Python에서 단일 연결 목록을 구현할 수 있습니다.
먼저, 요소와 다음 노드에 대한 포인터를 포함하는 노드 클래스를 정의합니다.
class Node: def __init__(self, data=None, next_node=None): self.data = data self.next_node = next_node
여기서 data는 노드의 요소를 나타내고 next_node는 다음 노드에 대한 포인터를 나타냅니다.
다음으로, 단일 연결 목록 작업 삽입, 삭제, 검색 및 인쇄와 같은 몇 가지 기본 작업 메서드와 헤드 노드를 포함하는 단일 연결 목록 클래스를 정의합니다.
class LinkedList: def __init__(self): self.head = Node() def insert(self, data): new_node = Node(data) current_node = self.head while current_node.next_node is not None: current_node = current_node.next_node current_node.next_node = new_node def delete(self, data): current_node = self.head previous_node = None while current_node is not None: if current_node.data == data: if previous_node is not None: previous_node.next_node = current_node.next_node else: self.head = current_node.next_node return previous_node = current_node current_node = current_node.next_node def search(self, data): current_node = self.head while current_node is not None: if current_node.data == data: return True current_node = current_node.next_node return False def print_list(self): current_node = self.head.next_node while current_node is not None: print(current_node.data) current_node = current_node.next_node
위 코드에서 삽입 메서드는 new 노드는 단일 연결 리스트의 끝에 삽입됩니다. delete 메소드는 지정된 요소가 위치한 노드를 삭제합니다. 검색 방법은 단일 연결 리스트에 노드가 존재하는지 찾는 데 사용됩니다. print_list 메소드는 전체 단일 연결 리스트를 인쇄하는 데 사용됩니다.
마지막으로 단일 연결 목록 클래스를 테스트할 수 있습니다.
linked_list = LinkedList() linked_list.insert(1) linked_list.insert(2) linked_list.insert(3) linked_list.insert(4) print(linked_list.search(3)) # True print(linked_list.search(5)) # False linked_list.delete(3) linked_list.print_list() # 1 2 4
위는 Python을 사용하여 단일 연결 목록을 구현하는 기본 단계입니다. Python의 특징은 간단하고 이해하기 쉽고, 적은 양의 코드로 읽고 이해하기 쉽다는 점에서 Python을 데이터 구조 구현에 매우 적합한 프로그래밍 언어로 만듭니다.
위 내용은 Python을 사용하여 단일 연결 목록을 구현하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

다음 단계를 통해 Numpy를 사용하여 다차원 배열을 만들 수 있습니다. 1) Numpy.array () 함수를 사용하여 NP.Array ([[1,2,3], [4,5,6]]과 같은 배열을 생성하여 2D 배열을 만듭니다. 2) np.zeros (), np.ones (), np.random.random () 및 기타 함수를 사용하여 특정 값으로 채워진 배열을 만듭니다. 3) 서브 어레이의 길이가 일관되고 오류를 피하기 위해 배열의 모양과 크기 특성을 이해하십시오. 4) NP.Reshape () 함수를 사용하여 배열의 모양을 변경하십시오. 5) 코드가 명확하고 효율적인지 확인하기 위해 메모리 사용에주의를 기울이십시오.

BroadcastingInnumpyIsamethodtoperformoperationsonArraysoffferentShapesByAutomicallyAligningThem.itsimplifiesCode, enourseadability, andboostsperformance.here'showitworks : 1) smalraysarepaddedwithonestomatchdimenseare

forpythondatastorage, chooselistsforflexibilitywithmixeddatatypes, array.arrayformemory-effic homogeneousnumericaldata, andnumpyarraysforadvancednumericalcomputing.listsareversatilebutlessefficipforlargenumericaldatasets.arrayoffersamiddlegro

pythonlistsarebetterthanarraysformanagingDiversEdatatypes.1) 1) listscanholdementsofdifferentTypes, 2) thearedynamic, weantEasyAdditionSandremovals, 3) wefferintufiveOperationsLikEslicing, but 4) butiendess-effectorlowerggatesets.

toaccesselementsInapyThonArray : my_array [2] AccessHetHirdElement, returning3.pythonuseszero 기반 인덱싱 .1) 사용 positiveAndnegativeIndexing : my_list [0] forthefirstelement, my_list [-1] forstelast.2) audeeliciforarange : my_list

기사는 구문 모호성으로 인해 파이썬에서 튜플 이해의 불가능성에 대해 논의합니다. 튜플을 효율적으로 생성하기 위해 튜플 ()을 사용하는 것과 같은 대안이 제안됩니다. (159 자)

이 기사는 파이썬의 모듈과 패키지, 차이점 및 사용법을 설명합니다. 모듈은 단일 파일이고 패키지는 __init__.py 파일이있는 디렉토리이며 관련 모듈을 계층 적으로 구성합니다.

기사는 Python의 Docstrings, 사용법 및 혜택에 대해 설명합니다. 주요 이슈 : 코드 문서 및 접근성에 대한 문서의 중요성.


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

MinGW - Windows용 미니멀리스트 GNU
이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

SublimeText3 영어 버전
권장 사항: Win 버전, 코드 프롬프트 지원!

SublimeText3 Linux 새 버전
SublimeText3 Linux 최신 버전

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

Atom Editor Mac 버전 다운로드
가장 인기 있는 오픈 소스 편집기
