Wednesday, October 7, 2020

#346 To class doublyLinedList, add method

To class doublyLinedList, add method - Computer Science

ChemistryExplain daily providing Q&A content “#346 To class doublyLinedList, add method" in Computer science, Ba computer science, Berkeley computer science, Computer science associate degree jobs

ChemistryExplain “#346 To class doublyLinedList, add method in Computer science, Ba computer science, Berkeley computer science, Computer science

Get the Free Online Chemistry Q&A Questions And Answers with explain. To crack any examinations and Interview tests these Chemistry Questions And Answers are very useful. Here we have uploaded the Free Online Chemistry Questions. Here we are also given the all chemistry topic.

 ChemistryExplain team has covered all Topics related to inorganic, organic, physical chemistry, and others So, Prepare these Chemistry Questions and Answers with Explanation Pdf.

For More Chegg Questions

Free Chegg Question

Assignment
To class doublyLinedList, add method remove (E e) Which removes all nodes that has data e. This method has void return type.

DoublyLinkedList.java

package doubly;

public class DoublyLinkedList <E>{
private Node<E> header;
private Node<E> trailer;
private int size=0;
public DoublyLinkedList() {
header=new Node<>(null,null,null);
trailer=new Node<>(null,header,null);
header.setNext(trailer);
   }
public int size() { return size;}
public boolean isEmpty() {return size==0;}
public E first()
{
if (isEmpty()) return null;
return header.getNext().getData();
}
public E last()
{
if (isEmpty()) return null;
return trailer.getPrev().getData();
}

private void addBetween(E e, Node<E> predecessor, Node<E> successor)
{
Node<E> newest=new Node<>(e,predecessor,successor);
predecessor.setNext(newest);
successor.setPrev(newest);
size++;
}
private E remove(Node<E> node)
{
Node<E> predecessor=node.getPrev();
Node<E> successor=node.getNext();
predecessor.setNext(successor);
successor.setPrev(predecessor);
size--;
return node.getData();
}
public void addFirst(E e){
addBetween(e,header,header.getNext());
}
public void addLast(E e){
addBetween(e,trailer.getPrev(),trailer);
}
public E removeFirst(){
if(isEmpty()) return null;
return remove(header.getNext());
}
public E removeLast()
{
if(isEmpty()) return null;
return remove(trailer.getPrev());
}
public void printForward()
{
for (Node<E> tmp=header.getNext();tmp!=trailer;tmp=tmp.getNext())
System.out.println(tmp.getData());
}
public void printBackward()
{
for (Node<E> tmp=trailer.getPrev();tmp!=header;tmp=tmp.getPrev())
System.out.println(tmp.getData());
}
}

Node.java

package doubly;

public class Node <E>{
   private E data;
   private Node<E> prev;
   private Node<E> next;
  
   public Node(E d, Node<E> p,Node<E> n)
   {
   data=d;
   prev=p;
   next=n;
   }
   public E getData() { return data; }
   public Node<E> getNext(){ return next; }
   public Node<E> getPrev(){ return prev; }
   public void setNext(Node<E> n) { next=n;}
   public void setPrev(Node<E> p) { prev=p;}

}

Student.java

package doubly;

public class Student {
private int id;
private String name;
public Student(int id, String name) {
   super();
   this.id = id;
   this.name = name;
}
public int getId() {
   return id;
}
public void setId(int id) {
   this.id = id;
}
public String getName() {
   return name;
}
public void setName(String name) {
   this.name = name;
}
@Override
public String toString() {
   return "[id=" + id + ", name=" + name + "]";
}

}

public class Test {
public static void main(String arg[])
{
   DoublyLinkedList<Student> myList=new DoublyLinkedList<>();
   myList.addFirst(new Student(1,"Ahmed"));
   myList.addFirst(new Student(2,"Khaled"));
   myList.addLast(new Student(3,"Ali"));
   myList.removeLast();
   myList.printForward();
}
}

For More Chemistry Notes and Helpful Content Subscribe Our YouTube Chanel - Chemistry Explain  

Free Chegg Answer

ANSWER

#include <stdio.h>
#include <stdlib.h>

struct node {
int data;
struct node *prev;
struct node *next;
};

struct node *head = NULL;
struct node *last = NULL;
struct node *current = NULL;

void printList()
{
struct node *ptr = head;

printf("\n[head] <=>");
while(ptr != NULL) {
printf(" %d <=>",ptr->data);
ptr = ptr->next;
}

printf(" [last]\n");
}

void insert(int data)
{
struct node *link = (struct node*) malloc(sizeof(struct node));

link->data = data;
link->prev = NULL;
link->next = NULL;

if(head==NULL) {
head = link;
return;
}

current = head;

while(current->next!=NULL)
current = current->next;

current->next = link;
last = link;
link->prev = current;
}

void remove_data(int data) {
int pos = 0;
struct node *pre_node;
  
if(head==NULL) {
printf("Linked List not initialized");
return;
}
  
if(head->data == data) {
if(head->next != NULL) {
head->next->prev = NULL;
head = head->next;
return;
} else {
head = NULL;
printf("List is empty now");
return;
}
} else if(head->data != data && head->next == NULL) {
printf("%d not found in the list\n", data);
return;
}

current = head;

while(current->next != NULL && current->data != data) {
pre_node = current;
current = current->next;
}

if(current->data == data) {
pre_node->next = pre_node->next->next;
  
if(pre_node->next != NULL) { // link back
pre_node->next->prev = pre_node;
} else
last = pre_node;

free(current);
} else
printf("%d not found in the list.", data);

}

int main()
{
int n,x;
printf("Enter the number of nodes : ");
scanf("%d",&n);
printf("\nEnter the nodes : ");
for(int i=0;i<n;i++)
{

scanf("%d",&x);
insert(x);
}
  

printf("\nEnter the element to be deleted : ");
scanf("%d",&x);
  
printf("\nList before deletion : ");
printList();

remove_data(x);
  
printf("\nList after deletion : ");
printList();
  
return 0;
}

IMAGE OF OUTPUT

ChemistryExplain “#346 To class doublyLinedList, add method in Computer science, Ba computer science, Berkeley computer science, Computer science

Labels: , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home