Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b2365c5

Browse files
committed
ported linked list code
1 parent c01c8e9 commit b2365c5

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

‎Linked_List/main.py‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
2+
class node:
3+
def __init__(self,data=None):
4+
self.data=data
5+
self.next=None
6+
7+
class linked_list:
8+
def __init__(self):
9+
self.head=node()
10+
11+
# Adds new node containing 'data' to the end of the linked list.
12+
def append(self,data):
13+
new_node=node(data)
14+
cur=self.head
15+
while cur.next!=None:
16+
cur=cur.next
17+
cur.next=new_node
18+
19+
# Returns the length (integer) of the linked list.
20+
def length(self):
21+
cur=self.head
22+
total=0
23+
while cur.next!=None:
24+
total+=1
25+
cur=cur.next
26+
return total
27+
28+
# Prints out the linked list in traditional Python list format.
29+
def display(self):
30+
elems=[]
31+
cur_node=self.head
32+
while cur_node.next!=None:
33+
cur_node=cur_node.next
34+
elems.append(cur_node.data)
35+
print(elems)
36+
37+
# Returns the value of the node at 'index'.
38+
def get(self,index):
39+
if index>=self.length() or index<0: # added 'index<0' post-video
40+
print("ERROR: 'Get' Index out of range!")
41+
return None
42+
cur_idx=0
43+
cur_node=self.head
44+
while True:
45+
cur_node=cur_node.next
46+
if cur_idx==index: return cur_node.data
47+
cur_idx+=1
48+
49+
# Deletes the node at index 'index'.
50+
def erase(self,index):
51+
if index>=self.length() or index<0: # added 'index<0' post-video
52+
print("ERROR: 'Erase' Index out of range!")
53+
return
54+
cur_idx=0
55+
cur_node=self.head
56+
while True:
57+
last_node=cur_node
58+
cur_node=cur_node.next
59+
if cur_idx==index:
60+
last_node.next=cur_node.next
61+
return
62+
cur_idx+=1
63+
64+
# Allows for bracket operator syntax (i.e. a[0] to return first item).
65+
def __getitem__(self,index):
66+
return self.get(index)
67+
68+
69+
#######################################################
70+
# Functions added after video tutorial
71+
72+
# Inserts a new node at index 'index' containing data 'data'.
73+
# Indices begin at 0. If the provided index is greater than or
74+
# equal to the length of the linked list the 'data' will be appended.
75+
def insert(self,index,data):
76+
if index>=self.length() or index<0:
77+
return self.append(data)
78+
cur_node=self.head
79+
prior_node=self.head
80+
cur_idx=0
81+
while True:
82+
cur_node=cur_node.next
83+
if cur_idx==index:
84+
new_node=node(data)
85+
prior_node.next=new_node
86+
new_node.next=cur_node
87+
return
88+
prior_node=cur_node
89+
cur_idx+=1
90+
91+
# Inserts the node 'node' at index 'index'. Indices begin at 0.
92+
# If the 'index' is greater than or equal to the length of the linked
93+
# list the 'node' will be appended.
94+
def insert_node(self,index,node):
95+
if index<0:
96+
print("ERROR: 'Erase' Index cannot be negative!")
97+
return
98+
if index>=self.length(): # append the node
99+
cur_node=self.head
100+
while cur_node.next!=None:
101+
cur_node=cur_node.next
102+
cur_node.next=node
103+
return
104+
cur_node=self.head
105+
prior_node=self.head
106+
cur_idx=0
107+
while True:
108+
cur_node=cur_node.next
109+
if cur_idx==index:
110+
prior_node.next=node
111+
return
112+
prior_node=cur_node
113+
cur_idx+=1
114+
115+
# Sets the data at index 'index' equal to 'data'.
116+
# Indices begin at 0. If the 'index' is greater than or equal
117+
# to the length of the linked list a warning will be printed
118+
# to the user.
119+
def set(self,index,data):
120+
if index>=self.length() or index<0:
121+
print("ERROR: 'Set' Index out of range!")
122+
return
123+
cur_node=self.head
124+
cur_idx=0
125+
while True:
126+
cur_node=cur_node.next
127+
if cur_idx==index:
128+
cur_node.data=data
129+
return
130+
cur_idx+=1

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /