Stack Using Linkedlist - Datastructure - JAVA

Program:
import java.io.*;
class Node
{
    public int data;
    public Node next;
    public Node(int x)
    {
        data=x;
    }
    public void display()
    {
        System.out.print(data+" ");
    }
}
class stacker
{
    public Node first;
    public stacker()
    {
        first=null;
    }
    public boolean isEmpty()
    {
        return(first==null);
    }
    public void push(int value)
    {
        Node newnode=new Node(value);
        newnode.next=first;
        first=newnode;
    }
    public int pop()
    {
        if(isEmpty())
         {
            System.out.println("\n Stack Empty...");
            return 0;
         }
        else
         {
            int r1=first.data;
            first=first.next;
            return r1;
         }
       
    }
    public void display()
    {
        Node current=first;
        while(current!=null)
         {
            System.out.print(" "+current.data);
            current=current.next;
         }
        System.out.println("");
    }
}
class stack_list
{
public static void main(String arg[]) throws IOException
{
    stacker a1=new stacker();
    DataInputStream x=new DataInputStream(System.in);
    System.out.print("\n\t\t<<STACK USING LINKEDLIST>>\n\nStack ready...\n\nEnter number of elements to push: ");
    int n1=Integer.parseInt(x.readLine());
    System.out.println("\nEnter elements: ");
    for(int i=0;i<n1;i++)
     {
        a1.push(Integer.parseInt(x.readLine()));
     }
    System.out.println("\nDo you want to see the stack items?\n\t1.Yes 2.No");
    switch(Integer.parseInt(x.readLine()))
    {
        case 1:
            System.out.println("\nThe current stack items:");
            a1.display();
            break;
        case 2:
        default:
            break;
    }
    System.out.println("\nEnter the number of elements to pop: ");
    int n2=Integer.parseInt(x.readLine());
    for(int i=0;i<n2;i++)
     {
        System.out.println(a1.pop()+" is poped from stack... ");
     }
    System.out.println("\nDo you want to see the stack items?\n\t1.Yes 2.No");
    switch(Integer.parseInt(x.readLine()))
    {
        case 1:
            System.out.println("\nThe current stack items:");
            a1.display();
            break;
        case 2:
        default:
            break;
    }
}
}
   
Output:
nn@linuxmint ~ $ javac stack_list.java
Note: stack_list.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
nn@linuxmint ~ $ java stack_list

        <<STACK USING LINKEDLIST>>

Stack ready...

Enter number of elements to push: 3

Enter elements: 
1
2
3

Do you want to see the stack items?
    1.Yes 2.No
1

The current stack items:
 3 2 1

Enter the number of elements to pop: 
1
3 is poped from stack... 

Do you want to see the stack items?
    1.Yes 2.No
1

The current stack content:
 2 1
nn@linuxmint ~ $

Electric Circuits & Systems- Previous Question Papers -Semester 4 - 2009 - 2K6CS 406








Computer Organization & Design(COD) - Previous Question Papers - Semester 4 - 2009 - 2K6CS 405




Microprocessors & Microcontrollers (MMD) - Previous Question Paper -Semester 4 - 2009 - 2K6CS 403




Systems Programming (SP) - Previous Question Papers -Semester 4 - 2009 - 2K6CS 403




Data Structures & Algorithms - Previous Question Papers -Semester 4 - 2009 - 2K6CS 402




Engineering Mathematics III - Previous Question Paper -Semester 4 - 2009 - 2K6CS 401




Electrical Circuits & Systems - A C Bridges


Stack Using Array - Datastructure - JAVA

Program:
import java.io.*;
class stackarray
{   
    private int maxsize;
    private int st[];
    public int top;
    public stackarray(int size)
    {
        maxsize=size;
        st=new int[maxsize];
        top=-1;
    }

    public boolean isempty()
    {
        return(top==-1);
    }
    public boolean isfull()
    {
        return(top==(maxsize-1));
    }
    public void push(int data)
    {
        if(isfull())
            System.out.println("Error:stack overflow");
        else
            {
                st[++top]=data;
                System.out.println("\tpushing to stack.... "+data);
            }

    }
    public int pop()
    {
        if(isempty())
        {
            System.out.print("\nError:empty");
            return 0;
        }
        else
        {
            return st[top--];
        }
    }
    public void display()
    {
        System.out.println("\n--STACK ITEMS-- ");
        for(int i=0;i<=top;i++)       
            System.out.print(st[i]+" ");
    }
}
class stack_array
{
public static void main(String arg[])throws IOException
{
    System.out.print("\n\n\t\t<<<STACK USING ARRAY>>>\nEnter the size of array:");
    DataInputStream x= new DataInputStream(System.in);
    stackarray ar1=new stackarray(Integer.parseInt(x.readLine()));
    int proceed=1;
    System.out.print("Enter the number of elements to push:");
    int p=Integer.parseInt(x.readLine());   
    System.out.println("Enter the elements:");
    for(int k=0;k<p;k++)
        ar1.push(Integer.parseInt(x.readLine()));
    ar1.display();
    System.out.print("\nEnter the number of elements to pop:");
    int q=Integer.parseInt(x.readLine());   
    for(int k=0;k<q;k++)
        System.out.println("\nPoping.... "+ar1.pop());
   
    do{
        ar1.display();
        System.out.println("\n---MENU---\n1.Push an element to stack.\n2.Pop an element from stack\n3.Check Full\n4.Check Empty\n5.Display Stack Items \n");
        int s1=Integer.parseInt(x.readLine());   
        switch(s1)
        {
            case 1:
                System.out.print("---PUSH AN ELEMENT---\nEnter the element:");
                ar1.push(Integer.parseInt(x.readLine()));
                break;
            case 2:
                System.out.print("---POP AN ELEMENT---\nPoping.... "+ar1.pop());
                break;
            case 3:
                if(ar1.isfull())
                    System.out.println("Stack is full.");
                else
                    System.out.println("Stack is not full.");
                break;
            case 4:
                if(ar1.isempty())
                    System.out.println("Stack is empty.");
                else
                    System.out.println("Stack is not empty.");
            case 5:
                    ar1.display();
            default:
                break;
        }
        System.out.println("\nPress 1 to continue...");
        proceed=Integer.parseInt(x.readLine());   
    }while(proceed==1);


}   
}

Output:
nn@linuxmint ~ $ javac stack_array.java
Note: stack_array.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
nn@linuxmint ~ $ java stack_array


        <<<STACK USING ARRAY>>>
Enter the size of array:3
Enter the number of elements to push:3
Enter the elements:
1
    pushing to stack.... 1
2
    pushing to stack.... 2
3
    pushing to stack.... 3

--STACK ITEMS--
1 2 3
Enter the number of elements to pop:1

Poping.... 3

--STACK ITEMS--
1 2
---MENU---
1.Push an element to stack.
2.Pop an element from stack
3.Check Full
4.Check Empty
5.Display Stack Items

1
---PUSH AN ELEMENT---
Enter the element:4
    pushing to stack.... 4

Press 1 to continue...
1

--STACK ITEMS--
1 2 4
---MENU---
1.Push an element to stack.
2.Pop an element from stack
3.Check Full
4.Check Empty
5.Display Stack Items

3
Stack is full.

Press 1 to continue...
1

--STACK ITEMS--
1 2 4
---MENU---
1.Push an element to stack.
2.Pop an element from stack
3.Check Full
4.Check Empty
5.Display Stack Items

2
---POP AN ELEMENT---
Poping.... 4
Press 1 to continue...
1

--STACK ITEMS--
1 2
---MENU---
1.Push an element to stack.
2.Pop an element from stack
3.Check Full
4.Check Empty
5.Display Stack Items

4
Stack is not empty.

--STACK ITEMS--
1 2
Press 1 to continue...
1

--STACK ITEMS--
1 2
---MENU---
1.Push an element to stack.
2.Pop an element from stack
3.Check Full
4.Check Empty
5.Display Stack Items

5

--STACK ITEMS--
1 2
Press 1 to continue...
2
nn@linuxmint ~ $

Leland L. Beck, “System Software – An Introduction to Systems Programming”, 3rd Edition - Systems Programming (SP)




System Software – An Introduction to Systems Programming
Leland L. Beck



DoublyLinkedList - Datastructure - JAVA

Program:
import java.io.*;
class Node
{
    public int data;
    public Node next;
    public Node previous;
    public Node(int x)
    {
        data=x;
    }
    public void display()
    {
        System.out.print(data+" ");
    }
}
class lab
{
    private Node first;
    private Node last;
    public lab()
    {
        first=null;
        last=null;
    }
    public void insertfirst(int x)
        {
          Node newnode=new Node(x);
      if(isEmpty())
        last=newnode;
      else
        first.previous=newnode;
        newnode.next=first;
        first=newnode;
    }
    public void insertlast(int y)
    {
        Node newnode=new Node(y);
        if(isEmpty())
            first=newnode;
        else
         {
            last.next=newnode;
            newnode.previous=last;
         }
        last=newnode;
    }
    public boolean isEmpty()
    {
        return(first==null&&last==null);
    }
    public void display()
    {
        Node current=first;
        while(current!=null)
         {
            System.out.print(" "+current.data);
            current=current.next;
         }
    }
    public void delfirst()
    {
        first=first.next;
        first.previous=null;
    }
    public void dellast()
    {
        last=last.previous;
        last.next=null;
    }
    public void insertatp(int p,int x2)
    {
        Node newnode=new Node(x2);
        Node current=first;
        int count=1;
        while(count+1<p)
        {
            current=current.next;
            count++;
            if(current.next==null)
             {
                System.out.print("Error");
                break;
             }
        }
        Node temp=current.next;
        current.next=newnode;
        newnode.next=temp;
    }
    public void deletatp(int p1)
    {
        Node current=first;
        int count=1;
        while(count+1<p1)
        {
            current=current.next;
            count++;
            if(current.next==null)
             {
                System.out.print("Error");
                break;
             }
        }
        Node temp=current.next;
        current.next=temp.next;
       
    }
       
       
       
}

class doublylinkedlist
{
public static void main (String arg[]) throws IOException
 {
    lab a1=new lab();
    DataInputStream x=new DataInputStream(System.in);
    System.out.print("Enter the number of elements:");
    int n= Integer.parseInt(x.readLine());
    System.out.print("\nInsert at first:\n");
    System.out.print("\nEnter elements:\n");
   
    for(int i=0;i<n;i++)
        a1.insertfirst(Integer.parseInt(x.readLine()));
    a1.display();
    System.out.print("\nInsert last:\n");
    System.out.print("Enter elements:\n");   
    for(int i=0;i<n;i++)
        a1.insertlast(Integer.parseInt(x.readLine()));
    a1.display();
        System.out.println("\nDo you want to delete first element:\n1.Yes\t2.No");
    switch(Integer.parseInt(x.readLine()))
    {
        case 1:
             a1.delfirst(); break;
        case 2:       
        default:    break;
    }
    a1.display();
    System.out.println("\nDo you want to delete the last element:\n1.Yes\t2.No");
    switch(Integer.parseInt(x.readLine()))
    {
        case 1:
             a1.dellast(); break;
        case 2:       
        default:    break;
    }
    a1.display();
    System.out.print("\nInsert at particular position:\nEnter the position & element:\n");
    a1.insertatp(Integer.parseInt(x.readLine()),Integer.parseInt(x.readLine()));
    a1.display();
    System.out.print("\nDelete from particular position:\nEnter the position:\n");
    a1.deletatp(Integer.parseInt(x.readLine()));
    a1.display();
}}

Output:
nn@linuxmint ~ $ javac doublylinkedlist.java
Note: doublylinkedlist.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
nn@linuxmint ~ $ java doublylinkedlist
Enter the number of elements:5

Insert at first:

Enter elements:
5
4
3
2
1
 1 2 3 4 5
Insert last:
Enter elements:
6
7
8
9
10
 1 2 3 4 5 6 7 8 9 10
Do you want to delete first element:
1.Yes    2.No
1
 2 3 4 5 6 7 8 9 10
Do you want to delete the last element:
1.Yes    2.No
1
 2 3 4 5 6 7 8 9
Insert at particular position:
Enter the position & element:
5
6
 2 3 4 5 6 6 7 8 9
Delete from particular position:
Enter the position:
5
 2 3 4 5 6 7 8 9

Linkedlist (singly) - Datastructure - JAVA

Program:
import java.io.*;
class Node
{
    public int data;
    public Node next;
    public Node(int x)
    {
        data=x;
    }
    public void display()
    {
        System.out.print(data+" ");
    }
}
class linkedList
{
    public Node first;
    public linkedList()
    {
        first=null;
    }
    public void insertfirst(int value)    //insert at the first of the list
    {
        Node n1=new  Node(value);
        n1.next=first;
        first=n1;
    }
    public void display()
    {   
        System.out.print("");
        Node current=first;
        while(current!=null)
        {
            current.display();
            current=current.next;
        }
    }
    public void insertlast(int value)  //insert at the last of the list
    {
        Node n1=new  Node(value);
        Node current=first;
        if(current==null)
        {
            n1.next=first;
            first=n1;
        }
        else
        {
            while(current.next!=null)
            {
                current=current.next;
            }
       
            current.next=n1;
        }
    }
    public int find(int key) //Search an element in the list
    {
        Node current=first;int pos=1;
        while(current.data!=key)
        {
        if(current.next==null)
            return -1;
        else
        {
            current=current.next;pos++;
        }
        }
        return pos;
    }
    public void delete(int key) //Delete an element in the list
    {
        Node current=first;
        if(current==null)
        {
            System.out.println("Error:Empty.");           
            return ;
        }
        Node previous= null;
        while(current.data!=key)
        {
            if(current.next==null)
            {
                System.out.println("Error:Element not present.");           
                return ;
            }
            else
            {
                previous=current;
                current=current.next;
            }
        }
        if(current==first)
            first=first.next;
        else
            previous.next=current.next;
    }
    public int numberofnodes() //Find number of nodes in the list
    {
        Node current=first;int i=0;
        while(current!=null)
        {
            current=current.next;
            i++;
        }
        return i;
    }
    public void insertatp(int p,int x) //  //insert at a particular position in the list
    {
        int i;
        if(p<1 || p-1>numberofnodes())
            System.out.println("error");
        else
        {
            Node newnode= new Node(x);
            if(p==1)
            {
                newnode.next=first;
                first=newnode;
            }
            else
            {   
                Node current=first;   
                for(int k=1;k<p-1;k++)
                current=current.next;
                Node temp=current.next;
                current.next=newnode;
                newnode.next=temp;
            }
        }
    }
       
    public void sort() //Sort the list
    {
    Node fnode,snode;
        int t;
        fnode=first;
        while(fnode!=null)
        {
            snode=fnode.next;
            while(snode!=null)
            {
            if(fnode.data>snode.data)
            {
                t=fnode.data;
                fnode.data=snode.data;
                snode.data=t;
            }
            snode=snode.next;
            }
        fnode=fnode.next;
        }
    }
}
class list
{
    public static void main(String arg[]) throws IOException
    {
        linkedList l1= new linkedList();
        System.out.print("Enter the number of elements:");
        DataInputStream x= new DataInputStream(System.in);
        int n=Integer.parseInt(x.readLine());
        System.out.print("Enter the elements:");
        for(int i=0;i<n;i++)
            l1.insertlast(Integer.parseInt(x.readLine()));
        l1.display();
        boolean proceed=true;
        do{
        System.out.print("\n\n~~MENU~~\n1.Insert\n2.Search an element\n3.Delete an element\n4.Find number of nodes\n5.Sort\n6.Exit>>\nEnter your choice:");
        int n1=Integer.parseInt(x.readLine());
        switch(n1)
        {
            case 1: System.out.print("\n\n~~MENU~~\n1.Insert at first \n2.Insert at last\n3.Insert at particular position\n4.Cancel\nEnter your choice:");
                int n2=Integer.parseInt(x.readLine());
                switch(n2)
                {
                    case 1:    System.out.println("\nInsert at first:\nEnter element: ");
                        l1.insertfirst(Integer.parseInt(x.readLine()));
                        System.out.println("After Insertion.....");
                        l1.display();
                        break;
                    case 2:    System.out.println("\nInsert at last:\nEnter element: ");
                        l1.insertlast(Integer.parseInt(x.readLine()));
                        System.out.println("After Insertion.....");
                        l1.display();
                        break;
                    case 3:    System.out.println("\nInsert at a particular position:\nEnter position &  element: ");
                        l1.insertatp(Integer.parseInt(x.readLine()),Integer.parseInt(x.readLine()));
                        System.out.println("After Insertion.....");
                        l1.display();
                        break;
                    case 4:    l1.display();               
                    default: break;
                }
               
                break;

            case 2: System.out.println("\nEnter the element to find:");
                int f=l1.find(Integer.parseInt(x.readLine()));
                if(f>=0)
                    System.out.print("The element is present at position "+f);
                else
                    System.out.print("The element is no more....");
                break;
            case 3:    System.out.println("\nEnter the element to delete:");
                l1.delete(Integer.parseInt(x.readLine()));
                System.out.println("After deletion:");
                l1.display();       
            case 4:    System.out.println("\nNumber of nodes = "+l1.numberofnodes());
                break;

            case 5:    System.out.println("Sorting.....");
                l1.sort();
                l1.display();
                break;
       
            case 6:
            default:proceed=false;
                break;
        }
        }while(proceed);


}
}

Output:
nn@linuxmint ~ $ javac list.java
Note: list.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
nn@linuxmint ~ $ java list
Enter the number of elements:3
Enter the elements:2
4
5
2 4 5 

~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:1


~~MENU~~
1.Insert at first 
2.Insert at last
3.Insert at particular position
4.Cancel
Enter your choice:1

Insert at first:
Enter element: 
1
After Insertion.....
1 2 4 5 

~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:1


~~MENU~~
1.Insert at first 
2.Insert at last
3.Insert at particular position
4.Cancel
Enter your choice:2

Insert at last:
Enter element: 
6
After Insertion.....
1 2 4 5 6 

~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:1


~~MENU~~
1.Insert at first 
2.Insert at last
3.Insert at particular position
4.Cancel
Enter your choice:3

Insert at a particular position:
Enter position &  element: 
3
3
After Insertion.....
1 2 3 4 5 6 

~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:3

Enter the element to delete:
1
After deletion:
2 3 4 5 6 
Number of nodes = 5


~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:1


~~MENU~~
1.Insert at first 
2.Insert at last
3.Insert at particular position
4.Cancel
Enter your choice:1

Insert at first:
Enter element: 
7
After Insertion.....
7 2 3 4 5 6 

~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:4

Number of nodes = 6


~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:5
Sorting.....
2 3 4 5 6 7 

~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:1


~~MENU~~
1.Insert at first 
2.Insert at last
3.Insert at particular position
4.Cancel
Enter your choice:4
2 3 4 5 6 7 

~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:6
nn@linuxmint ~ $
Related Posts Plugin for WordPress, Blogger...