← 목차로 돌아가기 / Back to Index

🧪 Ch03. 선형 리스트 — 실습 코드 / Lab Code: Linear Lists

자료구조와 알고리즘 실습 | Data Structures & Algorithms Lab

💡 학습 팁: 코드를 직접 타이핑하면서 익히세요! 숨겨진 코드는 버튼을 눌러 확인하기 전에 먼저 스스로 생각해 보세요.
💡 Study Tip: Type the code yourself to learn! Try to think about hidden sections before revealing them.

📘 교재 예제 (Code)

📘 Textbook Examples (Code)

Code03-01.py

선형 리스트에 데이터를 추가(append)합니다

katok = []          # 빈 배열

def add_data(friend) :
add_data('다현')
add_data('정연')
add_data('쯔위')
add_data('사나')
add_data('지효')

print(katok)
Code03-01.py

Appends data to a linear list

katok = []          # empty array

def add_data(friend) :
add_data('Dahyun')
add_data('Jungyeon')
add_data('Tzuyu')
add_data('Sana')
add_data('Jihyo')

print(katok)
Code03-02.py

선형 리스트의 특정 위치에 데이터를 삽입합니다

katok = ["다현", "정연", "쯔위", "사나", "지효"]

def insert_data(position, friend) :
insert_data(2, '솔라')
print(katok)
insert_data(6, '문별')
print(katok)
Code03-02.py

Inserts data at a specific position in a linear list

katok = ["Dahyun", "Jungyeon", "Tzuyu", "Sana", "Jihyo"]

def insert_data(position, friend) :
insert_data(2, 'Solar')
print(katok)
insert_data(6, 'Moonbyul')
print(katok)
Code03-03.py

선형 리스트에서 데이터를 삭제합니다

katok = ['다현', '정연', '쯔위', '사나', '지효']


def delete_data(position) :
delete_data(1)
print(katok)
delete_data(3)
print(katok)
Code03-03.py

Deletes data from a linear list

katok = ['Dahyun', 'Jungyeon', 'Tzuyu', 'Sana', 'Jihyo']


def delete_data(position) :
delete_data(1)
print(katok)
delete_data(3)
print(katok)
Code03-04.py

선형 리스트의 추가/삽입/삭제를 메뉴로 통합합니다

## 함수 선언 부분 ## 
def add_data(friend) :
def insert_data(position, friend) :
def delete_data(position) :
## 전역 변수 선언 부분 ## 
katok = []
select = -1	# 1: 추가, 2: 삽입, 3: 삭제, 4: 종료


## 메인 코드 부분 ## 
Code03-04.py

Integrates append/insert/delete into a menu-driven linear list

## Function Declarations ## 
def add_data(friend) :
def insert_data(position, friend) :
def delete_data(position) :
## Global Variables ## 
katok = []
select = -1	# 1: Add, 2: Insert, 3: Delete, 4: Quit


## Main Code ## 
if __name__ == "__main__" :
    
	while (select != 4) :

		select = int(input("Choose(1:Add, 2:Insert, 3:Delete, 4:Quit)--> "))

		if (select == 1) :
			data = input("Data to add--> ")
			add_data(data)
			print(katok)
		elif (select == 2) :
			pos = int(input("Insert position--> "))
			data = input("Data to add--> ")
			insert_data(pos, data)
			print(katok)
		elif (select == 3) :
			pos = int(input("Delete position--> "))
			delete_data(pos)
			print(katok)
		elif (select == 4) :
			print(katok)
			exit
		else :
			print("Please enter 1-4.")
			continue
Code03-05.py

다항식을 배열로 표현하고 덧셈을 수행합니다 (방법1)

## 함수 선언 부분 ## 
def printPoly(p_x) :
def calcPoly(xVal, p_x) :
## 전역 변수 선언 부분 ## 
px = [7, -4, 0, 5]			# = 7x^3 -4x^2 +0x^1 +5x^0 


## 메인 코드 부분 ## 
	
Code03-05.py

Represents polynomials as arrays and performs addition (Method 1)

## Function Declarations ## 
def printPoly(p_x) :
def calcPoly(xVal, p_x) :
## Global Variables ## 
px = [7, -4, 0, 5]			# = 7x^3 -4x^2 +0x^1 +5x^0 


## Main Code ## 
if __name__ == "__main__" :

	pStr = printPoly(px)
	print(pStr)

	xValue = int(input("X value-->"))

	pxValue = calcPoly(xValue, px)
	print(pxValue)

	
Code03-06.py

다항식을 (지수, 계수) 쌍으로 표현하고 덧셈합니다 (방법2)

## 함수 선언 부분 ## 
def printPoly(t_x, p_x) :
def calcPoly(xVal, t_x, p_x) :
## 전역 변수 선언 부분 ## 
tx = [300, 20, 0]
px = [7, -4, 5] 


## 메인 코드 부분 ## 
Code03-06.py

Represents polynomials as (exponent, coefficient) pairs and adds them (Method 2)

## Function Declarations ## 
def printPoly(t_x, p_x) :
def calcPoly(xVal, t_x, p_x) :
## Global Variables ## 
tx = [300, 20, 0]
px = [7, -4, 5] 


## Main Code ## 
if __name__ == "__main__" :

	pStr = printPoly(tx, px)
	print(pStr)

	xValue = int(input("X value-->"))

	pxValue = calcPoly(xValue, tx, px)
	print(pxValue)  

📗 응용 예제 (Ex)

📗 Applied Examples (Ex)

Ex03-01.py

카톡 횟수 기준으로 정렬 삽입하는 선형 리스트를 구현합니다

## 함수 선언 부분 ##
def find_and_insert_data(friend, k_count) :
def insert_data(position, friend):
## 전역 변수 선언 부분 ##
katok = [('다현', 200), ('정연', 150), ('쯔위', 90), ('사나', 30), ('지효', 15)]

## 메인 코드 부분 ##
Ex03-01.py

Implements a linear list that inserts sorted by message count

## Function Declarations ##
def find_and_insert_data(friend, k_count) :
def insert_data(position, friend):
## Global Variables ##
katok = [('Dahyun', 200), ('Jungyeon', 150), ('Tzuyu', 90), ('Sana', 30), ('Jihyo', 15)]

## Main Code ##
if __name__ == "__main__":

	while True:
		data = input("Friend to add--> ")
		count = int(input("Message count--> "))
		find_and_insert_data(data, count)
		print(katok)
Ex03-02.py

(지수, 계수) 방식의 다항식 덧셈을 구현합니다

## 함수 선언 부분 ##
def printPoly(p_x):
def calcPoly(xVal, p_x):
## 전역 변수 선언 부분 ##
px = [ [300, 20, 0],
		[7, -4, 5] ]

## 메인 코드 부분 ##
Ex03-02.py

Implements polynomial addition using (exponent, coefficient) pairs

## Function Declarations ##
def printPoly(p_x):
def calcPoly(xVal, p_x):
## Global Variables ##
px = [ [300, 20, 0],
		[7, -4, 5] ]

## Main Code ##
if __name__ == "__main__":
	pStr = printPoly(px)
	print(pStr)

	xValue = int(input("X value-->"))

	pxValue = calcPoly(xValue, px)
	print(pxValue)

📙 자가 실습 (Self)

📙 Self Practice (Self)

Self03-01.py

선형 리스트에서 데이터 삭제 기능을 직접 구현합니다

katok = ['다현', '정연', '쯔위', '사나', '지효']

def delete_data(position) :
delete_data(1)
print(katok)
delete_data(3)
print(katok)
Self03-01.py

Implements data deletion from a linear list yourself

katok = ['Dahyun', 'Jungyeon', 'Tzuyu', 'Sana', 'Jihyo']

def delete_data(position) :
delete_data(1)
print(katok)
delete_data(3)
print(katok)
Self03-02.py

다항식 표현과 연산을 직접 구현합니다

## 함수 선언부
def printPoly(p_x) :
def calcPoly(xVal, p_x) :
## 전역 변수 선언부
px = [7, -4, 0, 5] # = 7x^3  -4x^2  + 0x^1 +  5x^0 

## 메인 코드
Self03-02.py

Implements polynomial representation and operations yourself

## Function Declarations
def printPoly(p_x) :
def calcPoly(xVal, p_x) :
## Global Variables
px = [7, -4, 0, 5] # = 7x^3  -4x^2  + 0x^1 +  5x^0 

## Main Code
if __name__ == "__main__" :

    pStr = printPoly(px)
    print(pStr)