I am learning Stack today.
I am trying to implement push(Element) method, but for some reasons it is not working.
I have added 10 items to my stack, then using toString method I print. The only item prints is my most Top Element.
Your help would be greatly appreciated!.
private Node head = null;
private Node tail = null;
private static int size;
public void push(Customer custPair)
{
if (head == null)
{
head = new Node(custPair, null);
tail = head;
size++;
} else
{
tail.next = head;
head = new Node(custPair, null);
size++;
}
}
#Override
public String toString()
{
StringBuilder sBuilder = new StringBuilder();
// Walk down the list and append all values
Node curNode = head;
while (curNode != null)
{
sBuilder.append(curNode.value.toString()).append("\n");
curNode = curNode.next;
}
return sBuilder.toString();
}
Your logic is wrong when the stack already has a head. I think the new head must point to old head (via .next).
I'm bit confused, are you implementing a LIFO or FIFO stack?
For a LIFO
else {
Node node = new Node(custPair, null);
node.next = head;
head = node;
size++;
}
For a LIFO Stack, you really don't need a tail.
Related
My teacher guides us in this activity on how to delete the tail of the double link list. He created an step by step process or algorithm for us to follow. I followed it, but it doesn't work. Or maybe I am following it wrong. Here is the algorithm
Check if the list is empty
If not Empty
Check if there is only one node in the list
If only one node, set the head and tail reference to null.
if more than one node
create a temptail to point to next tail (tail.prev)
set the prev of the tail and next of temptail to null
assign the temptail value to the tail
Here is my code
public void delTail(){
DoubleNode temp;
if(isEmpty()){
return;
}
else if(!isEmpty()){
if(head == tail){
head = tail = null;
}
else{
temp = tail.next;
tail.prev = null;
temp.next = null;
temp = tail;
}
}
}
This is the error that i saw error in my terminal
I think that I am following it right or maybe not? Thank you so much for your help :)
This is my constructor*
public class DoubleNode{
public DoubleNode prev;
public int data;
public DoubleNode next;
public DoubleNode(int d){
this(null, d, null);
}
public DoubleNode(DoubleNode p, int d, DoubleNode n){
prev = p;
data = d;
next = n;
}
}
this is mt entire operator code*
public class operator{
DoubleNode head;
DoubleNode tail;
DoubleNode laman;
String output = "";
public operator(){
head = tail = null;
}
public boolean isEmpty(){
return head == null;
}
public void addHead(int i){
if(isEmpty()){
head = tail =new DoubleNode(i);
}
else{
head = new DoubleNode(null, i, head);
head.prev = head;
}
}
public void addTail(int i){
DoubleNode last = new DoubleNode(i);
if(isEmpty()){
head = tail = new DoubleNode(i);
}
else{
tail.next = last;
tail = last;
}
}
public void delHead(){
DoubleNode temp = head.next;
if(head==tail){ //this if condition is testing if the head and tail is one only,
head = tail =null; //if there is only one this will set the tail and head to null
}
else{
head = head.next;
head = temp;
}
}
public void delTail(){
DoubleNode temp;
if(isEmpty()) {
return;
}
else {
if(head != tail) {
tail = tail.prev;
temp = tail;
}
else {
head = tail = null;
}
}
}
public void display(){
DoubleNode tmp = head;
output = "<html>";
for(tmp = head; tmp != null; tmp = tmp.next){
output = output + "<br>" + tmp.data + "<b>" + "<br>";
}
output = output + "</html>";
}
}
This is my entire code so far, i have a main class with a jframe but i think its fine because i also use it for single link list. But I do have a problem here at double link list regarding on deleting the last node
Your problem is that you're just assigning to temp but don't actually use it. Additionally you're not setting the link back to the previous element correctly.
Assuming tail.next points to head again you might do the following:
tail.prev.next = tail.next; //you might need to check for `tail.prev` being null
tail.next.prev = tail.prev; //you might need to check for `tail.next` being null
//delete tail
To illustrate:
A -next-> Tail -next-> Head
^---prev--+ ^---prev--+
Step 1:
+----next--------------V
A Tail -next-> Head
^---prev--+ ^---prev--+
Step 2:
+----next--------------V
A Tail -next-> Head
^---prev--+ |
^--------------prev----+
Actually if tail.next == head removing the tail is no different than removing any other node.
You have two errors in your else block:
You never change the tail reference. The last statement should really be an assignment to tail.
You seem to assume that the tail has a non-null next reference, but that is a contradiction. The tail is supposed to be the last node, so its next reference will always be null (unless you are supposed to create a circular list). By consequence, temp will be null, and the statement temp.next = tail will trigger the Null Pointer Exception.
The more interesting property of tail is its prev property, which refers to the node that will become the tail after the current tail node has been removed. This tail.prev is explicitly mentioned in your assignment.
So:
else {
temp = tail.prev; // <--- should point to the new tail
tail.prev = null;
temp.next = tail;
tail = temp; // <--- reverse the assignment
}
Several other issues...
In addHead you do not set the prev property correctly. You make it a self-reference. Realise that head is already referencing the new node. Change:
head.prev = head;
to:
head.next.prev = head;
In addTail the assignment to prev is missing. Change:
tail.next = last;
tail = last;
to:
tail.next = last;
last.prev = tail; // needed!
tail = last;
In delHead you must make sure the new head's prev is null. So change:
head = temp;
to:
head = head.next;
head.prev = null;
NB: you don't need DoubleNode temp = head.next; in that method.
How can I add it and delete Node First and delete Node Last in a double-link list
Just want to add delete Node First and delete Node Last. I did not know how to add it in your programming, but I want to help it solve it
i have 3 class
class node .
class doublyLinkedListMain .
class doublyLinkedList .
I want to add special programming in class doublyLinkedList this one delete Node First and delete Node Last
class doublyLinkedList
{
Node head;
public void push(int newdata)
{
Node NewNode = new Node(newdata);
NewNode.next = head;
NewNode.prev = null;
if (head != null) head.prev = NewNode;
head = NewNode;
}
public void insertAfter(Node PrevNode, int newdata)
{
if (PrevNode == null)
{
System.out.println("The given previous node cannot be null");
return;
}
Node NewNode = new Node(newdata);
NewNode.next = PrevNode.next;
PrevNode.next = NewNode;
NewNode.prev = PrevNode;
if (NewNode.next != null)
NewNode.next.prev = NewNode;
}
public void append(int newdata)
{
Node NewNode = new Node(newdata);
Node last = head;
NewNode.next = null;
if (head == null)
{
NewNode.prev = null;
head = NewNode;
return;
}
while (last.next != null) last = last.next;
last.next = NewNode;
NewNode.prev = last;
return;
}
void insertBefore(Node NextNode, int newdata)
{
if (NextNode == null)
{
System.out.println("the given next node cannot be NULL");
return;
}
Node NewNode = new Node(newdata);
NewNode.data = newdata;
NewNode.prev = NextNode.prev;
NextNode.prev = NewNode;
NewNode.next = NextNode;
if (NewNode.prev != null)
NewNode.prev.next = NewNode;
else head = NewNode;
}
void deleteNode( Node del)
{
if (head == null || del == null) return;
if (head == del) head = head.next;
if (del.next != null) del.next.prev = del.prev;
if (del.prev != null) del.prev.next = del.next;
return;
}
void printList()
{
Node n = head;
while (n != null)
{
System.out.print(n.data+" ");
n = n.next;
}
System.out.print(" \n ") ;
}
}
sorry, but you have different mistakes in your code. Firstly, for a list you need two or three pointers as attribute in your List class (Node head /* begin */, tail /* end */, current /* yes, the current element on which you have access */ - I have learned it with a current pointer but depending on your implementation you can do it without it too). Because of that, you must change some of your code. For example, you need a hasAccess() : boolean method, if you use a current pointer, which return current != null and your methods must consider and use tail. Furthermore, you should add a method isEmpty():
public boolean isEmpty() {
return head == null;
}
This method checks if the list is empty. In the case, that the list is empty (the method returns true), you cannot do some things, but you had seen that yet. But, because of the new pointer tail, you have its much easier to delete the last Node (see below).
Please, change all your attributes to private and code a getter and a setter for each of them. For example in the class Node for the attribute next:
private Node next;
// other code...
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
I have solved the problem with "delete Node First and delete Node Last" in the following way:
public class DoublyLinkedList {
private Node head; // begin
private Node tail; // end
public void deleteFirstElement() {
if (!isEmpty()) {
head = head.getNext();
}
}
public void deleteLastElement() {
if (!isEmpty()) {
tail = tail.getPrev();
}
}
// other code
}
Of course, you have to proof your other code, if it works. I hope that I could help you.
Here is a implementation of LinkedList algorithm. Algorithm inserts node at beginner, after a given node, or at the end of list.
package LinkedList;
class LinkedList {
Node Head;
class Node {
int data;
Node Next;
public Node(int d) {
data = d;
Next = null;
}
}
public void insert(int value) {
if (Head ==null) {
Head = new Node(value);
return;
}
Node new_node = new Node(value);
new_node.Next = Head;
Head = new_node;
}
public void display() {
Node a = Head;
while (a != null) {
System.out.println("value:" + a.data);
a = a.Next;
}
}
public void insertMiddle(int valueToInsert, Node prev_node) {
if (Head == null) {
System.out.println("Cant put value after last node");
}
Node new_node = new Node(valueToInsert);
new_node.Next = prev_node.Next;
prev_node.Next = new_node;
}
public void last(int value){
Node new_node = new Node(value);
if(Head == null){
Head = new Node(value);
return;
}
new_node.Next = null;
Node last = Head;
while(last != null){
last = last.Next ;
}
last = new_node;
return;
}
}
public class LinkedList_Insertion {
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedList list = new LinkedList();
list.insert(8);
list.insert(20);
list.insert(0);
list.insertMiddle(999, list.Head.Next);
list.display();
System.out.println("--------------");
list.last(10000);
list.display();
}
}
In the above code, While using method insert:
public void insert(int value) {
if(Head ==null){
Head = new Node(value);
return;
}
Node new_node = new Node(value);
new_node.Next = Head;
Head = new_node;
}
why don't we use Head.next = new_node;
Similarly, for method :
public void last(int value){
Node new_node = new Node(value);
if(Head == null){
Head = new Node(value);
return;
}
new_node.Next = null;
Node last = Head;
while(last != null){
last = last.Next ;
}
last = new_node;
return;
}
why don't we use last.next = new_node;?
I often end up doing same mistake again and again. If someone can clear this concept, I will be grateful.
Looking forward to your response!
Head case:
You can have Head->node1->node2->node3->...->lastNode
If you do Head.next = newNode, then node1->node2->node3->...->lastNode will be lost.
If you had a doubly-linked list, you could do Head.prev = newNode; Head = Head.prev (prev means previous).
Last case:
This code:
public void last(int value){
Node new_node = new Node(value);
if(Head == null){
Head = new Node(value);
return;
}
new_node.Next = null;
Node last = Head;
while(last != null){
last = last.Next ;
}
last = new_node;
return;
}
looks weird, the condition should actually be while (last.next != null), but even then you are not inserting, you first get a reference to the element that is last in your list, then you make that reference point to another object, it should actually be last.next = newNode, you are right.
Implementing linked lists is a good way to understand how java references work, keep practicing and also try to implement a doubly-linked list.
Your Second Code seems to be wrong, You are right it has to be last.next = newNode
now answering your 1st question consider 3 nos in linked list 100(head),200,300.
here head node with value 100 points to next node with value 200 which must in turn point to the node with value 300. Now let us suppose you want to insert 50 before 100, so when you do
Head.next = new_node;
The node with value 100 is made to point to next node with value 50, so now we have 100,50 in the linked list with the head as 100 still which is wrong
so we do
new_node.Next = Head;
Head = new_node;
In this case linked list becomes 50,100,200,300.
Thus we do it this way.
the method 'insertAscending' only gives me the first number even after i enter new ones. can anyone help with what i'm doing wrong? Thanks.
public class LinkedList13 {
// Private inner class Node
private class Node{
int data;
Node link;
public Node(){
data = Integer.MIN_VALUE;
link = null;
}
public Node(int x, Node p){
data = x;
link = p;
}
}
// End of Node class
public Node head;
public LinkedList13(){
head = null;
}
public void insertAscending(int data){
Node node = new Node();
node.data = data;
if (head == null)
head = node;
Node p = head;
while (p.link != null)
{
if (p.link.data > data)
{ node.link = p.link;
p.link = node;
break;
}
p= p.link;
}
}
}
Hint: is (p.link != null) ever true?
First of all, you should return after setting the head of the list (when the first element is added).
Second of all, you should handle the case where the newly inserted node is the smallest in the list (and therefore should come first). Your loop never compares the added node to the head of the list.
Finally, if the added element wasn't inserted in the while loop, it should be inserted after the while loop.
public void insertAscending(int data)
{
Node node = new Node();
node.data = data;
if (head == null) {
head = node;
return;
} else if (node.data < head.data) {
node.link = head;
head = node;
return;
}
Node p = head;
boolean added=false;
while (p.link != null)
{
if (p.link.data > data)
{
node.link = p.link;
p.link = node;
added = true;
break;
}
p = p.link;
}
if (!added)
p.link = node;
}
Check out your if condition if(p.link.data > data) the only way a node gets into the list is when that is true. This means, that, if the value of data being inserted it greater than (or equal to) everything that's been inserted so far, it will be discarded.
An easy way to fix this is change break to return and add p.link=node at the end (after the loop).
I'm trying to write a method for adding at the end of a singly linked list in a constant time. I don't know how to assign a pointer to the last node in the list in constant time. This method runs in 0(n):
public void insertEnd(Object obj) {
if (head == null) {
head = new SListNode(obj);
} else {
SListNode node = head;
while (node.next != null) {
node = node.next;
}
node.next = new SListNode(obj);
}
size++;
}
This is a beginning of my new method:
public void addLast(SListNode obj){
//if the list is empty, the new element is head and tail
if(tail == null){
obj.next = null;
head = tail = obj;
}else{ -----> here I'm confused
}
}
This is my SList class:
public class SList {
private SListNode head;
private SListNode tail;
private int size;
public SList() {
size = 0;
head = null;
tail = null;
}
I think this should cover it: (should go in the else)
tail.next = obj; // have (the current tail).next point to the new node
tail = obj; // assign 'tail' to point to the new node
obj.next = null; // may not be required
You have to implement a Double Ended Queue that allow to insert at head or at tail of your List
If your List is empty, both head and tail are null, if it contains one element head == tail
public void addLast(SListNode obj){
//if the list is empty, the new element is head and tail
if(tail == null){
obj.next = null;
head = tail = obj;
}else{ -----> here I'm confused
tail.next = obj;
tail = obj;
}