Question

In: Computer Science

Q1) In the implementation of Singly linked list we had an integer variable called size that...

Q1) In the implementation of Singly linked list we had an integer variable called size that keeps track of how many elements in the list. Also, we have a reference “tail” that points to the last node in the list. You are asked to re-implement the concept of singly linked list without using variable size, and without using reference “tail”.

a) What are the methods of the main operation of singly linked list that need to be changed? Rewrite them according to this change (only the methods that need to be changed).

b) Will that affect the performance of any one of the main operations( size, isEmpty, first, last, addFirst, addLast, removeFirst, removeLast)? Explain.

Please answer based in the following codes:

public class Node <E>{

private E data;

private Node<E> next;

public Node(E d, Node<E> n)

{  data=d;

next=n; }

public E getData() { return data; }

public Node<E> getNext(){ return next; }

public void setNext(Node<E> n) { next=n; } }

--------------------------------------------------------------------

public class SinglyLinkedList <E>{

private Node<E> head=null;

private Node<E> tail=null;

private int size=0;

public SinglyLinkedList() { }

public int size() { return size;}

public boolean isEmpty() {return size==0;}

public E first()

{

if (isEmpty()) return null;

return head.getData();

}

public E last() // last()

{

if (isEmpty()) return null;

return head.getData();

}

public void addFirst(E e) // addFirst()

{

head=new Node<>(e,head);

if(size==0)

tail=head;

size++;

}

public void addLast(E e) //addLast()

{

Node<E> newest=new Node<>(e,null);

if(isEmpty())

head=newest;

else

tail.setNext(newest);

tail=newest;

size++;

}

public E removeFirst() // removeFirst()

{

if(isEmpty()) return null;

E answer=head.getData();

head=head.getNext();

size--;

if (size==0)

tail=null;

return answer;

}

public E removeLast() // removeLast()

{

if(isEmpty()) return null;

E answer=tail.getData();

if (head==tail)

head=tail=null;

else

{ Node<E> tmp=head;

while (tmp.getNext()!=tail)

tmp=tmp.getNext();

tmp.setNext(null);

tail=tmp;

}

size--;

return answer;

}

}

Solutions

Expert Solution

note: plzzz don't give dislike.....plzzz comment if you have any problem i will try to solve your problem.....plzzz give thumbs up i am in need....


Related Solutions

In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Singly Linked List. 5. Insert a new node at the end of a Singly Linked List 6. Insert a new node after the value 5 of Singly Linked List 7. Delete the node with value 6. 8. Search an existing element in...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class in C++ that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value x if...
The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked)....
The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked). For this lab, you will need to write the following two functions in list.cpp, and add function prototypes for them to list.h. The provided main.cpp has calls to each of these functions commented out. As you write the functions, uncomment them from main.cpp. void reverse(node * head, node *& newHead) Recursively make a revserse copy of the source list with head where newhead is...
C++ PROGRAMMING In the linked-list based bag implementation, we demonstrated the functionalities, such as, add, remove,...
C++ PROGRAMMING In the linked-list based bag implementation, we demonstrated the functionalities, such as, add, remove, and list. This assignment is to extend the functionalities of the bag with other operations average, min, and max, You need to extend the Bag class (under Wk 2, BagLinked_List.cpp) with the following methods: -int Bag::min( ), is to find minimum of items of the bag. -int Bag::max( ), is to find maximum of items of the bag -float Bag::ave( ), is to find...
Evaluate and write an algorithm to find the largest item in an unsorted singly linked list...
Evaluate and write an algorithm to find the largest item in an unsorted singly linked list with cells containing integers
Write a java method to swap between two values in a singly linked list
Write a java method to swap between two values in a singly linked list
1. Considering singly linked list, be able to determine if inserting nodes at the end of...
1. Considering singly linked list, be able to determine if inserting nodes at the end of a LinkedList is less time-efficient than inserting them at the front of the list. 2. Be able to differentiate between the data structures Stack, Queue, and Linked List. 3. Determine between the acronyms LIFO and FIFO, and be able to give one real life example where each is applicable
I've provided a Node class that implements a node of a simple singly-linked list (with .value...
I've provided a Node class that implements a node of a simple singly-linked list (with .value and .next fields), and an empty LinkedList class. Your task is to implement LinkedList.sort(l), where given the node l as the head of a singly-linked list, LinkedList.sort(l) sorts the nodes in the list into ascending order according to the values in the .value field of each node. Your implementation should do an in-place update of the list. It is ok to use a simple...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT