dont send me web address please. I need the code so if you can help, I will appr
ID: 3531217 • Letter: D
Question
dont send me web address please. I need the code so if you can help, I will appriciate it.
thanks in advence,
import java.awt.Color;
public class LinkedList {
ListNode first;
public LinkedList(){
}
public LinkedList(ListNode n){
first=n;
}
public ListNode makeNullNode(){
return new ListNode();
}
public void insert(ListNode n){
if (n == null){
throw new NullPointerException("Attempt to insert a null node failed");
}
if(first==null){
first =n;
}
else{
n.setNext(first);
first =n;
}
}
public void insertBefore(ListNode successor, ListNode n){
if (n == null){
throw new NullPointerException("Attempt to insert a null node failed.");
}
else if(first==null){
first =n;
}
else if(first==successor){
n.setNext(first);
first =n;
}
else{
ListNode a = first;
while(a!=null){
if(a.getNext()==successor){
n.setNext(a.getNext());
a.setNext(n);
return;
}
a=a.getNext();
}
throw new NullPointerException("Attempt to insert failed; missing target.");
}
}
public void delete(ListNode n){
if (n==null){
return;
}
if(n==first){
first=first.getNext();
return;
}
ListNode a = first;
while(a!=null){
if (a.getNext()==n){
a.setNext(a.getNext().getNext());
}
a=a.getNext();
}
}
public ListNode getFirst(){
return first;
}
public ListNode find(int d){
ListNode a = first;
return findNext(a,d);
}
public ListNode findNext(ListNode n, int d){
if (n==null){
return null;
}
ListNode a = n.getNext();
while(a!=null){
if(a.getData()==d){
return a;
}
a=a.getNext();
}
return null;
}
public boolean isEmpty(){
if (first==null){
return true;
}
else{
return false;
}
}
}
class ListNode {
private ListNode next;
private int data;
ListNode(){
set(null, 0);
}
ListNode(int i){
set(null, i);
}
ListNode(ListNode n, int i){
set(n, i);
}
private void set(ListNode n, int i){
next = n;
data = i;
}
public ListNode getNext(){
return next;
}
public void setNext(ListNode n){
next=n;
}
public boolean isEqual(ListNode l){
if(l==this){
return true;
}
else if(l.data==this.data){
return true;
}
else {
return false;
}
}
public int getData(){
return data;
}
public ListNode makeNode(int n){
return new ListNode (n);
}
public ListNode setNode(int n){
data = n;
return this;
}
}
Explanation / Answer
You should add a print backwards to prove that the list works in reverse.
You must remove the previous node in the insert and delete routines and use the previous pointer instead.
A node structure for a doubly linked list is:
struct Node{ int data;/ this can be any type data Node *next; Node *prev; }
struct node { /* a structure called node*/
char data; /* first member of the struct*/
struct node *nextPtr; /* second member of the struct which points back to the struct*/
};
struct listNode {
char data;
struct listNode *nextPtr;
struct listNode *prevPtr;}
void insert( ListNodePtr *sPtr, char value )
{
ListNodePtr newPtr;
ListNodePtr previousPtr;
ListNodePtr currentPtr;
newPtr = malloc(sizeof( ListNode ) );
if ( newPtr != NULL ) {
newPtr->data = value;
newPtr->nextPtr = NULL;
previousPtr = NULL;
currentPtr = *sPtr;
while ( currentPtr != NULL && value > currentPtr-> data) {
previousPtr = currentPtr;
currentPtr = currentPtr-> nextPtr;
}
if ( previousPtr == NULL ) {
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
}
else {
previousPtr-> nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
}
}
else {
printf( "%c not inserted. No memory available.|n", value );
}
}
previousPtr = NULL;
currentPtr = *sPtr;
while ( currentPtr != NULL && value > currentPtr-> data) {
previousPtr = currentPtr;
currentPtr = currentPtr-> nextPtr;
}
if ( previousPtr == NULL ) {
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
}
else {
previousPtr-> nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.