Created
July 25, 2023 14:28
-
-
Save L8RFN/6ab180adb33015627e9abc39d9722a97 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class node { | |
int data; | |
node next; | |
} | |
class Slist { | |
static node head; | |
node tail; | |
int count; | |
public Slist(){ | |
head=tail=null; | |
count=0; | |
} | |
public boolean isEmpty(){ | |
return (head==null); | |
} | |
public void insertToHead(int val){ | |
if(isEmpty()){ | |
head= new node(); | |
head.data=val; | |
head.next=null; | |
tail=head; | |
} | |
else{ | |
node nn= new node(); | |
nn.data=val; | |
nn.next=head; | |
head=nn; | |
} | |
count++; | |
} | |
public void insertToTail(int val){ | |
if(isEmpty()) | |
{ | |
insertToHead(val); | |
return; | |
} | |
else{ | |
node nn=new node(); | |
nn.data=val; | |
nn.next=null; | |
tail.next=nn; | |
tail=nn; | |
} | |
count++; | |
} | |
public void insertToPos(int val, int pos){ | |
if(pos<1 || pos>count+1) | |
System.out.println("wrong position"); | |
else{ | |
if(pos==1) | |
insertToHead(val); | |
else if (pos==count+1) | |
insertToTail(val); | |
else{ | |
node nn= new node(); | |
nn.data=val; | |
node t=head; | |
for(int i=1; i<pos-1;i++) | |
t=t.next; | |
nn.next=t.next; | |
t.next=nn;//this line and the one above must be in this exact order. | |
count++; | |
} | |
} | |
} | |
public int deleteFromHead(){ | |
int x=-1;//you can put -1000, -999, etc... | |
if(!isEmpty()) | |
{ | |
x=head.data;// to get back the deleted value if needed. | |
if(head==tail) | |
head=tail=null;//the whole "array" got deleted. | |
else | |
{ | |
node del=head;//this | |
head=head.next; | |
del=null;//and this line, are to make sure that the data got deleted completely. | |
//with out them, the data is just inaccessible but it does exist in the memory. | |
} | |
count--; | |
} | |
return x; | |
} | |
public int deleteFromTail(){ | |
int x=-1; | |
if(!isEmpty()) | |
{ | |
x=tail.data; | |
if(head==tail) | |
head=tail=null; | |
else | |
{ | |
node t=head; | |
while(t.next!=tail) | |
t=t.next; | |
node del=t.next; | |
tail=t; | |
tail.next=null;//every "array" must ends with null '/0', to let the compiler know that this is the end of the array. | |
//if not, then the compiler will continue to print rubbish data after the tail. | |
del=null; | |
} | |
count--; | |
} | |
return x; | |
} | |
public int deleteFromPos(int pos){ | |
int x=-1; | |
if(pos<1 || pos>count)//notice that here, pos>count, not count+1, In the other hand InsertToPos was count+1. | |
//because we can't delete something that does not exist but we can insert. | |
//NOT IMPORTANT LINE//insert(2,8) in array of count 7 means put 2 as an extra node after the already existing tail. | |
//NOT IMPORTANT LINE//delete(8) in array of count 7 means delete the 8th node , which is does not exist. | |
System.out.println("wrong position"); | |
else | |
{ | |
if(pos==1) | |
return(deleteFromHead()); | |
else | |
if(pos==count) | |
return(deleteFromTail()); | |
else | |
{ | |
node t=head; | |
for(int i=1;i<pos-1;i++) | |
t=t.next; | |
node del=t.next; | |
x=del.data; | |
t.next= del.next; | |
del=null; | |
count--; | |
} | |
} | |
return x; | |
} | |
public static void print(){ | |
node t=head; | |
while(t!=null){ | |
System.out.println(t.data); | |
t=t.next; | |
} | |
} | |
public int counter (){ | |
while (node.next) | |
} | |
} | |
class Main | |
{ | |
public static void main(String[] args) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment