This project demonstrates how to design and write a program in Till that will implement project requirements and logic.
Project objectives:
- Arrangement of code by sections: UI, logic, data management
- Project show modes: ID and ORDER
- Linked list mechanism that implements bi-directional navigation
- Full linked list functionalities: get, set, insert and remove
- Navigation and locating of nodes
- Handling various scenarios of linked list chaining logic
- Full representation of the linked list, including root and top location
Note:
Though there is a thorough commentating in the code, full analysis and explanation of the code is beyond the scope of this overview, and the developer is advised to see all programming guidance and system functionalities in Till Programming Reference.
Project’s code:
≡
≡ Title: Linked List
≡ Version: 2.2
≡ Till Version: 2.4
≡ Created: 2019-11-10
≡ Updated: 2024-01-03
≡ Author: Till Software Foundation
≡
≡ ───────────────────────── HEADER ───────────────────────────────────────
≡ -- user specified data --
≡ show mode options: 'ID', 'ORDER'
SHOWMODE 'ID'
≡ -- global variables --
≡ linked list
linkedlist { counter 0 rootid 0 topid 0 }
linkedlist ◄ 'ids' [ ]
linkedlist ◄ 'map' { }
node newnode { id 0 val 0 prev 0 next 0 }
val 'val'
prev 'prev'
next 'next'
nullid 0
≡ ui
colsep '|'
nl newline
spcs
≡ ───────────────────────── MAIN ───────────────────────────────────────
go »
init
intro
insert 50
insert 20
insert 120
insert 70
remove 20
removeval 20
remove 4
removeval 170
≡ ───────────────────────── INIT & RESET ───────────────────────────────────────
init » i 10 ~ spcs <- spc i
≡ ───────────────────────── LINKED LIST MGMT ───────────────────────────────────────
empty » not linkedlist.rootid
getnode id » linkedlist.map id
get id key »
node linkedlist.map id
node key
set id key v »
node linkedlist.map id
node key v
linkedlist.map id node
add v p n »
_p _n nullid
p ? _p p
n ? _n n
newid linkedlist.counter ++
newnode.id newid
newnode.val v
newnode.prev _p
newnode.next _n
linkedlist.ids ◄ newid
linkedlist.map ◄ newid newnode
newid
insert v »
print 'Insert: ' + v
inserted t
empty ?
insertfirst v
⇋
id linkedlist.rootid
~
curv get id val
v < curv ?
id linkedlist.rootid ?
insertbeforefirst v
✖
⇋
insertbefore id v
✖
ᐤ
⇋
id linkedlist.topid ?
insertaftertop v
✖
⇋
id get id next
ᐤ
ᐤ
ᐤ
ᐤ
show
insertfirst v »
newid add v
linkedlist.rootid linkedlist.topid newid
insertbeforefirst v »
newid add v nullid linkedlist.rootid
set linkedlist.rootid prev newid
linkedlist.rootid newid
insertbefore id v »
p get id prev
newid add v p id
set p next newid
set id prev newid
insertaftertop v »
newid add v linkedlist.topid nullid
set linkedlist.topid next newid
linkedlist.topid newid
remove id »
print 'Remove ID: ' + id
found f
empty ?
print nl + 'List is empty!' + nl + 'Nothing to remove.' + nl + nl
⇋
null linkedlist.map id ?
print nl + 'No item found for ID ' + id + '!' + nl + nl
⇋
found t
removeid id
ᐤ
found ?
linkedlist.ids ► id
linkedlist.map ► id
show
ᐤ
ᐤ
removeval v »
print 'Remove Value: ' + v
empty ?
print 'List is empty!' + nl + 'Nothing to remove.' + nl + nl
⇋
found f
id linkedlist.rootid
~
curv get id val
curv v ?
found t
removeid id
✖
ᐤ
id get id next
id nullid ? ✖
ᐤ
found ?
linkedlist.ids ► id
linkedlist.map ► id
show
⇋
print nl + 'Value ' + v + ' not found!'
ᐤ
ᐤ
removeid id »
node getnode id
p node prev
n node next
hasprev ( p > nullid ? t ⇋ f )
hasnext ( n > nullid ? t ⇋ f )
hasprev ? set p next n ⇋ linkedlist.rootid n
hasnext ? set n prev p ⇋ linkedlist.topid p
≡ ───────────────────────── UI ───────────────────────────────────────
intro »
print 'LINKED LIST PROJECT' + nl
pkv 'Show Mode' SHOWMODE
print
show »
msg nl
empty ?
msg ++ 'List is empty!' + nl + nl
⇋
msg ++ ' ID | DATA | PREV NEXT ' + nl
msg ++ '------+----------+-------------' + nl
SHOWMODE 'ID' ?
max len linkedlist.ids
i max ~ msg ++ getrowstr linkedlist.ids i
⇋
id linkedlist.rootid
~
msg ++ getrowstr id
id get id next
not id ? ✖
ᐤ
ᐤ
msg ++ nl + nl
ᐤ
print msg
getrowstr id »
node getnode id
isroot ( id linkedlist.rootid ? t ⇋ f )
istop ( id linkedlist.topid ? t ⇋ f )
rowstr datastr 2 6 id
rowstr ++ colsep
rowstr ++ datastr 3 10 node val
rowstr ++ colsep
rowstr ++ datastr 2 6 node prev
rowstr ++ datastr 2 5 node next
roottopstr ''
isroot ? roottopstr ++ ' <- ROOT' ⇋ istop ? roottopstr ++ ' <- TOP'
rowstr ++ roottopstr + nl
rowstr
numlen num » 1 + math.floor math.log num 10
datalen data »
str '' + data
len str
spc num »
str ''
i num ~ str ++ ' '
str
datastr beflen strlen data »
not data ? data '-'
str spcs beflen
str ++ data
aftlen strlen - beflen + datalen data
str ++ spcs aftlen
str
pkv k v » print k + ': ' + v