all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
3
Task/Singly-linked-list-Element-insertion/0DESCRIPTION
Normal file
3
Task/Singly-linked-list-Element-insertion/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Using the link element defined in [[Singly-Linked List (element)]], define a method to insert an element into a [[singly-linked list]] following a given element.
|
||||
|
||||
Using this method, insert an element C into a list comprised of elements A->B, following element A.
|
||||
2
Task/Singly-linked-list-Element-insertion/1META.yaml
Normal file
2
Task/Singly-linked-list-Element-insertion/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Data Structures
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
(defun insert-after (x e xs)
|
||||
(cond ((endp xs)
|
||||
nil)
|
||||
((equal x (first xs))
|
||||
(cons (first xs)
|
||||
(cons e (rest xs))))
|
||||
(t (cons (first xs)
|
||||
(insert-after x e (rest xs))))))
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
MODE STRINGLIST = STRUCT(STRING value, REF STRINGLIST next);
|
||||
|
||||
STRINGLIST list := ("Big",
|
||||
LOC STRINGLIST := ("fjords",
|
||||
LOC STRINGLIST := ("vex",
|
||||
LOC STRINGLIST := ("quick",
|
||||
LOC STRINGLIST := ("waltz",
|
||||
LOC STRINGLIST := ("nymph",NIL))))));
|
||||
|
||||
PROC insert = (REF STRINGLIST list, node)VOID: (
|
||||
next OF node := next OF list;
|
||||
next OF list := node
|
||||
);
|
||||
|
||||
STRINGLIST very := ("VERY", NIL);
|
||||
|
||||
# EXAMPLE OF INSERTION #
|
||||
insert(next OF next OF list, very );
|
||||
|
||||
REF STRINGLIST node := list;
|
||||
WHILE REF STRINGLIST(node) ISNT NIL DO
|
||||
print((value OF node, space));
|
||||
node := next OF node
|
||||
OD;
|
||||
print((newline))
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
package
|
||||
{
|
||||
public class Node
|
||||
{
|
||||
public var data:Object = null;
|
||||
public var link:Node = null;
|
||||
|
||||
public function insert(node:Node):void
|
||||
{
|
||||
node.link = link;
|
||||
link = node;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
import Node;
|
||||
|
||||
var A:Node = new Node(1);
|
||||
var B:Node = new Node(2);
|
||||
var C:Node = new Node(3);
|
||||
A.insert(B);
|
||||
A.insert(C);
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
with Ada.Unchecked_Deallocation;
|
||||
-- Define the link type
|
||||
procedure Singly_Linked is
|
||||
|
||||
type Link;
|
||||
type Link_Access is access Link;
|
||||
type Link is record
|
||||
Data : Integer;
|
||||
Next : Link_Access := null;
|
||||
end record;
|
||||
-- Instantiate the generic deallocator for the link type
|
||||
procedure Free is new Ada.Unchecked_Deallocation(Link, Link_Access);
|
||||
|
||||
-- Define the procedure
|
||||
procedure Insert_Append(Anchor : Link_Access; Newbie : Link_Access) is
|
||||
begin
|
||||
if Anchor /= null and Newbie /= null then
|
||||
Newbie.Next := Anchor.Next;
|
||||
Anchor.Next := Newbie;
|
||||
end if;
|
||||
end Insert_Append;
|
||||
|
||||
-- Create the link elements
|
||||
A : Link_Access := new Link'(1, null);
|
||||
B : Link_Access := new Link'(2, null);
|
||||
C : Link_Access := new Link'(3, null);
|
||||
-- Execute the program
|
||||
begin
|
||||
Insert_Append(A, B);
|
||||
Insert_Append(A, C);
|
||||
Free(A);
|
||||
Free(B);
|
||||
Free(C);
|
||||
end Singly_Linked;
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
a = 1
|
||||
a_next = b
|
||||
b = 2
|
||||
b_next = 0
|
||||
c = 3
|
||||
insert_after("c", "a")
|
||||
Listvars
|
||||
msgbox
|
||||
return
|
||||
|
||||
insert_after(new, old)
|
||||
{
|
||||
local temp
|
||||
temp := %old%_next
|
||||
%old%_next := new
|
||||
%new%_next := temp
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
DIM node{pNext%, iData%}
|
||||
DIM a{} = node{}, b{} = node{}, c{} = node{}
|
||||
|
||||
a.pNext% = b{}
|
||||
a.iData% = 123
|
||||
b.iData% = 789
|
||||
c.iData% = 456
|
||||
|
||||
PROCinsert(a{}, c{})
|
||||
END
|
||||
|
||||
DEF PROCinsert(here{}, new{})
|
||||
new.pNext% = here.pNext%
|
||||
here.pNext% = new{}
|
||||
ENDPROC
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
template<typename T> void insert_after(link<T>* list_node, link<T>* new_node)
|
||||
{
|
||||
new_node->next = list_node->next;
|
||||
list_node->next = new_node;
|
||||
};
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
link<int>* a = new link<int>('A', new link<int>('B'));
|
||||
link<int>* c = new link<int>('C');
|
||||
|
|
@ -0,0 +1 @@
|
|||
insert_after(a, c);
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
while (a)
|
||||
{
|
||||
link<int>* tmp = a;
|
||||
a = a->next;
|
||||
delete tmp;
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
void insert_append (link *anchor, link *newlink) {
|
||||
newlink->next = anchor->next;
|
||||
anchor->next = newlink;
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
link *a, *b, *c;
|
||||
a = malloc(sizeof(link));
|
||||
b = malloc(sizeof(link));
|
||||
c = malloc(sizeof(link));
|
||||
a->data = 1;
|
||||
b->data = 2;
|
||||
c->data = 3;
|
||||
|
|
@ -0,0 +1 @@
|
|||
insert_append (a, b);
|
||||
|
|
@ -0,0 +1 @@
|
|||
insert_append (a, c);
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
free (a);
|
||||
free (b);
|
||||
free (c);
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(defn insert-after [new old ls]
|
||||
(cond (empty? ls) ls
|
||||
(= (first ls) old) (cons old (cons new (rest ls)))
|
||||
:else (cons (first ls) (insert-after new old (rest ls)))))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
user=> (insert-after 'c 'a '(a b))
|
||||
(a c b)
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
(defun insert-after (new-element old-element list &key (test 'eql))
|
||||
"Return a list like list, but with new-element appearing after the
|
||||
first occurence of old-element. If old-element does not appear in
|
||||
list, then a list returning just new-element is returned."
|
||||
(if (endp list) (list new-element)
|
||||
(do ((head (list (first list)) (cons (first tail) head))
|
||||
(tail (rest list) (rest tail)))
|
||||
((or (endp tail) (funcall test old-element (first head)))
|
||||
(nreconc head (cons new-element tail))))))
|
||||
|
||||
(defun ninsert-after (new-element old-element list &key (test 'eql))
|
||||
"Like insert-after, but modifies list in place. If list is empty, a
|
||||
new list containing just new-element is returned."
|
||||
(if (endp list) (list new-element)
|
||||
(do ((prev list next)
|
||||
(next (cdr list) (cdr next)))
|
||||
((or (null next) (funcall test old-element (car prev)))
|
||||
(rplacd prev (cons new-element next))
|
||||
list))))
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(defun simple-insert-after (new-element old-element list &key (test 'eql))
|
||||
(let ((tail (rest (member old-element list :test test))))
|
||||
(nconc (ldiff list tail)
|
||||
(cons new-element tail))))
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
(defun insert-after (list new existing &key (test #'eql))
|
||||
"Insert item new into list, before existing, or at the end if existing
|
||||
is not present. The default comparison test function is EQL. This
|
||||
function destroys the original list and returns the new list."
|
||||
(cond
|
||||
;; case 1: list is empty: just return list of new
|
||||
((endp list)
|
||||
(list new))
|
||||
;; case 2: existing element is first element of list
|
||||
((funcall test (car list) existing)
|
||||
`(,(car list) ,new ,@(cdr list)))
|
||||
;; case 3: recurse: insert the element into the rest of the list,
|
||||
;; and make that list the new rest.
|
||||
(t (rplacd list (insert-before (cdr list) new existing :test test))
|
||||
list)))
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
struct SLinkedNode(T) {
|
||||
T data;
|
||||
typeof(this)* next;
|
||||
}
|
||||
|
||||
void insertAfter(T)(SLinkedNode!T* listNode, SLinkedNode!T* newNode) {
|
||||
newNode.next = listNode.next;
|
||||
listNode.next = newNode;
|
||||
}
|
||||
|
||||
void main() {
|
||||
alias N = SLinkedNode!char;
|
||||
|
||||
auto lh = new N('A', new N('B'));
|
||||
auto c = new N('C');
|
||||
|
||||
// Inserts C after A, creating the (A C B) list:
|
||||
insertAfter(lh, c);
|
||||
|
||||
// The GC will collect the memory.
|
||||
}
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
// Using the same type defs from the one way list example.
|
||||
|
||||
Type
|
||||
|
||||
// The pointer to the list structure
|
||||
pOneWayList = ^OneWayList;
|
||||
|
||||
// The list structure
|
||||
OneWayList = record
|
||||
pData : pointer ;
|
||||
Next : pOneWayList ;
|
||||
end;
|
||||
|
||||
// I will illustrate a simple function that will return a pointer to the
|
||||
// new node or it will return NIL. In this example I will always insert
|
||||
// right, to keep the code clear. Since I am using a function all operations
|
||||
// for the new node will be conducted on the functions result. This seems
|
||||
// somewhat counter intuitive, but it is the simplest way to accomplish this.
|
||||
|
||||
Function InsertNode(VAR CurrentNode:pOneWayList): pOneWayList
|
||||
begin
|
||||
|
||||
// I try not to introduce different parts of the language, and keep each
|
||||
// example to just the code required. in this case it is important to use
|
||||
// a try/except block. In any OS that is multi-threaded and has many apps
|
||||
// running at the same time, you cannot rely on a call to check memory available
|
||||
// and then attempting to allocate. In the time between the two, another
|
||||
// program may have grabbed the memory you were trying to get.
|
||||
|
||||
Try
|
||||
// Try to allocate enough memory for a variable the size of OneWayList
|
||||
GetMem(Result,SizeOf(OneWayList));
|
||||
Except
|
||||
On EOutOfMemoryError do
|
||||
begin
|
||||
Result := NIL
|
||||
exit;
|
||||
end;
|
||||
end;
|
||||
|
||||
// Initialize the variable.
|
||||
Result.Next := NIL ;
|
||||
Reuslt.pdata := NIL ;
|
||||
|
||||
// Ok now we will insert to the right.
|
||||
|
||||
// Is the Next pointer of CurrentNode Nil? If it is we are just tacking
|
||||
// on to the end of the list.
|
||||
|
||||
if CurrentNode.Next = NIL then
|
||||
CurrentNode.Next := Result
|
||||
else
|
||||
// We are inserting into the middle of this list
|
||||
Begin
|
||||
Result.Next := CurrentNode.Next ;
|
||||
CurrentNode.Next := result ;
|
||||
end;
|
||||
end;
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
def insertAfter(head :LinkedList ? (!head.null()),
|
||||
new :LinkedList ? (new.next().null())) {
|
||||
new.setNext(head.next())
|
||||
head.setNext(new)
|
||||
}
|
||||
|
||||
def a := makeLink(1, empty)
|
||||
def b := makeLink(2, empty)
|
||||
def c := makeLink(3, empty)
|
||||
|
||||
insertAfter(a, b)
|
||||
insertAfter(a, c)
|
||||
|
||||
var x := a
|
||||
while (!x.null()) {
|
||||
println(x.value())
|
||||
x := x.next()
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
: list-append ( previous new -- )
|
||||
[ swap next>> >>next drop ] [ >>next drop ] 2bi ;
|
||||
|
||||
SYMBOLS: A B C ;
|
||||
|
||||
A <linked-list>
|
||||
[ C <linked-list> list-append ] keep
|
||||
[ B <linked-list> list-append ] keep
|
||||
.
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
class Node
|
||||
{
|
||||
const Int value
|
||||
Node? successor // can be null, for end of series
|
||||
|
||||
new make (Int value, Node? successor := null)
|
||||
{
|
||||
this.value = value
|
||||
this.successor = successor
|
||||
}
|
||||
|
||||
// insert method for this problem
|
||||
public Void insert (Node newNode)
|
||||
{
|
||||
newNode.successor = this.successor
|
||||
this.successor = newNode
|
||||
}
|
||||
}
|
||||
|
||||
// simple class to test putting 'c' between 'a' and 'b'
|
||||
class Main
|
||||
{
|
||||
public static Void main ()
|
||||
{
|
||||
c := Node (2)
|
||||
b := Node (3)
|
||||
a := Node (1, b)
|
||||
a.insert (c)
|
||||
|
||||
echo (a.value)
|
||||
echo (a.successor.value)
|
||||
echo (a.successor.successor.value)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
\ Create the list and some list elements
|
||||
create A 0 , char A ,
|
||||
create B 0 , char B ,
|
||||
create C 0 , char C ,
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
B A chain
|
||||
C B chain
|
||||
|
|
@ -0,0 +1 @@
|
|||
: chain ( a b -- ) 2dup @ swap ! ! ;
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
elemental subroutine addAfter(nodeBefore,value)
|
||||
type (node), intent(inout) :: nodeBefore
|
||||
real, intent(in) :: value
|
||||
type (node), pointer :: newNode
|
||||
|
||||
allocate(newNode)
|
||||
newNode%data = value
|
||||
newNode%next => nodeBefore%next
|
||||
nodeBefore%next => newNode
|
||||
end subroutine addAfter
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Ele struct {
|
||||
Data interface{}
|
||||
Next *Ele
|
||||
}
|
||||
|
||||
func (e *Ele) insert(data interface{}) {
|
||||
if e == nil {
|
||||
panic("attept to modify nil")
|
||||
}
|
||||
e.Next = &Ele{data, e.Next}
|
||||
}
|
||||
|
||||
func (e *Ele) printList() {
|
||||
if e == nil {
|
||||
fmt.Println(nil)
|
||||
return
|
||||
}
|
||||
fmt.Printf("(%v", e.Data)
|
||||
for {
|
||||
e = e.Next
|
||||
if e == nil {
|
||||
fmt.Println(")")
|
||||
return
|
||||
}
|
||||
fmt.Print(" ", e.Data)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
h := &Ele{"A", &Ele{"B", nil}}
|
||||
h.printList()
|
||||
h.insert("C")
|
||||
h.printList()
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
class NodeList {
|
||||
private enum Flag { FRONT }
|
||||
private ListNode head
|
||||
void insert(value, insertionPoint=Flag.FRONT) {
|
||||
if (insertionPoint == Flag.FRONT) {
|
||||
head = new ListNode(payload: value, next: head)
|
||||
} else {
|
||||
def node = head
|
||||
while (node.payload != insertionPoint) {
|
||||
node = node.next
|
||||
if (node == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"Insertion point ${afterValue} not already contained in list")
|
||||
}
|
||||
}
|
||||
node.next = new ListNode(payload:value, next:node.next)
|
||||
}
|
||||
}
|
||||
String toString() { "${head}" }
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
def list = new NodeList()
|
||||
list.insert('B')
|
||||
list.insert('A')
|
||||
println list
|
||||
|
||||
list.insert('C', 'A')
|
||||
println list
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
insertAfter a b (c:cs) | a==c = a : b : cs
|
||||
| otherwise = c : insertAfter a b cs
|
||||
insertAfter _ _ [] = error "Can't insert"
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
record Node (value, successor)
|
||||
|
||||
procedure insert_node (node, newNode)
|
||||
newNode.successor := node.successor
|
||||
node.successor := newNode
|
||||
end
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
class Node (value, successor)
|
||||
|
||||
method insert (node)
|
||||
node.successor := self.successor
|
||||
self.successor := node
|
||||
end
|
||||
|
||||
initially (value, successor)
|
||||
self.value := value
|
||||
self.successor := successor
|
||||
end
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
list=: 1 65,:_ 66
|
||||
A=:0 NB. reference into list
|
||||
B=:1 NB. reference into list
|
||||
insertAfter=: monad define
|
||||
'localListName localListNode localNewValue'=. y
|
||||
localListValue=: ".localListName
|
||||
localOldLinkRef=: <localListNode,0
|
||||
localNewLinkRef=: #localListValue
|
||||
localNewNode=: (localOldLinkRef { localListValue), localNewValue
|
||||
(localListName)=: (localNewLinkRef localOldLinkRef} localListValue), localNewNode
|
||||
)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
void insertNode(Node<T> anchor_node, Node<T> new_node)
|
||||
{
|
||||
new_node.next = anchor_node.next;
|
||||
anchor_node.next = new_node;
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
LinkedList.prototype.insertAfter = function(searchValue, nodeToInsert) {
|
||||
if (this._value == searchValue) {
|
||||
nodeToInsert.next(this.next());
|
||||
this.next(nodeToInsert);
|
||||
}
|
||||
else if (this.next() == null)
|
||||
throw new Error(0, "value '" + searchValue + "' not found in linked list.")
|
||||
else
|
||||
this.next().insertAfter(searchValue, nodeToInsert);
|
||||
}
|
||||
var list = createLinkedListFromArray(['A','B']);
|
||||
list.insertAfter('A', new LinkedList('C', null));
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
to insert :after :list :value
|
||||
localmake "tail member :after :list
|
||||
if not empty? :tail [.setbf :tail fput :value bf :tail]
|
||||
output :list
|
||||
end
|
||||
|
||||
show insert 5 [3 5 1 8] 2
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
Append[{a, b}, c]
|
||||
->{a, b, c}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
let rec insert_after a b = function
|
||||
c :: cs when a = c -> a :: b :: cs
|
||||
| c :: cs -> c :: insert_after a b cs
|
||||
| [] -> raise Not_found
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
type
|
||||
pCharNode = ^CharNode;
|
||||
CharNode = record
|
||||
data: char;
|
||||
next: pCharNode;
|
||||
end;
|
||||
|
||||
(* This procedure inserts a node (newnode) directly after another node which is assumed to already be in a list.
|
||||
It does not allocate a new node, but takes an already allocated node, thus allowing to use it (together with
|
||||
a procedure to remove a node from a list) for splicing a node from one list to another. *)
|
||||
procedure InsertAfter(listnode, newnode: pCharNode);
|
||||
begin
|
||||
newnode^.next := listnode^.next;
|
||||
listnode^.next := newnode;
|
||||
end;
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
var
|
||||
A, B: pCharNode;
|
||||
begin
|
||||
(* build the two-component list A->C manually *)
|
||||
new(A);
|
||||
A^.data := 'A';
|
||||
new(A^.next);
|
||||
A^.next^.data := 'C';
|
||||
A^.next^.next := nil;
|
||||
|
||||
(* create the node to be inserted. The initialization of B^.next isn't strictly necessary
|
||||
(it gets overwritten anyway), but it's good style not to leave any values undefined. *)
|
||||
new(B);
|
||||
node^.data := 'B';
|
||||
node^.next := nil;
|
||||
|
||||
(* call the above procedure to insert node B after node A *)
|
||||
InsertAfter(A, B);
|
||||
|
||||
(* delete the list *)
|
||||
while A <> nil do
|
||||
begin
|
||||
B := A;
|
||||
A := A^.next;
|
||||
dispose(B);
|
||||
end
|
||||
end.
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
my $letters = 'A' => 'C' => Mu;
|
||||
|
||||
sub insert-after($list, $after, $new) {
|
||||
loop (my $l = $list; $l; $l = $l.value) {
|
||||
if $l.key eqv $after {
|
||||
$l.value = $new => $l.value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
die "Element $after not found";
|
||||
}
|
||||
|
||||
$letters.&insert-after('A', 'B');
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
my @l = ($A, $B);
|
||||
push @l, $C, splice @l, 1;
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
sub insert_after {
|
||||
# first argument: node to insert after
|
||||
# second argument: node to insert
|
||||
$_[1]{next} = $_[0]{next};
|
||||
$_[0]{next} = $_[1];
|
||||
}
|
||||
|
||||
my %B = (
|
||||
data => 3,
|
||||
next => undef, # not a circular list
|
||||
);
|
||||
my %A = (
|
||||
data => 1,
|
||||
next => \%B,
|
||||
);
|
||||
my %C = (
|
||||
data => 2,
|
||||
);
|
||||
insert_after \%A, \%C;
|
||||
|
|
@ -0,0 +1 @@
|
|||
insert_after \%A, { data => 2 };
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
sub insert_after {
|
||||
my $node = $_[0];
|
||||
my $next = $node->{next};
|
||||
shift;
|
||||
while (defined $_[0]) {
|
||||
$node->{next} = $_[0];
|
||||
$node = $node->{next};
|
||||
shift;
|
||||
}
|
||||
$node->{next} = $next;
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
my %list = ( data => 'A' );
|
||||
insert_after \%list, { data => 'B' }, { data => 'C' };
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
my $list2;
|
||||
|
||||
# create a new list ('A'. 'B', 'C') and store it in $list2
|
||||
insert_after $list2 = { data => 'A' }, { data => 'B' }, { data => 'C' };
|
||||
|
||||
# append two new nodes ('D', 'E') after the first element
|
||||
insert_after $list2, { data => 'A2' }, { data => 'A3' };
|
||||
|
||||
# append new nodes ('A2a', 'A2b') after the second element (which now is 'A2')
|
||||
insert_after $list2->{next}, { data => 'A2a' }, { data => 'A2b' };
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(de insertAfter (Item Lst New)
|
||||
(when (member Item Lst)
|
||||
(con @ (cons New (cdr @))) )
|
||||
Lst )
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(de insertAfter (Item Lst New)
|
||||
(if (index Item Lst)
|
||||
(conc (cut @ 'Lst) (cons New Lst))
|
||||
Lst ) )
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
define insert_into_list(anchor, x);
|
||||
cons(x, back(anchor)) -> back(anchor);
|
||||
enddefine;
|
||||
;;; Build inital list
|
||||
lvars l1 = cons("a", []);
|
||||
insert_into_list(l1, "b");
|
||||
;;; insert c
|
||||
insert_into_list(l1, "c");
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
uses objectclass;
|
||||
define :class ListNode;
|
||||
slot value = [];
|
||||
slot next = [];
|
||||
enddefine;
|
||||
|
||||
define insert_into_List(anchor, x);
|
||||
consListNode(x, next(anchor)) -> next(anchor);
|
||||
enddefine;
|
||||
;;; Build inital list
|
||||
lvars l2 = consListNode("a", []);
|
||||
insert_into_List(l2, "b");
|
||||
;;; insert c
|
||||
insert_into_List(l2, "c");
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
Procedure insertAfter(Value, *node.MyData = #Null)
|
||||
Protected *newNode.MyData = AllocateMemory(SizeOf(MyData))
|
||||
If *newNode
|
||||
If *node
|
||||
*newNode\next = *node\next
|
||||
*node\next = *newNode
|
||||
EndIf
|
||||
*newNode\Value = Value
|
||||
EndIf
|
||||
ProcedureReturn *newNode ;return pointer to newnode
|
||||
EndProcedure
|
||||
|
||||
|
||||
Define *SL_List.MyData, a = 1, b = 2, c = 3
|
||||
|
||||
*SL_List = insertAfter(a) ;start the list
|
||||
insertAfter(b, *SL_List) ;insert after head of list
|
||||
insertAfter(c, *SL_List) ;insert after head of list and before tail
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
def chain_insert(lst, at, item):
|
||||
while lst is not None:
|
||||
if lst[0] == at:
|
||||
lst[1] = [item, lst[1]]
|
||||
return
|
||||
else:
|
||||
lst = lst[1]
|
||||
raise ValueError(str(at) + " not found")
|
||||
|
||||
chain = ['A', ['B', None]]
|
||||
chain_insert(chain, 'A', 'C')
|
||||
print chain
|
||||
|
|
@ -0,0 +1 @@
|
|||
['A', ['C', ['B', None]]]
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
class ListNode
|
||||
def insert_after(search_value, new_value)
|
||||
if search_value == value
|
||||
self.succ = self.class.new(new_value, succ)
|
||||
elsif self.succ.nil?
|
||||
raise StandardError, "value #{search_value} not found in list"
|
||||
else
|
||||
self.succ.insert_after(search_value, new_value)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
list = ListNode.new(:a, ListNode.new(:b))
|
||||
list.insert_after(:a, :c)
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
object Node {
|
||||
def insert(a: Node, c: Node) = {
|
||||
c.next = a.next
|
||||
a.next = c
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
(define (insert-after a b lst)
|
||||
(if (null? lst)
|
||||
lst ; This should be an error, but we will just return the list untouched
|
||||
(let ((c (car lst))
|
||||
(cs (cdr lst)))
|
||||
(if (equal? a c)
|
||||
(cons a (cons b cs))
|
||||
(cons c (insert-after a b cs))))))
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(define (insert-after! a b lst)
|
||||
(let ((pos (member a lst)))
|
||||
(if pos
|
||||
(set-cdr! pos (cons b (cdr pos))))))
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
# Assume rest of definition is already present
|
||||
oo::define List method insertAfter element {
|
||||
$element attach $next
|
||||
set next $element
|
||||
}
|
||||
|
||||
set A [List new "A" [List new "B"]]
|
||||
$A insertAfter [List new "C"]
|
||||
Loading…
Add table
Add a link
Reference in a new issue