Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 3rd, 2013, 03:29 PM
tritnat tritnat is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 tritnat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 42 m 21 sec
Reputation Power: 0
RemoveAfter - Linked List

Hi everyone. Nice to meet you. I'm starting studying Linked Lists with Sedgewick's book, Algorithms 4rd edition and I need to do the following assignment from the book:

Quote:

Write a method removeAfter() that takes a linked list Node as argument and
removes the node following the given one (and does nothing if the argument or the next
field in the argument node is null).


I think this one is a bit strange because the class Node is private. So, how would I code this method if I can't even create an object of it in the main source? Please help me with a pseudocode for this method. I don't understand linked lists yet.

Code:
package stack;

public class LinkedListStack<Item> {

    private Node first;
    private int N;

    private class Node {

        Item item;
        Node next;
    }

    public boolean isEmpty() {
        return first == null;
    }

    public int size() {
        return N;
    }

    public void delete(int k) {
        if (first == null) {
            return;
        }
        Node n = first;
        for (int i = 0; i < k - 1; i++) {
            if (n.next == null) {
                return;
            }
            n = n.next;
        }
        if (n.next == null) {
            return;
        }
        n.next = n.next.next;
        --N;
    }
    
    public boolean find(Item item) {

        Node n;
        for (n = first; n != null; n = n.next) {
            if (n.item == item) {
                return true;
            }
        }
        return false;
    }

    public void push(Item item) {
        Node oldfirst = first;
        first = new Node();
        first.item = item;
        first.next = oldfirst;
        N++;
    }

    public void peek() {
        System.out.println("Last node contains: " + first.item);
    }

    public Item pop() {
        Item item = first.item;
        first = first.next;
        N--;
        return item;
    }
}


Code:
package stack;

public class LinkedListStackTest {

    public static void main(String[] args) {
          
       boolean found = true;
       LinkedListStack<String> lls = new LinkedListStack();  
       
       lls.push("a");
       lls.push("b");
       lls.push("c");
       lls.push("d");
       
       lls.delete(2);
       
       found = lls.find("b");
       if(found)
        System.out.println("The element was found");
       else 
        System.out.println("The element wasn't found");
    }
}

Reply With Quote
  #2  
Old January 3rd, 2013, 04:37 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
Does the posted code compile and execute without errors?

The Node class is only for use by the LinkedListStack class. It is used to chain together Item objects in the linked list.

Reply With Quote
  #3  
Old January 3rd, 2013, 07:59 PM
tritnat tritnat is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 tritnat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 42 m 21 sec
Reputation Power: 0
Quote:
Originally Posted by NormR
Does the posted code compile and execute without errors.


yep.

Reply With Quote
  #4  
Old January 3rd, 2013, 08:06 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
First thing to do is use a pencil and paper and draw a diagram of a linked list.
Then work out the simple, single steps that need to be done to remove the desired item.
When you have the list of steps the program needs to do, then look at coding it.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > RemoveAfter - Linked List

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap