#Issue turning a node linkedlist to generic type node linkedlist (this is what I tried)

163 messages · Page 1 of 1 (latest)

cobalt brook
#

public class LinkedListf<E> {

static class node <E>{
    E data;
    node<E> next;

    node(E value) {
        data = value;
        next = null;
    }
}
static node head;

// display the list
static void printList() {
    node p = head;
    System.out.print("\n[");

    //start from the beginning
    while(p != null) {
        System.out.print(" " + p.data + " ");
        p = p.next;
    }
    System.out.print("]");
}

//insertion at the beginning
void insertatbegin(E data) {

    //create a link
    node lk = new node(data);

    // point it to old first node
    lk.next = head;

    //point first to new first node
    head = lk;

    //pointing old node to new node
}

void deletedata(E data){

    node del = head;
    node prev = null;
    if(del != null && del.data == data){
        head= del.next;
        return;
    }

    while (del != null && del.data != data){
        prev = del;
        del= del.next;

    }

    prev.next=del.next;

}
 void insertafter(E index, E data){

    node sert = head;
    //node prev = null;
    node ins = new node(data);

    while (sert != null){
        if(sert.data == index){

            ins.next = sert.next;
            sert.next = ins;

            break;

        }
        sert = sert.next;

    }


}

public static void main(String args[]) {
    int k=0;

    insertatbegin(12);
    insertatbegin(22);
    insertatbegin(30);
    deletedata(12);
    insertafter(44, 2);
    System.out.println("Linked List: ");


    // print list
    printList();
}

} //issue is im not sure if its correct, and I get a cannot run from a static point because I had to change the functions from static when trying to call the functions in MAIN as is.

marble heartBOT
#

This post has been reserved for your question.

Hey @cobalt brook! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

void cipher
#

do check #❗︱how-to-get-help too

marble heartBOT
#

Please format your code & make it more readable.
For the java programming language, it should look like this:

```java
public class Main{
public static void main(String[] args){
System.out.println("Hello World!");
}
}```
• These are backticks, not quotes.

void cipher
#

you didn't say what your issue was

cobalt brook
#

is it not the title?

#

or did I miss a part?

void cipher
#

you said there was an issue

#

you didn't say what it was

#

we can't really help you if we don't know what to help with

cobalt brook
#

I put at the bottom of the code

void cipher
#

see that's an issue with not formatting your code but alright

#

you're not supposed to run the functions from main.

#

you're supposed to run the functions from a linkedlist.

#

that's how OOP works

cobalt brook
#

I got it to work

#

Am I allowed to post the code for you to see? here @void cipher

void cipher
#

for a code review?

cobalt brook
#

yes

void cipher
#

sure

cobalt brook
void cipher
#

you're using the raw type in main

#

printlist is still static

cobalt brook
void cipher
#

node should be PascalCase

#

overall formatting could be better

#

methods should be in camelCase

cobalt brook
#

I haven't learned PascalCase

void cipher
#

index should not be generic

void cipher
#

camelCaseIsThisCasing

#

just like i typed it

cobalt brook
#

i usual call it camelBack notation, was confused but i get it

void cipher
#

that's an alternate name of camelCase

#

PascalCase is different

#

it capitalizes the first word

cobalt brook
#

so it's Node not node?

void cipher
#

yes

cobalt brook
#

at the class or all of them?

void cipher
#

if you change it at the class, node ceases to exist

#

you have to change it everywhere

#

you can use F2 i believe to use the ide tool to rename the symbol though

#

that changes the name at all uses

cobalt brook
#

yes

#

i did it

#

also It needs to be able to input integers and strings

#

is it good as is or do I need to create a object for the list?

void cipher
#

you already have

#

that's what new LinkedListf() does

cobalt brook
#

oh sht I forgot i put that in

#

bare with me im kind of special

#

so its good ?

void cipher
#

well there's all the issues i described above

void cipher
void cipher
#

other than that they're just stylistic issues

cobalt brook
#

I'm doomed

void cipher
#

check out that section

cobalt brook
#

oki doke

#

LinkedListf<String> StringList = new LinkedListf<>();

#

would that be generic instead of :

#

LinkedListf listf = new LinkedListf();

#

?

void cipher
#

yes

cobalt brook
#

ah ha i GET IT NOW

#

but would i get added to another list?

void cipher
#

what?

cobalt brook
#

so since it doesn't do this anymore

#

would one object list have all strings and another have all intergers?

void cipher
#

depends on what you need

#

if you need both strings and integers in the same list you can do that

#

do you want a single list with both or 2 lists each with their own type?

cobalt brook
#

Im guessing that means two different lists

void cipher
#

shrugging pretty vague but that seems like a reasonable intent

cobalt brook
#

is this what it should do?

void cipher
#

sure

cobalt brook
#

I didn't even call String list

void cipher
#

variables should be camelCase though

void cipher
#

i just checked your list declarations lol

#

also your printlist is still static

#

ah so is your head

#

you still can only ever have 1 list

cobalt brook
#

this is both intList and stringList

void cipher
#

yeah you still only have 1 linkedlist

#

because you're still using static stuff for your list

#

this is like the first issue i mentioned

cobalt brook
#

I still had a static head

#

sorry lol

#

but its that correct?

void cipher
#

check your warnings

#

that's a raw type

#

you're using raw types for Node everywhere else, too

cobalt brook
void cipher
#

yeah

cobalt brook
#

You're alot smarter than me idk how to fix it

void cipher
#

you've solved this problem before

#

about 20 minutes ago

cobalt brook
#

I was about to slap this bitch in for submission

cobalt brook
void cipher
#

yep

void cipher
cobalt brook
#

i can't even refractor lol give me some time

#

This is the best I could do

void cipher
#

is index still E

cobalt brook
#

yes

#

It looks so good there i don't wanna change it 😦

void cipher
#

are you going to have string indices when you have string values

cobalt brook
#

I don't know what indices are

void cipher
#

plural of index, same as indexes

#

english weird

void cipher
#

bruh

#

no, no you aren't

cobalt brook
#

whaaa

void cipher
#

"insert value "text" at position "words"" 💀

#

that doesn't make sense for a list, no

#

indexes are always integers, for java that's int

cobalt brook
void cipher
#

those wouldn't be indices then

cobalt brook
#

it won't allow me to put index function variable as int

void cipher
#

those would be values

#

indices are integers, what you have there is not an index

cobalt brook
#

i have function(E index)

void cipher
cobalt brook
#

i know i just randomly named it that

void cipher
#

wait, do you not have any actual index-based methods

#

that's... an odd design choice, to say the least

cobalt brook
#

i was suppose to name it replace

void cipher
#

what?

#

that's not a replacement

cobalt brook
#

i don't know what I mean ok

void cipher
#

no, that isn't ok

cobalt brook
#

this is it

#

what would I name it?

void cipher
#

after or element/elem probably

cobalt brook
#

ok i'll change it

void cipher
#

all the others too

cobalt brook
#

yes i refractored all of indices to after

void cipher
#

don't you have an insertBefore

#

that would be before

cobalt brook
#

i deleted that

#

i don't get how previous works

void cipher
#

....you didn't need that?

cobalt brook
#

for the delete method

#

my teacher wrote that

#

i don't understand it

#

the delete function has a prev to go back through the list but I don't understand it

void cipher
#

try debugging it

cobalt brook
#

how i do dat?

marble heartBOT
void cipher
#

it lets you step through the process line-by-line

cobalt brook
#

I debugged the prev variable

void cipher
#

you might need breakpoints to do line-by-line

cobalt brook
#

I've never really studied that

void cipher
#

yes