' https://www.sanfoundry.com/c-program-doubly-linked-list/
/*
* Program to Implement a Doubly Linked List & provide Insertion,
* Deletion & Display Operations
*/
$ filename "Dbl_LinkedList.exe"
'uses rtl32
'uses rtl64
uses corewin
uses console
indexbase 0
type CONSOLE_CURSOR_INFO ' cci
dword dwSize
bool bVisible
end type
sub setcolor(int fg, bg)
SetConsoleTextAttribute (ConsOut, fg+bg*16)
end sub
sub locate(int row,int col, optional int cursor_visible=0,int shape=12)
CONSOLE_CURSOR_INFO cci
SetPos(col-1,row-1)
cci.bVisible = cursor_visible
cci.dwSize = shape
SetConsoleCursorInfo(ConsOut, cci)
end sub
sub display(int col, row, string txt, optional int cursor_visible=0,int shape=12)
locate(row, col, cursor_visible, shape)
print txt
end sub
/*
struct node
{
struct node *prev;
int n;
struct node *next;
}*h,*temp,*temp1,*temp2,*temp4;
*/
type node
node *prev
int n
node *after
end type
node *h, *temp, *temp1,*temp2,*temp4
def -> .
macro getstructmemory(structure)
sys _temp_ = getmemory(sizeof(structure))
@structure = _temp_
end macro
declare sub insert1()
declare sub insert2()
declare sub insert3()
declare sub traversebeg()
declare sub traverseend(int)
declare sub sort()
declare sub search()
declare sub update()
declare sub delete()
int count = 0
string empty = " "
sub main()
int ch
@h = NULL
@temp = @temp1 := NULL
SetConsoleTitle "Doubly linked lists in Oxygenbasic"
do
start:
setcolor(11, 0)
display(1, 1, " 1 - Insert Integer at beginning")
display(1, 2, " 2 - Insert Integer at end")
display(1, 3, " 3 - Insert Integer at position Ix")
display(1, 4, " 4 - Delete at position Ix")
display(1, 5, " 5 - Display from beginning")
display(1, 6, " 6 - Display from end")
display(1, 7, " 7 - Search for a value element")
display(1, 8, " 8 - Sort the list")
display(1, 9, " 9 - Update a value element")
display(1,10, " 10 - Exit")
display(16,12, " ")
display(1,12, " Enter choice : ", 1)
ch = val(input())
setcolor(7,0)
display(1,14, empty)
display(1,15, empty)
select case ch
case 0
cls
case 1
insert1()
case 2
insert2()
case 3
insert3()
case 4
delete()
case 5
traversebeg()
case 6
@temp2 = @h
if (@temp2 == NULL) then
display 1, 14, " Error : List empty to display "
else
display 1, 14, " Reverse order of linked list is : "
traverseend(temp2->n)
end if
case 7
search()
case 8
sort()
case 9
update()
case 10
terminate
end
case else
display 1,14, " Wrong choice menu"
end select
goto start
end do
end sub
/* TO create an empty node */
sub create()
int data
// temp =(struct node *)malloc(1*sizeof(struct node))
'sys _temp_ = getmemory(sizeof(node))
'@temp = _temp_
getstructmemory(temp)
@temp->prev = NULL
@temp->after = NULL
display 1,15, " Enter value to node : ", 1
data = val(input())
temp->n = data
count++
end sub
/* TO insert at beginning */
sub insert1()
if (@h == NULL) then
create()
@h = @temp
@temp1 = @h
else
create()
@temp->after = @h
@h->prev = @temp
@h = @temp
end if
end sub
/* To insert at end */
sub insert2()
if (@h == NULL) then
create()
@h = @temp
@temp1 = @h
else
create()
@temp1->after = @temp
@temp->prev = @temp1
@temp1 = @temp
end if
end sub
/* To insert at any position */
sub insert3()
int pos, i = 2
display 1, 14, " Enter position to be inserted : ", 1
pos = val(input())
@temp2 = @h
display 1,15, empty
if ((pos < 1) || (pos >= count + 1)) then
display 1, 15, " Position out of range to insert"
return
end if
if ((@h == NULL) && (pos != 1)) then
display 1, 15, " Empty list cannot insert other than 1st position"
return
end if
if ((@h == NULL) && (pos == 1)) then
create()
@h = @temp
@temp1 = @h
return
elseif pos = 1 then
create()
@temp->after = @h
@h->prev = @temp
@h = @temp
else
while (i < pos)
@temp2 = @temp2->after
i++
wend
create()
@temp->prev = @temp2
@temp->after = @temp2->after
@temp2->after->prev = @temp
@temp2->after = @temp
end if
end sub
/* To delete an element */
sub delete()
int i = 1, pos
if (@h == NULL) then
display 1, 14, " Error : Empty list no elements to delete"
return
end if
display 1, 14, " Enter position to be deleted : ", 1
pos = val(input())
@temp2 = @h
if ((pos < 1) || (pos >= count + 1)) then
display 1, 14, " Error : Position out of range to delete"
return
end if
while (i < pos)
@temp2 = @temp2->after
i++
wend
if (i == 1) then
if (@temp2->after == NULL) then
display 1,15, "Node deleted from list"
freememory(@temp2)
@temp2 = @h := NULL
return
end if
end if
if (@temp2 == NULL) then
display 1, 14, " Error : Position out of range to delete"
return
end if
if (@temp2->after == NULL) then
@temp2->prev->after = NULL
freememory(@temp2)
display 1,15, "Node deleted from list"
return
end if
@temp2->after->prev = @temp2->prev
if (i != 1) then
/* Might not need this statement if i == 1 check */
@temp2->prev->after = @temp2->after
end if
if (i == 1) then
@h = @temp2->after
end if
display 1, 15, " Node deleted"
freememory(@temp2)
count--
end sub
/* Traverse from beginning */
sub traversebeg()
@temp2 = @h
if (@temp2 == NULL) then
display 1,14, "List empty to display"
return
end if
display 1,14, " Linked list elements from begining : "
while (@temp2->after != NULL)
print temp2->n " "
@temp2 = @temp2->after
wend
print temp2->n
end sub
/* To traverse from end recursively */
sub traverseend(int i)
if (@temp2 != NULL) then
i = temp2->n
@temp2 = @temp2->after
traverseend(i)
print " " i
end if
end sub
/* To search for an element in the list */
sub search()
int data, count = 0
@temp2 = @h
if (@temp2 == NULL) then
display 1, 14, " Error : List empty to search for data"
return
end if
display 1, 14, " Enter value to search : ", 1
data = val(input())
while (@temp2 != NULL)
if (temp2->n == data) then
display 1, 15, " Data found in position " (count+1)
return
else
@temp2 = @temp2->after
count++
end if
wend
display 1, 15, " Error : " data " not found in list"
end sub
/* To update a node value in the list */
sub update()
int data, data1
@temp2 = @h
if (@temp2 == NULL) then
display 1, 14, " Error : List empty no node to update"
return
end if
display 1, 14, " Enter data of node to be updated : ", 1
data = val(input())
display 1, 15, " Enter new data : ", 1
data1 = val(input())
while (@temp2 != NULL)
if (temp2->n == data) then
temp2->n = data1
traversebeg()
return
else
@temp2 = @temp2->after
end if
wend
display 1, 15, " Error : " data " not found in list to update"
end sub
/* To sort the linked list */
sub sort()
int i, j, x
@temp2 = @h
@temp4 = @h
if (@temp2 == NULL) then
display 1, 14, " List empty to sort"
return
end if
'for (temp2 = h; temp2 != NULL; temp2 = temp2->after)
@temp2 = @h
while (@temp2 != NULL)
'for (temp4 = temp2->after; temp4 != NULL; temp4 = temp4->after)
@temp4 = @temp2->after
while (@temp4 != NULL)
if (temp2->n > temp4->n) then
x = temp2->n
temp2->n = temp4->n
temp4->n = x
end if
@temp4 = @temp4->after
wend
@temp2 = @temp2->after
wend
traversebeg()
end sub
main()