SlideShare a Scribd company logo
1 of 9
Download to read offline
Code of main class:
public class LBmain {
public static void main(String[]args)
{
LinkedStack list = new LinkedStack<>();
System.out.println("Let's make a List!");
System.out.println("Push 3 times.");
System.out.println("Check the size.");
System.out.println("Peek the top element.");
System.out.println("Pop three times.");
System.out.println ("The size now should be zero!" + " ");
list.Push(1);
list.Push(2);
list.Push(3);
System.out.println(list.toString());
list.Size();
list.Peek();
list.Pop();
list.Pop();
list.Pop();
list.Size();
}
public class LinkedStack implements Stack {
private int count;
private LinearNode top;
//-----------------------------------------------------------------
// Creates an empty stack using the default capacity.
//-----------------------------------------------------------------
public LinkedStack()
{
count = 0;
top = null;
}
@Override
public boolean IsEmpty()
{
if(top == null)
{
System.out.println("Stack is empty");
}
return top == null;
}
@Override
public void Push(T element)
{
LinearNode current = new LinearNode<>(element);
current.setNext(top);
top = current;
count++;}
@Override
public T Pop()
{
T result;
System.out.println("Lets pop the top element!");
if (count == 0)
{
System.out.println("Pop operation failed. "+ "The stack is empty.");
}
result = top.getElement();
top = top.getNext();
count--;
System.out.println("The element that we have poped is :" + result);
return result;
}
Override
public String toString()
{
String result = " ";
LinearNode current = top;
while (current != null)
{
result += current.getElement() + " ";
current = current.getNext();
}
return result + "";
}
@Override
public T Peek() {
System.out.println("Lets peek the top element!");
if(count == 0)
{
System.out.println("Peek failed stack is empty");
}
System.out.println("The element that we have peeked is: " + top.getElement());
return top.getElement();
}
@Override
public int Size() {
System.out.println("The size of the list now is: " + count);
return count;
}
}
main.Peek();
main.Pop();
main.Pop();
main.Size();
main.toString();
}
}
@Override
public void Push(T element)
{
LinearNode current = new LinearNode<>(element);
current.setNext(top);
top = current;
count++;
}
}
public class LinearNode {
private LinearNode next; //se guarda la referencia del Nodo
private T element; //Lista vacia
public LinearNode()
{
next = null;
element = null;
}
//-----------------------------------------------------------------
// Creates a node storing the specified element.
//-----------------------------------------------------------------
public LinearNode (T elem)
{
next = null;
element = elem;
}
//-----------------------------------------------------------------
// Returns the node that follows this one.
//-----------------------------------------------------------------
public LinearNode getNext()
{
return next;
}
//-----------------------------------------------------------------
// Sets the node that follows this one.
//-----------------------------------------------------------------
public void setNext (LinearNode node)
{
next = node;
}
//-----------------------------------------------------------------
// Returns the element stored in this node.
//-----------------------------------------------------------------
public T getElement()//asigna valor
{
return element;
}
public void setElement(T elem)
{
element = elem;
}
Solution
Code of main class:
public class LBmain {
public static void main(String[]args)
{
LinkedStack list = new LinkedStack<>();
System.out.println("Let's make a List!");
System.out.println("Push 3 times.");
System.out.println("Check the size.");
System.out.println("Peek the top element.");
System.out.println("Pop three times.");
System.out.println ("The size now should be zero!" + " ");
list.Push(1);
list.Push(2);
list.Push(3);
System.out.println(list.toString());
list.Size();
list.Peek();
list.Pop();
list.Pop();
list.Pop();
list.Size();
}
public class LinkedStack implements Stack {
private int count;
private LinearNode top;
//-----------------------------------------------------------------
// Creates an empty stack using the default capacity.
//-----------------------------------------------------------------
public LinkedStack()
{
count = 0;
top = null;
}
@Override
public boolean IsEmpty()
{
if(top == null)
{
System.out.println("Stack is empty");
}
return top == null;
}
@Override
public void Push(T element)
{
LinearNode current = new LinearNode<>(element);
current.setNext(top);
top = current;
count++;}
@Override
public T Pop()
{
T result;
System.out.println("Lets pop the top element!");
if (count == 0)
{
System.out.println("Pop operation failed. "+ "The stack is empty.");
}
result = top.getElement();
top = top.getNext();
count--;
System.out.println("The element that we have poped is :" + result);
return result;
}
Override
public String toString()
{
String result = " ";
LinearNode current = top;
while (current != null)
{
result += current.getElement() + " ";
current = current.getNext();
}
return result + "";
}
@Override
public T Peek() {
System.out.println("Lets peek the top element!");
if(count == 0)
{
System.out.println("Peek failed stack is empty");
}
System.out.println("The element that we have peeked is: " + top.getElement());
return top.getElement();
}
@Override
public int Size() {
System.out.println("The size of the list now is: " + count);
return count;
}
}
main.Peek();
main.Pop();
main.Pop();
main.Size();
main.toString();
}
}
@Override
public void Push(T element)
{
LinearNode current = new LinearNode<>(element);
current.setNext(top);
top = current;
count++;
}
}
public class LinearNode {
private LinearNode next; //se guarda la referencia del Nodo
private T element; //Lista vacia
public LinearNode()
{
next = null;
element = null;
}
//-----------------------------------------------------------------
// Creates a node storing the specified element.
//-----------------------------------------------------------------
public LinearNode (T elem)
{
next = null;
element = elem;
}
//-----------------------------------------------------------------
// Returns the node that follows this one.
//-----------------------------------------------------------------
public LinearNode getNext()
{
return next;
}
//-----------------------------------------------------------------
// Sets the node that follows this one.
//-----------------------------------------------------------------
public void setNext (LinearNode node)
{
next = node;
}
//-----------------------------------------------------------------
// Returns the element stored in this node.
//-----------------------------------------------------------------
public T getElement()//asigna valor
{
return element;
}
public void setElement(T elem)
{
element = elem;
}

More Related Content

Similar to Code of main classpublic class LBmain {    public static void m.pdf

Java simple programs
Java simple programsJava simple programs
Java simple programsVEERA RAGAVAN
 
JAVA 8 : Migration et enjeux stratégiques en entreprise
JAVA 8 : Migration et enjeux stratégiques en entrepriseJAVA 8 : Migration et enjeux stratégiques en entreprise
JAVA 8 : Migration et enjeux stratégiques en entrepriseSOAT
 
Java Generics for Dummies
Java Generics for DummiesJava Generics for Dummies
Java Generics for Dummiesknutmork
 
Java practice programs for beginners
Java practice programs for beginnersJava practice programs for beginners
Java practice programs for beginnersishan0019
 
Sharable_Java_Python.pdf
Sharable_Java_Python.pdfSharable_Java_Python.pdf
Sharable_Java_Python.pdfICADCMLTPC
 
JAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfJAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfRohitkumarYadav80
 
import java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfimport java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfoptokunal1
 

Similar to Code of main classpublic class LBmain {    public static void m.pdf (13)

Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
 
Oop lecture9 13
Oop lecture9 13Oop lecture9 13
Oop lecture9 13
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programs
 
delegates
delegatesdelegates
delegates
 
Java simple programs
Java simple programsJava simple programs
Java simple programs
 
JAVA 8 : Migration et enjeux stratégiques en entreprise
JAVA 8 : Migration et enjeux stratégiques en entrepriseJAVA 8 : Migration et enjeux stratégiques en entreprise
JAVA 8 : Migration et enjeux stratégiques en entreprise
 
Java Generics for Dummies
Java Generics for DummiesJava Generics for Dummies
Java Generics for Dummies
 
Java practice programs for beginners
Java practice programs for beginnersJava practice programs for beginners
Java practice programs for beginners
 
Sharable_Java_Python.pdf
Sharable_Java_Python.pdfSharable_Java_Python.pdf
Sharable_Java_Python.pdf
 
JAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfJAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdf
 
Collection
CollectionCollection
Collection
 
Java Unit 1 Project
Java Unit 1 ProjectJava Unit 1 Project
Java Unit 1 Project
 
import java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfimport java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdf
 

More from akkhan101

1.Cexplanation; Cloud consumers that use cloud-based IT resources .pdf
1.Cexplanation; Cloud consumers that use cloud-based IT resources .pdf1.Cexplanation; Cloud consumers that use cloud-based IT resources .pdf
1.Cexplanation; Cloud consumers that use cloud-based IT resources .pdfakkhan101
 
1) WBC count is high and this level indicates that the person has le.pdf
1) WBC count is high and this level indicates that the person has le.pdf1) WBC count is high and this level indicates that the person has le.pdf
1) WBC count is high and this level indicates that the person has le.pdfakkhan101
 
What are the four steps of a process involving a heterogeneous catal.pdf
What are the four steps of a process involving a heterogeneous catal.pdfWhat are the four steps of a process involving a heterogeneous catal.pdf
What are the four steps of a process involving a heterogeneous catal.pdfakkhan101
 
The given function is-Strictly increasing from 2 to infinityHen.pdf
The given function is-Strictly increasing from 2 to infinityHen.pdfThe given function is-Strictly increasing from 2 to infinityHen.pdf
The given function is-Strictly increasing from 2 to infinityHen.pdfakkhan101
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdfakkhan101
 
In the span of several decades, the Kingdom Protista has been disass.pdf
In the span of several decades, the Kingdom Protista has been disass.pdfIn the span of several decades, the Kingdom Protista has been disass.pdf
In the span of several decades, the Kingdom Protista has been disass.pdfakkhan101
 
Information privacy It refers to the collection of data and disse.pdf
Information privacy It refers to the collection of data and disse.pdfInformation privacy It refers to the collection of data and disse.pdf
Information privacy It refers to the collection of data and disse.pdfakkhan101
 
H2SO4 --- 2H+ + SO42-1 mole of H2SO4 produces 2 moles of H+ ions.pdf
H2SO4 --- 2H+ + SO42-1 mole of H2SO4 produces 2 moles of H+ ions.pdfH2SO4 --- 2H+ + SO42-1 mole of H2SO4 produces 2 moles of H+ ions.pdf
H2SO4 --- 2H+ + SO42-1 mole of H2SO4 produces 2 moles of H+ ions.pdfakkhan101
 
Gene editing of somatic cellsThere are grave concerns regarding th.pdf
Gene editing of somatic cellsThere are grave concerns regarding th.pdfGene editing of somatic cellsThere are grave concerns regarding th.pdf
Gene editing of somatic cellsThere are grave concerns regarding th.pdfakkhan101
 
Four parts of compare and contrastSolutionFour parts of compar.pdf
Four parts of compare and contrastSolutionFour parts of compar.pdfFour parts of compare and contrastSolutionFour parts of compar.pdf
Four parts of compare and contrastSolutionFour parts of compar.pdfakkhan101
 
Each Restriction enzymes has a unique restriction site and therefore.pdf
Each Restriction enzymes has a unique restriction site and therefore.pdfEach Restriction enzymes has a unique restriction site and therefore.pdf
Each Restriction enzymes has a unique restriction site and therefore.pdfakkhan101
 
correctSolutioncorrect.pdf
correctSolutioncorrect.pdfcorrectSolutioncorrect.pdf
correctSolutioncorrect.pdfakkhan101
 
BibliographyHall, J. E. (2015). Guyton and Hall textbook of medic.pdf
BibliographyHall, J. E. (2015). Guyton and Hall textbook of medic.pdfBibliographyHall, J. E. (2015). Guyton and Hall textbook of medic.pdf
BibliographyHall, J. E. (2015). Guyton and Hall textbook of medic.pdfakkhan101
 
synthesis .pdf
                     synthesis                                      .pdf                     synthesis                                      .pdf
synthesis .pdfakkhan101
 
LiOH is a strong base so we assume it dissociates.pdf
                     LiOH is a strong base so we assume it dissociates.pdf                     LiOH is a strong base so we assume it dissociates.pdf
LiOH is a strong base so we assume it dissociates.pdfakkhan101
 
With Sp3d hybridization, a seesaw or linear shape.pdf
                     With Sp3d hybridization, a seesaw or linear shape.pdf                     With Sp3d hybridization, a seesaw or linear shape.pdf
With Sp3d hybridization, a seesaw or linear shape.pdfakkhan101
 
Two-photon transition probability .pdf
                     Two-photon transition probability                .pdf                     Two-photon transition probability                .pdf
Two-photon transition probability .pdfakkhan101
 
this is because Nitrogen has a lone pair and and .pdf
                     this is because Nitrogen has a lone pair and and .pdf                     this is because Nitrogen has a lone pair and and .pdf
this is because Nitrogen has a lone pair and and .pdfakkhan101
 
There is no easy way to remember the ionization l.pdf
                     There is no easy way to remember the ionization l.pdf                     There is no easy way to remember the ionization l.pdf
There is no easy way to remember the ionization l.pdfakkhan101
 
My opinion is to carry out in the complete absenc.pdf
                     My opinion is to carry out in the complete absenc.pdf                     My opinion is to carry out in the complete absenc.pdf
My opinion is to carry out in the complete absenc.pdfakkhan101
 

More from akkhan101 (20)

1.Cexplanation; Cloud consumers that use cloud-based IT resources .pdf
1.Cexplanation; Cloud consumers that use cloud-based IT resources .pdf1.Cexplanation; Cloud consumers that use cloud-based IT resources .pdf
1.Cexplanation; Cloud consumers that use cloud-based IT resources .pdf
 
1) WBC count is high and this level indicates that the person has le.pdf
1) WBC count is high and this level indicates that the person has le.pdf1) WBC count is high and this level indicates that the person has le.pdf
1) WBC count is high and this level indicates that the person has le.pdf
 
What are the four steps of a process involving a heterogeneous catal.pdf
What are the four steps of a process involving a heterogeneous catal.pdfWhat are the four steps of a process involving a heterogeneous catal.pdf
What are the four steps of a process involving a heterogeneous catal.pdf
 
The given function is-Strictly increasing from 2 to infinityHen.pdf
The given function is-Strictly increasing from 2 to infinityHen.pdfThe given function is-Strictly increasing from 2 to infinityHen.pdf
The given function is-Strictly increasing from 2 to infinityHen.pdf
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdf
 
In the span of several decades, the Kingdom Protista has been disass.pdf
In the span of several decades, the Kingdom Protista has been disass.pdfIn the span of several decades, the Kingdom Protista has been disass.pdf
In the span of several decades, the Kingdom Protista has been disass.pdf
 
Information privacy It refers to the collection of data and disse.pdf
Information privacy It refers to the collection of data and disse.pdfInformation privacy It refers to the collection of data and disse.pdf
Information privacy It refers to the collection of data and disse.pdf
 
H2SO4 --- 2H+ + SO42-1 mole of H2SO4 produces 2 moles of H+ ions.pdf
H2SO4 --- 2H+ + SO42-1 mole of H2SO4 produces 2 moles of H+ ions.pdfH2SO4 --- 2H+ + SO42-1 mole of H2SO4 produces 2 moles of H+ ions.pdf
H2SO4 --- 2H+ + SO42-1 mole of H2SO4 produces 2 moles of H+ ions.pdf
 
Gene editing of somatic cellsThere are grave concerns regarding th.pdf
Gene editing of somatic cellsThere are grave concerns regarding th.pdfGene editing of somatic cellsThere are grave concerns regarding th.pdf
Gene editing of somatic cellsThere are grave concerns regarding th.pdf
 
Four parts of compare and contrastSolutionFour parts of compar.pdf
Four parts of compare and contrastSolutionFour parts of compar.pdfFour parts of compare and contrastSolutionFour parts of compar.pdf
Four parts of compare and contrastSolutionFour parts of compar.pdf
 
Each Restriction enzymes has a unique restriction site and therefore.pdf
Each Restriction enzymes has a unique restriction site and therefore.pdfEach Restriction enzymes has a unique restriction site and therefore.pdf
Each Restriction enzymes has a unique restriction site and therefore.pdf
 
correctSolutioncorrect.pdf
correctSolutioncorrect.pdfcorrectSolutioncorrect.pdf
correctSolutioncorrect.pdf
 
BibliographyHall, J. E. (2015). Guyton and Hall textbook of medic.pdf
BibliographyHall, J. E. (2015). Guyton and Hall textbook of medic.pdfBibliographyHall, J. E. (2015). Guyton and Hall textbook of medic.pdf
BibliographyHall, J. E. (2015). Guyton and Hall textbook of medic.pdf
 
synthesis .pdf
                     synthesis                                      .pdf                     synthesis                                      .pdf
synthesis .pdf
 
LiOH is a strong base so we assume it dissociates.pdf
                     LiOH is a strong base so we assume it dissociates.pdf                     LiOH is a strong base so we assume it dissociates.pdf
LiOH is a strong base so we assume it dissociates.pdf
 
With Sp3d hybridization, a seesaw or linear shape.pdf
                     With Sp3d hybridization, a seesaw or linear shape.pdf                     With Sp3d hybridization, a seesaw or linear shape.pdf
With Sp3d hybridization, a seesaw or linear shape.pdf
 
Two-photon transition probability .pdf
                     Two-photon transition probability                .pdf                     Two-photon transition probability                .pdf
Two-photon transition probability .pdf
 
this is because Nitrogen has a lone pair and and .pdf
                     this is because Nitrogen has a lone pair and and .pdf                     this is because Nitrogen has a lone pair and and .pdf
this is because Nitrogen has a lone pair and and .pdf
 
There is no easy way to remember the ionization l.pdf
                     There is no easy way to remember the ionization l.pdf                     There is no easy way to remember the ionization l.pdf
There is no easy way to remember the ionization l.pdf
 
My opinion is to carry out in the complete absenc.pdf
                     My opinion is to carry out in the complete absenc.pdf                     My opinion is to carry out in the complete absenc.pdf
My opinion is to carry out in the complete absenc.pdf
 

Recently uploaded

Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 

Recently uploaded (20)

Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 

Code of main classpublic class LBmain {    public static void m.pdf

  • 1. Code of main class: public class LBmain { public static void main(String[]args) { LinkedStack list = new LinkedStack<>(); System.out.println("Let's make a List!"); System.out.println("Push 3 times."); System.out.println("Check the size."); System.out.println("Peek the top element."); System.out.println("Pop three times."); System.out.println ("The size now should be zero!" + " "); list.Push(1); list.Push(2); list.Push(3); System.out.println(list.toString()); list.Size(); list.Peek(); list.Pop(); list.Pop(); list.Pop(); list.Size(); } public class LinkedStack implements Stack { private int count; private LinearNode top; //----------------------------------------------------------------- // Creates an empty stack using the default capacity. //----------------------------------------------------------------- public LinkedStack() { count = 0; top = null; } @Override public boolean IsEmpty()
  • 2. { if(top == null) { System.out.println("Stack is empty"); } return top == null; } @Override public void Push(T element) { LinearNode current = new LinearNode<>(element); current.setNext(top); top = current; count++;} @Override public T Pop() { T result; System.out.println("Lets pop the top element!"); if (count == 0) { System.out.println("Pop operation failed. "+ "The stack is empty."); } result = top.getElement(); top = top.getNext(); count--; System.out.println("The element that we have poped is :" + result); return result; } Override public String toString() { String result = " "; LinearNode current = top; while (current != null) {
  • 3. result += current.getElement() + " "; current = current.getNext(); } return result + ""; } @Override public T Peek() { System.out.println("Lets peek the top element!"); if(count == 0) { System.out.println("Peek failed stack is empty"); } System.out.println("The element that we have peeked is: " + top.getElement()); return top.getElement(); } @Override public int Size() { System.out.println("The size of the list now is: " + count); return count; } } main.Peek(); main.Pop(); main.Pop(); main.Size(); main.toString(); } } @Override public void Push(T element) { LinearNode current = new LinearNode<>(element); current.setNext(top); top = current; count++; }
  • 4. } public class LinearNode { private LinearNode next; //se guarda la referencia del Nodo private T element; //Lista vacia public LinearNode() { next = null; element = null; } //----------------------------------------------------------------- // Creates a node storing the specified element. //----------------------------------------------------------------- public LinearNode (T elem) { next = null; element = elem; } //----------------------------------------------------------------- // Returns the node that follows this one. //----------------------------------------------------------------- public LinearNode getNext() { return next; } //----------------------------------------------------------------- // Sets the node that follows this one. //----------------------------------------------------------------- public void setNext (LinearNode node) { next = node; } //----------------------------------------------------------------- // Returns the element stored in this node. //----------------------------------------------------------------- public T getElement()//asigna valor {
  • 5. return element; } public void setElement(T elem) { element = elem; } Solution Code of main class: public class LBmain { public static void main(String[]args) { LinkedStack list = new LinkedStack<>(); System.out.println("Let's make a List!"); System.out.println("Push 3 times."); System.out.println("Check the size."); System.out.println("Peek the top element."); System.out.println("Pop three times."); System.out.println ("The size now should be zero!" + " "); list.Push(1); list.Push(2); list.Push(3); System.out.println(list.toString()); list.Size(); list.Peek(); list.Pop(); list.Pop(); list.Pop(); list.Size(); } public class LinkedStack implements Stack { private int count; private LinearNode top; //----------------------------------------------------------------- // Creates an empty stack using the default capacity.
  • 6. //----------------------------------------------------------------- public LinkedStack() { count = 0; top = null; } @Override public boolean IsEmpty() { if(top == null) { System.out.println("Stack is empty"); } return top == null; } @Override public void Push(T element) { LinearNode current = new LinearNode<>(element); current.setNext(top); top = current; count++;} @Override public T Pop() { T result; System.out.println("Lets pop the top element!"); if (count == 0) { System.out.println("Pop operation failed. "+ "The stack is empty."); } result = top.getElement(); top = top.getNext(); count--; System.out.println("The element that we have poped is :" + result); return result;
  • 7. } Override public String toString() { String result = " "; LinearNode current = top; while (current != null) { result += current.getElement() + " "; current = current.getNext(); } return result + ""; } @Override public T Peek() { System.out.println("Lets peek the top element!"); if(count == 0) { System.out.println("Peek failed stack is empty"); } System.out.println("The element that we have peeked is: " + top.getElement()); return top.getElement(); } @Override public int Size() { System.out.println("The size of the list now is: " + count); return count; } } main.Peek(); main.Pop(); main.Pop(); main.Size(); main.toString(); } }
  • 8. @Override public void Push(T element) { LinearNode current = new LinearNode<>(element); current.setNext(top); top = current; count++; } } public class LinearNode { private LinearNode next; //se guarda la referencia del Nodo private T element; //Lista vacia public LinearNode() { next = null; element = null; } //----------------------------------------------------------------- // Creates a node storing the specified element. //----------------------------------------------------------------- public LinearNode (T elem) { next = null; element = elem; } //----------------------------------------------------------------- // Returns the node that follows this one. //----------------------------------------------------------------- public LinearNode getNext() { return next; } //----------------------------------------------------------------- // Sets the node that follows this one. //----------------------------------------------------------------- public void setNext (LinearNode node)
  • 9. { next = node; } //----------------------------------------------------------------- // Returns the element stored in this node. //----------------------------------------------------------------- public T getElement()//asigna valor { return element; } public void setElement(T elem) { element = elem; }