.java to .c conversion

Discussion in 'OT Technology' started by darkrick, Mar 22, 2005.

  1. darkrick

    darkrick New Member

    Joined:
    Sep 17, 2004
    Messages:
    1,191
    Likes Received:
    0
    Location:
    Brisbane, AU
    Ok, I got a Linked List in Java, and I'd be very gratefull if someone could convert the following two classes into the C equivilant.


    MyList.java
    Code:
    public class MyList {
      
      private Node head;
      private Node tail;
      private Node previous;
      private int size;
      
      public MyList() {
        head = null;
        tail = null;
        size = 0;
      }
      
      public int size(){
        return size; 
      }
      
      public void add(Object o) {
        Node temp = new Node(o, null);
        if (size == 0) {
          head = temp;
          tail = temp;
        } else {
          previous = tail;
          previous.setNext(temp);
          tail = temp;
        }
        size ++;
      }
      
      public Object get(int i) {
        if (i == 0) {
          return head.getObject();
        } else if (i == (size-1)) {
          return tail.getObject();
        } else {
          Node temp = head;
          for (int j = 0; j < i; j++) {
            temp = temp.getNext(); 
          }
          return temp.getObject();
        }
      }
      
      public void remove(int i) {
        if (i == 0) {
          head = head.getNext();
        } else if (i == (size-1)) {
          tail = previous;
          tail.setNext(null);
        } else {
          Node temp = head;
          for (int j = 0; j < (i-1); j++) {
            temp = temp.getNext();
          }
         temp.setNext((temp.getNext()).getNext());
        }
        size --;
      }
    }
    Node.java
    Code:
    public class Node {
      private Object element;
      private Node next;
      
      public Node(Object o, Node n) {
        element = o;
        next = n;
      }
      
      public Object getObject() {
        return element;  
      }
      
      public void setObject(Object o){
       element = o; 
      }
      
      public Node getNext(){
        return next; 
      }
      
      public void setNext(Node n){
        next = n;
      }
    }
     
  2. I'd be grateful if someone would deposit a million dollars in my bank account.

    *waiting*
     
  3. SL1200MK4

    SL1200MK4 New Member

    Joined:
    Sep 27, 2003
    Messages:
    1,552
    Likes Received:
    0
    Java is OO and C is not. So, how the hack will that be possible unless you modify the interface or do it in C++?

    Also, did you ever try to STFW? If you need OO, then you will need to use C++, and in such case, look into C++ STL, it's already there.
     
  4. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    Just search Google for a Linked List implementation in C. A good C reference online is cprogramming.com
     

Share This Page