Stack using Linked List- Java

import java.util.*;
import java.io.*;
import java.lang.*;
class Nodes
{
  int data;
  Nodes next;
}
class List
 {
   Nodes first,last,node;
   int d;
   Scanner s=new Scanner(System.in);
   List()
    {
     first=last=null;
    } 

   void push()
   {
    System.out.println("data");
    int d=s.nextInt();
    Nodes node=new Nodes();
    node.data=d;
    node.next=null;

    if(first==null) first=last=node;
    else{ node.next=first;first=node;}
      
   }

     void dis()
     {
       Nodes t=new Nodes();;
       t=first;

      System.out.println("list..");
      while(t!=null)
      {

      System.out.print(t.data + "--->");
      t=t.next;
      }
       System.out.print("NULL");
     } 

     void pop()
     {

      if(first==null)  System.out.println("empty...");
    else
      {
      System.out.println(" Popped item is ....." +       first.data);
      first=first.next;

     } 
    }

}

class Main
{

 public static void main(String args[])
 {
   List l=new List();
   Scanner s=new Scanner(System.in);
   int op;
   while(true)
   {
   System.out.println("\n1.push\n2.pop\n3.dis\n4.exit");
   System.out.println("Enter option....");
   op=s.nextInt();
   switch(op)
   {

    case 1:l.push();
           break;
    case 2:l.pop();
           break;
    case 3:l.dis();
           break;  
    case 4:System.exit(0);
   }

   }
}

}

Comments

Popular posts from this blog

KTU OOP LAB JAVA CSL 203 BTech CS S3 - Dr Binu V P

Syllabus and Practice Questions KTU OOPS Lab Java CSL 203

String Problems