Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Linked Lists

  • 17-08-2000 3:24pm
    #1
    Closed Accounts Posts: 1,484 ✭✭✭


    Ok then big brainy programmer types.

    I'm trying to get my head around linked lists and have a class called Element which holds and interger and a refernce to to the next elemnt.

    Could anyone give me the code to use this to construct a linked list of integers and then dup this information into an array?

    And no, I'm not cheating on any college work, this is just for my own interest.


Comments

  • Registered Users, Registered Users 2 Posts: 332 ✭✭spod


    what language, and any chance of letting us see the class? or are at least the interface details?

    It should make it alot easier to whip together the necessary code..


  • Closed Accounts Posts: 1,484 ✭✭✭El_Presidente


    Java

    public class Element(int value, Element next)


  • Registered Users, Registered Users 2 Posts: 2,660 ✭✭✭Baz_


    hmmmmmmmm, don't know java, but at last there is a good programming question on the board.


  • Registered Users, Registered Users 2 Posts: 310 ✭✭Cerberus


    It is easy enough to do in C++ (a bit messy though) so I imagine it should be a hell of a lot easier and more straight forward in Java given all the fancy memory management stuff I heard it has. Come on Javamen, show us what your made of and help El_P!!


  • Closed Accounts Posts: 1,484 ✭✭✭El_Presidente


    Yes indeed, lets all pool are collective programming abilities together and help El_Pres!


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 3,308 ✭✭✭quozl


    This is all done off the top of my head without rereading, so expect typos/wrong variable names, and it not to compile. But this is the code you want for C. If you know some java should be easy to translate.


    struct element{
    int value;
    element *next_element;
    }

    void main(){
    element first, current;
    first->next_element=NULL;
    first.value=10;
    current=first;

    }

    // to find one in particular given a
    // value //to find
    element *findit(int value, element *first){
    element *loop;
    loop=first;
    while (loop) { //if not NULL
    if (loop.value == value)
    return &loop;
    loop=loop->next; // next in list
    }
    return (NULL);
    }

    void add_one(int value, element *first){
    element *loop, *new;
    loop=first;
    new.value=value;
    if (!loop){
    printf("Empty list, bold.\n\r");
    return;
    }
    while (loop->next) //long as more
    loop=loop->next
    loop->next=new; //append new element
    return;
    }

    to dump into array just iterate through the list, counting elements, then malloc (memory allocate) the correct size array. SImpler in java I think.
    to copy code use something like
    (assumning my_array[?] is global and right size from above)

    void dump(element *first){
    element *loop=first;
    int i=0; //arrays start at 0
    while (loop){
    my_array=loop.value;
    loop=loop->next;
    i++;
    }
    }
    this doesnt check for array overruns so make sure you have already made correct size array or instead resize array on the fly.


  • Registered Users, Registered Users 2 Posts: 332 ✭✭spod


    gah busy atm.

    if i get a chance I'ill do up something over weekend.

    How's that?

    ps. java, om nyomgh.


  • Closed Accounts Posts: 14 doon


    Here is a link to a sample linked list written in java. http://www.cs.waikato.ac.nz/~312/lectures/lecture2/sld001.htm

    What you should use is the Vector class in java.util. I does all the stuff you can do with a linked list.
    doon.


Advertisement