Showing posts with label Semester 4. Show all posts
Showing posts with label Semester 4. Show all posts

Kannur University Exam Results - Semester 4 - Btech -2010

The exam results of semester 4 ,b.tech ,2010 has been published.Click below to go to the University site.

Kannur University:Exam Results

Pattersen D.A. & Hennesy J.L., Computer Organisation & Design: The Hardware/ Software Interface, Harcourt Asia - COD -



Computer Organisation & Design: The Hardware/ Software Interface
Pattersen D.A. & Hennesy J.L





Sorting Algorithms - JAVA

Program:
import java.io.*;
class arraylist
{
    private int[] a;
    private int n;
    private int[] temp;
    public arraylist(int x)
    {
        a = new int[x];
        n=0;
        temp = new int[x];
    }
    public void insert(int e)
    {
        n++;
        a[n]=e;
    }
    public void display()
    {
        for(int i=1;i<=n;i++)
        System.out.print(a[i]+" ");
        System.out.println("\n");
    }
    public void bubble()    //Bubblesort
    {   
        System.out.print("(bubblesort)\n\n");
        display();
        for(int i=1;i<n;i++)
        for(int j=1;j<=n-i;j++)
        if(a[j]>a[j+1])
        {
            swap(j,j+1);
            display();
        }
    }
    public void swap(int x,int y)
    {
            int temp=a[x];
            a[x]=a[y];
            a[y]=temp;
    }
    public void selection()    //selectionsort
    {
        System.out.print("(selectionsort)\n\n");
        display();
        for(int i=1;i<n;i++)
        {
            int min=i;
            for(int j=i+1;j<=n;j++)
                if(a[j]<a[min])
                    min=j;
            swap(i,min);
            display();
        }
    }
    public void insertion()    //insertionsort
    {
        System.out.print("(insertionsort)\n\n");
        display();
        for(int j=2;j<=n;j++)
        {
            int key=a[j];int i=j-1;
            while((i>0)&&(a[i]>key))
            {
                a[i+1]=a[i];
                i--;
            }
            a[i+1]=key;
        display();
        }
    }
    public void quick(int first,int last)    //quicksort
    {
       
       
        if(last>first)
        {
            int i=first;int j=last+1;int pivot=a[first];
            do
            { 
                do{
                    i=i+1;
                  }while((a[i]<=pivot)&&(i<last));
                do{
                    j=j-1;   
                  }while((a[j]>=pivot)&&(j>first));
                   
                if(i<j)
                {   
                   
                    swap(i,j);
                    display();
                }
            }while(i<j);
            swap(first,j);
            display();
            quick(first,j-1);
            quick(j+1,last);
        }
    }
    public void merge(int low,int high)    //mergesort
    {
        if(low!=high)
        {
            int mid=(low+high)/2;
            merge(low,mid);
            merge(mid+1,high);
            rmerge(low,mid,high);
        }
    }
    public void rmerge(int low,int mid,int high)
    {
       
        int i=low;int j=mid+1;int k=low;
       
        while((i<=mid)&&(j<=high))
        {
            if(a[i]<=a[j])
                {
                    temp[k]=a[i];k++;i++;
                }
            else
                {
                    temp[k]=a[j];k++;j++;
                }
        }
        while(i<=mid)
        {
            temp[k]=a[i];k++;i++;
        }
        while(j<=high)
        {
            temp[k]=a[j];k++;j++;
        }
        for(i=1;i<=high;i++)
            a[i]=temp[i];
            display();

    }
    public void buildheap()    //heapsort
    {
        for(int i=n/2;i>=1;i--)
        maxheapify(n,i);
    }
    public void maxheapify(int n,int i)
    {
        int largest;
        int l=left(i);
        int r=right(i);
        if(l<=n&&a[l]>a[i])
            largest=l;
        else
            largest=i;
        if(r<=n&&a[r]>a[largest])
            largest=r;
        if(largest!=i)
        {
            swap(i,largest);
            maxheapify(n,largest);
        }
    }
    public void heapsort()
    {
        buildheap();
        int heapsize=n;
        for(int i=heapsize;i>=2;i--)
        {
            swap(i,1);
            display();
            heapsize--;
            maxheapify(heapsize,1);
        }
    }
    public int left(int a)
    {
        return 2*a;
    }
    public int right(int b)
    {
        return 2*b+1;
    }
    public void nidhi()
    {
        System.out.print("\t\t\t\t\t\t* www.2k8618.blogspot.com *\n");
    }
   
    public void end()
    {
        System.out.println("\n\n\t\t\t***     ***\n\t\t\t*****   ***\n\t\t\t*** *** ***\n\t\t\t***   *****\n\t\t\t***    ****\n");
        System.out.print("\n\t\t<<<<SORTING ALGORITHMS>>>>\n");
        char z=177;
        System.out.print("\t\t"+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z);
        char y=2;
        System.out.print("\n\t\t\t @ 2010 - www.2k8618.blogspot.com -\n\t\t2k8618@gmail.com,S4-CSE,GCEK\n");
        for(int x=1;x<=75;x++)
            System.out.print(""+y);
        System.out.print("\n");
    }

}

class nsort
{
    public static void main(String arg[]) throws IOException
    {
        int exit=1;arraylist ob;
        System.out.println("\n\n\t\t\t***     ***\n\t\t\t*****   ***\n\t\t\t*** *** ***\n\t\t\t***   *****\n\t\t\t***    ****\n");
        System.out.print("\n\t\t<<<<SORTING ALGORITHMS>>>>\n");
        char z=177;
        System.out.print("\t\t"+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z+z);
        do{
           
            DataInputStream x1= new DataInputStream(System.in);
       
            System.out.print("\nEnter the number of elements:");
            int na=Integer.parseInt(x1.readLine());
            ob=new arraylist(na+1);
            System.out.println("Enter the elements:");
            for (int k=1;k<=na;k++)
                ob.insert(Integer.parseInt(x1.readLine()));
            ob.nidhi();
            System.out.println("Before sorting:\n");
            ob.display();
            System.out.println("Enter your choice:\n\n\t1.BUBBLESORT\n\t\t2.SELECTIONSORT\n\t\t\t3.INSERTIONSORT\t\n\t4.QUICKSORT\n\t\t5.MERGESORT\n\t\t\t6.HEAPSORT\t\t*www.2k8618.blogspot.com*\n\t\t\t\t\t7.Exit>>>\n");       
            int turn=Integer.parseInt(x1.readLine());
            if(turn>=1 && turn <=6)
                System.out.println("Sorting.............");
       
            ob.nidhi();
            switch(turn)
            {
                case 1:       
                    ob.bubble();
                    System.out.println("After sorting:\n");
                    break;
                case 2:       
                    ob.selection();
                    System.out.println("After sorting:\n");
                    break;
                case 3:       
                    ob.insertion();
                    System.out.println("After sorting:\n");
                    break;
                case 4:
                    System.out.print("(QuickSort)\n\n");
                    ob.display();
                    ob.quick(1,na);
                    System.out.println("After sorting:\n");
                    break;
                case 5:
                    System.out.print("(MergeSort)\n\n");
                    ob.display();
                    ob.merge(1,na);
                    System.out.println("After sorting:\n");
                    break;
                case 6:
                    System.out.print("(HeapSort)\n\n");
                    ob.display();
                    ob.heapsort();
                    System.out.println("After sorting:\n");
                    break;
                case 7:
                    exit=0;
                    System.out.println("Not sorted:\n");
                    break;
                default:
                    System.out.println("Not sorted:\n");
                    break;
            }
       
            ob.display();ob.nidhi();
            System.out.println("\nPress 1 to continue...");
            exit=Integer.parseInt(x1.readLine());
        }while(exit==1);
        ob.end();
    }
}

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


            ***     ***
            *****   ***
            *** *** ***
            ***   *****
            ***    ****


        <<<<SORTING ALGORITHMS>>>>
        ±±±±±±±±±±±±±±±±±±±±±±±±±±
Enter the number of elements:5
Enter the elements:
5
4
3
2
1
                        * www.2k8618.blogspot.com *
Before sorting:

5 4 3 2 1 

Enter your choice:

    1.BUBBLESORT
        2.SELECTIONSORT
            3.INSERTIONSORT   
    4.QUICKSORT
        5.MERGESORT
            6.HEAPSORT        *www.2k8618.blogspot.com*
                    7.Exit>>>

1
Sorting.............
                        * www.2k8618.blogspot.com *
(bubblesort)

5 4 3 2 1 

4 5 3 2 1 

4 3 5 2 1 

4 3 2 5 1 

4 3 2 1 5 

3 4 2 1 5 

3 2 4 1 5 

3 2 1 4 5 

2 3 1 4 5 

2 1 3 4 5 

1 2 3 4 5 

After sorting:

1 2 3 4 5 

                        * www.2k8618.blogspot.com *

Press 1 to continue...
1

Enter the number of elements:5
Enter the elements:
5
4
3
2
1
                        * www.2k8618.blogspot.com *
Before sorting:

5 4 3 2 1 

Enter your choice:

    1.BUBBLESORT
        2.SELECTIONSORT
            3.INSERTIONSORT   
    4.QUICKSORT
        5.MERGESORT
            6.HEAPSORT        *www.2k8618.blogspot.com*
                    7.Exit>>>

2
Sorting.............
                        * www.2k8618.blogspot.com *
(selectionsort)

5 4 3 2 1 

1 4 3 2 5 

1 2 3 4 5 

1 2 3 4 5 

1 2 3 4 5 

After sorting:

1 2 3 4 5 

                        * www.2k8618.blogspot.com *

Press 1 to continue...
1

Enter the number of elements:5
Enter the elements:
5
4
3
2
1
                        * www.2k8618.blogspot.com *
Before sorting:

5 4 3 2 1 

Enter your choice:

    1.BUBBLESORT
        2.SELECTIONSORT
            3.INSERTIONSORT   
    4.QUICKSORT
        5.MERGESORT
            6.HEAPSORT        *www.2k8618.blogspot.com*
                    7.Exit>>>

3
Sorting.............
                        * www.2k8618.blogspot.com *
(insertionsort)

5 4 3 2 1 

4 5 3 2 1 

3 4 5 2 1 

2 3 4 5 1 

1 2 3 4 5 

After sorting:

1 2 3 4 5 

                        * www.2k8618.blogspot.com *

Press 1 to continue...
1

Enter the number of elements:5
Enter the elements:
5
4
3
2
1
                        * www.2k8618.blogspot.com *
Before sorting:

5 4 3 2 1 

Enter your choice:

    1.BUBBLESORT
        2.SELECTIONSORT
            3.INSERTIONSORT   
    4.QUICKSORT
        5.MERGESORT
            6.HEAPSORT        *www.2k8618.blogspot.com*
                    7.Exit>>>

4
Sorting.............
                        * www.2k8618.blogspot.com *
(QuickSort)

5 4 3 2 1 

1 4 3 2 5 

1 4 3 2 5 

1 2 3 4 5 

1 2 3 4 5 

After sorting:

1 2 3 4 5 

                        * www.2k8618.blogspot.com *

Press 1 to continue...
1

Enter the number of elements:5
Enter the elements:
5
4
3
2
1
                        * www.2k8618.blogspot.com *
Before sorting:

5 4 3 2 1 

Enter your choice:

    1.BUBBLESORT
        2.SELECTIONSORT
            3.INSERTIONSORT   
    4.QUICKSORT
        5.MERGESORT
            6.HEAPSORT        *www.2k8618.blogspot.com*
                    7.Exit>>>

5
Sorting.............
                        * www.2k8618.blogspot.com *
(MergeSort)

5 4 3 2 1 

4 5 3 2 1 

3 4 5 2 1 

3 4 5 1 2 

1 2 3 4 5 

After sorting:

1 2 3 4 5 

                        * www.2k8618.blogspot.com *

Press 1 to continue...
1

Enter the number of elements:5
Enter the elements:
5
4
3
2
1
                        * www.2k8618.blogspot.com *
Before sorting:

5 4 3 2 1 

Enter your choice:

    1.BUBBLESORT
        2.SELECTIONSORT
            3.INSERTIONSORT   
    4.QUICKSORT
        5.MERGESORT
            6.HEAPSORT        *www.2k8618.blogspot.com*
                    7.Exit>>>

6
Sorting.............
                        * www.2k8618.blogspot.com *
(HeapSort)

5 4 3 2 1 

1 4 3 2 5 

1 2 3 4 5 

1 2 3 4 5 

1 2 3 4 5 

After sorting:

1 2 3 4 5 

                        * www.2k8618.blogspot.com *

Press 1 to continue...
2


            ***     ***
            *****   ***
            *** *** ***
            ***   *****
            ***    ****


        <<<<SORTING ALGORITHMS>>>>
        ±±±±±±±±±±±±±±±±±±±±±±±±±±
             @ 2010 - www.2k8618.blogspot.com -
        2k8618@gmail.com,S4-CSE,GCEK

nn@linuxmint ~ $

Breadth First Search (BFS) - Datastructure - JAVA

Program:
import java.io.*;
class quelist
{
  public int front;
  public int rear;
  public int maxsize;
  public int[] que;
 
  public quelist(int size)
  {
    maxsize = size;
    que = new int[size];
    front = rear = -1;
  }
  
  public void display()
  {
     for(int i = front;i<=rear;i++)
         System.out.print(que[i]+"    ");
  }
 
  public void enque(int x)
  {
     if(front==-1)
     front = 0;
     que[++rear]=x;
  }

  public int deque()
  {
    int temp = que[front];
    front = front +1;
    return temp;
  }

  public boolean isempty()
  {
    return((front>rear)||(front==-1));
  }
}    

class vertex
{
 public char label;
 public boolean wasvisited;
 
 public vertex(char lab)
 {
   label = lab;
   wasvisited = false;
 }
}

class graph
{
  public final int MAX = 20;
  public int nverts;
  public int adj[][];
  public vertex vlist[];
  quelist qu; 

  public graph()
  {
   nverts = 0;
   vlist = new vertex[MAX];
   adj = new int[MAX][MAX];
   qu = new quelist(MAX);
   for(int i=0;i<MAX;i++)
    for(int j=0;j<MAX;j++)
     adj[i][j] = 0;
  }

  public void addver(char lab)
  {
    vlist[nverts++] = new vertex(lab);
  }

  public void addedge(int start,int end)
  {
    adj[start][end] = 1;
    adj[end][start] = 1;
  }
  
  public int getadjunvis(int i)
  {
    for(int j=0;j<nverts;j++)
      if((adj[i][j]==1)&&(vlist[j].wasvisited==false))
      return j;
    return (MAX+1);    
  }

  public void display(int i)
  {
    System.out.print(vlist[i].label);
  }

  public int getind(char l)
  {
    for(int i=0;i<nverts;i++) 
      if(vlist[i].label==l)
      return i;
    return (MAX+1);
  }

  public void brfs()
  {
    vlist[0].wasvisited = true;
    display(0);
    qu.enque(0);
    int v2;
    while(!(qu.isempty()))
    { 
     int v1 = qu.deque();
     while((v2=getadjunvis(v1))!=(MAX+1))
      { 
    vlist[v2].wasvisited = true;
        display(v2);
        qu.enque(v2);
      }    
    }
    System.out.print("\n");
  }
}

class bfs
{
  public static void main(String args[])throws IOException
  {
    graph gr = new graph();
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    System.out.println("Enter the number of vertices");
    int n = Integer.parseInt(br.readLine());
    System.out.println("Enter the labels for the vertices");
    for(int i=0;i<n;i++)
    {
      String temp = br.readLine();
      char ch = temp.charAt(0);
      gr.addver(ch);
    }
    System.out.println("Enter the number of edges");
    int edg = Integer.parseInt(br.readLine());
    System.out.println("Enter the vertices which you need to connect");
    for(int j=0;j<edg;j++)
    {
      System.out.println("Enter the first vertex");
      String t = br.readLine();
      char c = t.charAt(0);
      int start = gr.getind(c);
    
      System.out.println("Enter the second vertex");
      t = br.readLine();
      c = t.charAt(0);
      int end = gr.getind(c);
    
      gr.addedge(start,end);
    }
    System.out.print("The vertices in the graph traversed breadthwise:");
    gr.brfs();
  }  
}  

Output:

Binary Search Tree - Datastructure - JAVA

Program:
import java.io.*;
class Node
{
    int data;
    public Node left;
    public Node right;
    public Node(int value)
    {
        data=value;
    }
}
class tree
{
    public Node insert(int x,Node root)
    {
        Node newnode=new Node(x);
        if(root==null)
        {
            root=newnode;
        }
        else if(x<root.data)
            root.left=insert(x,root.left);
        else
            root.right=insert(x,root.right);
        return root;
    }
    public void display(Node root)
    {
        if(root!=null)
        {
            display(root.left);
            System.out.print(root.data+" ");
            display(root.right);
        }
    }
    public int search(int x ,Node p)
    {
        while(p!=null)
        {
            if(x==p.data)
                return 1;
            else if(x<p.data)
                p=p.left;
            else
                p=p.right;
        }
        return 0;
    }
    public Node delete(int x,Node root)
    {
        Node p;
        Node parent=root;
        Node inorderSucc;
        if(root==null)
        {
            System.out.println("The tree is empty.");
        }
        p=root;
        while(p!=null && p.data!=x)
        {
            parent=p;
            if(x<p.data)
                p=p.left;
            else
                p=p.right;
        }
        if(p.left!=null&& p.right!=null)
        {
            parent=p;
            inorderSucc=p.right;
            while(inorderSucc.left!=null)
            {
                parent=inorderSucc;
                inorderSucc=inorderSucc.left;
            }
            p.data=inorderSucc.data;
            p=inorderSucc;
        }
        if(p.left==null && p.right==null)
        {
            if(parent.left==p)
                parent.left=null;
            else
                parent.right=null;
        }
        if(p.left==null && p.right==null)
        {
            if(parent.right==p)
                parent.right=p.right;
            else
                parent.left=p.right;
        }
        if(p.left!=null && p.right==null)
        {
            if(parent.right==p)
                parent.right=p.left;
            else
                parent.left=p.left;
        }
        return p;
    }
}

class bstn
{
public static void main(String arg[]) throws IOException
{
        DataInputStream x=new DataInputStream(System.in);
        int arr[]=new int[20];
        tree t1=new tree();
        Node root=null;
        System.out.print("Enter the number of elements:");
        int n=Integer.parseInt(x.readLine());
        System.out.println("\nEnter elements:");
        for(int i=0;i<n;i++)
        {
            root=t1.insert(Integer.parseInt(x.readLine()),root);
       
        }
        int flag=1;
        t1.display(root);
        do{
            System.out.println("\n1.Insert \n\t2.Search an element\n\t\t3.delete an element\n\t\t\t4.Exit");
            switch(Integer.parseInt(x.readLine()))
            {
            case 1:
                System.out.println("\nEnter the element:");
                t1.insert(Integer.parseInt(x.readLine()),root);
                t1.display(root);
                break;
            case 2:
                System.out.println("\nEnter the element to search:");
                int res=t1.search(Integer.parseInt(x.readLine()),root);
                if(res==1)
                    System.out.println("FOUND");
                else
                    System.out.println("Not found");
                break;
            case 3:
                System.out.println("\nEnter number");
                t1.delete(Integer.parseInt(x.readLine()),root);
                t1.display(root);
                break;
            case 4:
                flag=0;
                break;
        }
    }while(flag==1);
}
}
 

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

Enter elements:
5
4
3
2
1
1 2 3 4 5 
1.Insert 
    2.Search an element
        3.delete an element
            4.Exit
1

Enter the element:
6
1 2 3 4 5 6 
1.Insert 
    2.Search an element
        3.delete an element
            4.Exit
2

Enter the element to search:
4
FOUND

1.Insert 
    2.Search an element
        3.delete an element
            4.Exit
3

Enter number
3
1 2 4 5 6 
1.Insert 
    2.Search an element
        3.delete an element
            4.Exit
4
nn@linuxmint ~ $

Hashing - Chaining - Datastrucutre - JAVA

Program:
import java.io.*;
class Node
{
    int data;
    Node next;
    public Node(int x)
    {
        data=x;
        next=null;
    }
    public void display()
    {
        System.out.print(" "+data);
    }
}
class list
{
    Node first;
    public list()
    {
        first=null;
    }
    public void deletekey(int key)
    {
        Node current=first;
        Node prev=null;
        while(current.data!=key)
        {
            if(current.next==null)
            {
                System.out.println("\nNot found...");   
                return;
            }
            else
            {
                prev=current;
                current=current.next;
            }
        }
            if(current==first)
                first=first.next;
            else
            prev.next=current.next;
    }
    public void insertlast(int x)
    {
        Node newnode=new Node(x);
        if(first==null)
        {
            first=newnode;
        }
        else
        {
            Node current=first;
            while(current.next!=null)
                current=current.next;
            current.next=newnode;
        }
    }
    public boolean search(int key)
    {
        Node current=first;
        Node prev=null;
        while(current.data!=key)
        {
            if(current.next==null)
                return false;
            else
                current=current.next;
        }
            return true;
    }
    public void display()
    {
        System.out.println("\n\n");
        Node current=first;
        while(current!=null)
        {
            current.display();
            current=current.next;
        }
    }
}
class hashtable
{
    int tablesize;
    list[] H;
    public hashtable(int size)
    {
        tablesize=size;
        H= new list[tablesize];
        for(int i=0;i<tablesize;i++)
            H[i]=new list();
    }
    public int hashfunc(int key)
    {
        return key%tablesize;
    }
    public void insert(int key)
    {
        int hashval=hashfunc(key);
        H[hashval].insertlast(key);
    }
    public void delete(int key)
    {
        int hashval=hashfunc(key);
        H[hashval].deletekey(key);
    }
    public void display()
    {
        for(int i=0;i<tablesize;i++)
        {
            H[i].display();
            System.out.print("\n");
        }
    }
    public boolean search(int key)
    {
        int hashval=hashfunc(key);

        if(H[hashval].search(key))
            return true;
        else
            return false;
    }
}
class chain
{
public static void main(String arg[])throws IOException
{
    DataInputStream x= new DataInputStream(System.in);
    System.out.println("Enter tablesize:");
    int size=Integer.parseInt(x.readLine());
    hashtable t=new hashtable(size);
    System.out.println("Enter number of terms to insert:");
    int num= Integer.parseInt(x.readLine());
    System.out.println("Enter elements:");
    for(int i=0;i<num;i++)
        t.insert(Integer.parseInt(x.readLine()));
    t.display();
    System.out.println("\nEnter number to delete:");
    t.delete(Integer.parseInt(x.readLine()));
    t.display();
    System.out.println("\nEnter number to search:");
    if(t.search(Integer.parseInt(x.readLine())))
        System.out.println("Present..");
    else
        System.out.println("Not present...");
}
}

Output:
nn@linuxmint ~ $ javac chain.java
Note: chain.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
nn@linuxmint ~ $ java chain
Enter tablesize:
10
Enter number of terms to insert:
12
Enter elements:
1
5
8
7
4
2
9
15
33
47
66
14







 1



 2



 33



 4 14



 5 15



 66



 7 47



 8



 9

Enter number to delete:
15







 1



 2



 33



 4 14



 5



 66



 7 47



 8



 9

Enter number to search:
5
Present..
nn@linuxmint ~ $

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...