🔒 Closed Reverse Method Using Push and Pop (JAVA po language nito)

Status
Not open for further replies.

ken454545

Enthusiast
Patulong nman mga paps kung paano to mareverse using pop and push :( MARAMING SALAMAT PO SA MAKAKATULONG SAKEN.

class Link

{

public long dData; // data item

public Link next; // next link in list

// -------------------------------------------------------------

public Link(long dd) // constructor

{ dData = dd; }

// -------------------------------------------------------------

public void displayLink() // display ourself

{ System.out.print(dData + " "); }

} // end class Link

////////////////////////////////////////////////////////////////

class LinkList

{

private Link first; // ref to first item on list

// -------------------------------------------------------------

public LinkList() // constructor

{ first = null; } // no items on list yet

// -------------------------------------------------------------

public boolean isEmpty() // true if list is empty

{ return (first==null); }

// -------------------------------------------------------------

public void insertFirst(long dd) // insert at start of list

{ // make new link

Link newLink = new Link(dd);

newLink.next = first; // newLink --> old first

first = newLink; // first --> newLink

}

// -------------------------------------------------------------

public long deleteFirst() // delete first item

{ // (assumes list not empty)

Link temp = first; // save reference to link

first = first.next; // delete it: first-->old next

return temp.dData; // return deleted link

}

// -------------------------------------------------------------

public void displayList()

{

Link current = first; // start at beginning of list

while(current != null) // until end of list,

{

current.displayLink(); // print data

current = current.next; // move to next link

}

System.out.println("");

}

// -------------------------------------------------------------

} // end class LinkList

////////////////////////////////////////////////////////////////

class LinkStack

{

private LinkList theList;

//--------------------------------------------------------------

public LinkStack() // constructor

{

theList = new LinkList();

}

//--------------------------------------------------------------

public void push(long j) // put item on top of stack

{

theList.insertFirst(j);

}

//--------------------------------------------------------------

public long pop() // take item from top of stack

{

return theList.deleteFirst();

}

//--------------------------------------------------------------

public boolean isEmpty() // true if stack is empty

{

return ( theList.isEmpty() );

}

//--------------------------------------------------------------

public void displayStack()

{

System.out.print("Stack (top-->bottom): ");

theList.displayList();

}

//--------------------------------------------------------------

} // end class LinkStack

////////////////////////////////////////////////////////////////

class LinkStackApp

{

public static void main(String[] args)

{

LinkStack theStack = new LinkStack(); // make stack



theStack.push(20); // push items

theStack.push(40);



theStack.displayStack(); // display stack



theStack.push(60); // push items

theStack.push(80);



theStack.displayStack(); // display stack



theStack.pop(); // pop items

theStack.pop();



theStack.displayStack(); // display stack

} // end main()

} // end class LinkStackApp

////////////////////////////////////////////////////////////////
 
paps palagay sa insert code java,
ibig mong sabihin, gagawa ng method na reverse() method na gagamitin yung push() and pop() method ng LinkedListStack?
 
Status
Not open for further replies.

About this Thread

  • 3
    Replies
  • 551
    Views
  • 2
    Participants
Last reply from:
Arcturus

Trending Topics

Online now

Members online
1,159
Guests online
1,016
Total visitors
2,175

Forum statistics

Threads
2,274,693
Posts
28,957,671
Members
1,233,403
Latest member
uradox
Back
Top