开发者

help with lists?

开发者 https://www.devze.com 2022-12-23 00:44 出处:网络
im learning linked lists in java but am stuck on the following Before deleting Hobnobs: bourbon, shortbread, Hobnobs, oreos

im learning linked lists in java but am stuck on the following

Before deleting Hobnobs: bourbon, shortbread, Hobnobs, oreos

After deleting Hobnobs: bourbon, shortbread, oreos

i would like to create a delete method that will delete the intermediate node "Hobnobs".

ive got this so far

 public class Biscuit {

 private BiscuitNode first;
 public Biscuit( )
 {
  this.first=null;
 }
 public BiscuitNode getFirst() {
  return first;
 }
 public void insert(BiscuitNode first) {
  this.first = first;
 }

 public void deleteFirst()
 {
  this.first.setSucc(this.first);
 }

 public void delete(String BiscuitName)
 {

the Hobnobs is "BiscuitNode secondLast=new BiscuitNo开发者_Go百科de("Hobnobs", last);"


Walk the chain of nodes in a loop using getSucc() on the current node. Then, once you find the node that matches, make its predecessor point to its successor. E.g. if we want to delete node C from the following list:

A--->B--->C--->D

becomes

A--->B--->D

You should be able to figure it out.


Add a recursive method to BiscuitNode, maybe named deleteYourSuccessorIfItMatches(String pattern) that checks if it's successor matches the pattern and if it does, the successor of the current node would be set to the successor of the successor, else the method is called recursively on the successor. The calling method, Biscuit.delete(String pattern) has to do the same thing on the first node.

0

精彩评论

暂无评论...
验证码 换一张
取 消