我在这里已经有了一个工作链表:
Single_Linked_List := Object clone do(
head ::= nil;
// constructor
init := method(
head = nil
);
// methods
isEmpty := method(
self head == nil
);
empty := method(
self head = nil
);
insert_at_tail := method(n,
if(self head == nil,
self setHead(Linked_List_Node clone)
);
self head insert_at_tail(n);
);
insert_at_head := method(n,
newnode := Linked_List_Node clone;
if(self head == nil,
self setHead(newnode),
newnode link = self head;
self setHead(newnode)
);
self head insert_at_head(n);
);
remove_at_head := method(
if(self head == nil, return nil);
if(self head link == nil, self head value println; self setHead(nil), self setHead(self head link));
);
)
)
Linked_List_Node := Object clone do(
value ::= nil;
link ::= nil;
// constructor
init := method(
value = nil;
link = nil
);
// methods
insert_at_tail := method(n,
if(self value == nil,
self setValue(n),
if (self link == nil,
self setLink(Linked_List_Node clone)
);
self link insert_at_tail(n)
);
);
insert_at_head := method(n,
self setValue(n));
push := method(n,
self setValue(n);
);
enqueue := method(n,
self setValue(n));
)
Stack_2 := Object Single_Linked_List clone do(
head ::= nil;
// constructor
init := method(
head = nil
);
// methods
isEmpty := method(
self head == nil
);
push := method(n,
newhead := Linked_List_Node clone;
if(self head == nil, newhead link = nil, newhead link = self head
);
self setHead(newhead);
self head push(n);
);
pop := method(
if(self head == nil, return nil);
if(self head link == nil, self head value println, self head value println; setHead(self head link)
);
)
)
Queue_2 := Single_Linked_List clone do(
head ::= nil;
tail ::= nil;
// constructor
init := method(
head = nil;
tail = nil
);
// methods
enqueue := method(n,
newnode := Linked_List_Node clone;
if(self tail == nil, self setHead(newnode); self setTail(newnode); newnode link = nil, self tail link = newnode; self setTail(newnode)
);
self tail enqueue(n);
);
dequeue := method(
if(self head == nil, return nil, self head value println; setHead(self head link));
)
)
现在我需要实现一个堆栈和一个队列。这是我第一次尝试使用这种语言,我很难理解我现在需要为堆栈和队列做什么。