实现了链表。需要帮助创建堆栈和队列

时间:2012-07-22 20:26:17

标签: iolanguage

我已经在这里找到了一个工作链表:

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)); 

  )
)

现在我需要实现堆栈和队列。这是我第一次尝试使用这种语言,现在我很难绕过我需要为堆栈和队列做的事情。

1 个答案:

答案 0 :(得分:0)

Stack      := List clone
Stack push := method(i, insertAt(i, 0))
Stack pop  := method(removeFirst)

Io> stack := Stack clone
==> list()
Io> stack push(1) push(2) push(3)
==> list(3, 2, 1)
Io> stack pop
==> 3
Io> stack pop
==> 2
Io> stack push(4)
==> list(3, 4)
Io> stack pop
==> 4
Io> stack pop
==> 1
Io> stack pop
==> nil

和...

Queue         := List clone
Queue enqueue := method(i, insertAt(i, 0))
Queue dequeue := method(pop)

Io> queue := Queue clone
==> list()
Io> queue enqueue(1) enqueue(2) enqueue(3) enqueue(4)
==> list(4, 3, 2, 1)
Io> queue dequeue
==> 1
Io> queue dequeue
==> 2
Io> queue enqueue(5)
==> list(5, 4, 3)
Io> queue dequeue
==> 3
Io> queue dequeue
==> 4
Io> queue dequeue
==> 5
Io> queue dequeue
==> nil