Our ArrayStack class implements a bounded stack The LLNode c
Solution
32. The capacity is set at creation time
pushes have a preconditioon that the stack isn\'t already full.
Pushing on a full stack is a state exception, not an argument exception.
The field for the current size doubles as the index of the next item to be pushed, and
When poping we take care to mullify the newly unused slot in the array to prevent a possible memory leak.
33. A Self referential class contains a member that points to a clsss object of the same calss type. For example the difintion
Class Node
{
public:
explict Node (int);//constructor
void setData(int);//set data member
int fetData90 const;//get data member
void setNextPrt (Node*); //set pointer to next node
Node *getNextPtr()cons; //get pointer to next Node
private;
int data; //data stored in this Node
Node *nextPtr; // pointer to another object of same type
};//end class Node
34 public class LinkedStack implements UnboundedStackInterface
protected LLobjectNOde top;
public LinkedStack()
{
top = null;
}
public void push (object item)
{
LLObjectNode newNode = new LLObjectNode(item);
newNode.setLink(top);
top = newNode;
}
public object pop()
{
if ( ! isEmpty())
{
Object x= top.getInfo();
top = top. getLink();
return x;
} else
throw new StackUnderflowException(\"pop attempted on an empty stack\");
}
public object top()
{
if (! isEmpty())
return top.getInfo();
else
throw new StackUnderflowException(\"top attempted on an empty stack\");
}
public boolean isEmpty()
{
return top == null;
}
public boolean isFull()
{
return false;
}
35 import java.util.*;
class Node
{
protected int data;
protected Node link;
public Node()
{
link = null;
datq = 0;
}
public Node(int d, Node n)
{
data = d;
link = n;
}
public void setLink(Node n)
{
link = n;
}
public void setData(int d)
{
data = d;
}
public Node getLink()
{
return link;
}
public int getData()
{
return data;
}
}
class linkedStack
{
protected Node top;
protected int size;
public linkedStack()
{
top = null;
size = 0;
}
public boolean isEmpty()
{
return top == null;
}
public int getSize()
{
return size;
}
public foid push(int data);
{
Node nptr = new Node (data, null);
if (top == null)
top = nptr;
else
{
nptr.setLink(top);
top = nptr;
}
size++;
}
public int pop()
{
if(isEmpty())
throw new NOsuchElementException(\"Underflow Exception\");
Node ptr = top;
top = ptr.getLink();
size ---;
return ptr.getData();
}
public in tpeek()
{
if (isEmpty())
throw new NoSuchElementException(\"undrflow Exception\");
return top.getData();
}
public void display()
{
system.out.println(\"stack =\");
if (size == 0)
{
system.out.println(\"empty\");
return;
}
Node ptr = top;
while (ptr ! = null)
{
System.out.println(ptr.getData()+\"\");
ptr = ptr.getLink();
}
}
Public clss LInkedStackImplement
{
public static void main (string[] agrs)
{
Scanner scan = new Scanner (system.in);
linkedStack is = new linkedStack();
System.out.println(\"linked list Test\");
char ch;
do
{
system.out.println(\"linked stack operations\");
System.out.printtln(\"1.push\");
System.out.println(\"2.pop\");
System.out.println(\"3.peek\");
System.out.println(\"4 check empty\");
System.out.println(\"5. size\");
int.choice = scan.nextInt();
switch (choice)
{
case 1:
System.out.println(\"enter integer element to push\");
Is.push(scan.nextInt());
break;
case 2;
try
{
System.ut.println(\"poped element =\"+ Is.pop());
}
catch (Exception e)
{
System.out.println(\"Error: \"+e.getMessage());
}
break;
case 3;
try
{
System.out.println(\"peek element=\"+ Is.peek());
}
catch.(Exception e)
{
System.out.println(\"Error :\" +e.getMessage());
}
break;
case 4:
system.out.println(\"empty status =\"+ Is.isempty());
break;
case 5:
System.out.println(\"size = \"+ Is.getSize());
break;
case 6:
System.out.println(\"stack =\");
Is.display();
break;
default:
System.out.println(\"wrongly entry \");
break;
}
Is.display();
System.outr.pritln(\"you wnt tocaontinue (type y or n)/n\");
ch = scan.next().charAt(0);
}
while (ch == \'y\' || ch == \'y\');
}
}
)
36 application (user)
37.implement
38 Logical
39 pop followed by push
40 information and behaviour
41 methods






