Linearsearch - LISP

Program:
(defun linsearch(n)
    (format t "~&<<LINEARSEARCH >>")
    (readn n)
    (format t "~&Enter the number to be searched:")
    (setf num (read))
    (setf flag 1)
    (do ((x 0 (+ x 1))) ((= x n))
        (if(= num (aref arr x))
            (setf flag 2)
        )
    )
    (if (= flag 2)
        (format t "Number present...")
        (format t "Number not present...")
    )
)
(defun readn(n)
    (setf arr (make-array n))
    (format t "~&Enter the ~d numbers:" n )
    (dotimes (x n t)
        (setf (aref arr x) (read))
    )
)

Output:
Break 4 [5]> (load 'ls.lsp)
;;  Loading file ls.lsp ...
;;  Loaded file ls.lsp
T
Break 4 [5]> (linsearch 5)
<<LINEARSEARCH >>
Enter the 5 numbers:1
2
3
4
5
Enter the number to be searched:4
Number present...
NIL
Break 4 [5]>

Towers of Hanoi - Programming Environment Lab - LISP

Program:
(defun hanoi(n)
    (dohanoi n 3 1 2)
)

(defun dohanoi(ndisks destination source temp)
    (cond
        ((> ndisks 0) (dohanoi (- ndisks 1) temp source destination)
                (format t "Move the top disk from peg~d to peg~d ~&" source destination)
                (dohanoi (- ndisks 1) destination temp source)
        )
    )
)

Output:
Break 4 [5]> (load 'h.lsp)
;; Loading file h.lsp ...
;; Loaded file h.lsp
T
Break 4 [5]> (hanoi 3)
Move the top disk from peg1 to peg3
Move the top disk from peg1 to peg2
Move the top disk from peg3 to peg2
Move the top disk from peg1 to peg3
Move the top disk from peg2 to peg1
Move the top disk from peg2 to peg3
Move the top disk from peg1 to peg3
NIL
Break 4 [5]>

Generation of Fibonacci Series - Recursion & Iteration -LISP

Program:
;;FIBONACCI SERIES
(defun fib(n)
    (setf a 0 b 1)
    (format t "~&<<<Generation of Fibonacci series with ~D terms>>>~&1.Iterative method~&2.Recursive method~&Enter your choice:" n)
    (setf x (read))
    (cond
        ((= n 1) (print a))
        ((= n 2) (print a)(print b))
        ((> n 2) (if (= x 1)
                (fib1 n))
        (if (= x 2)
             (do ((i 0 (+ i 1))) ((= i n))
                (print (fib2 i)))))
    )
)
(defun fib1(n)
                ;Iterative method
    (print a)(print b)
    (do((i 2 (+ i 1))) ((= i n))
        (setf c (+ a b))
        (print c)
        (setf a b b c))
)
(defun fib2(n)
    (cond
        ((= n 0) 0)
        ((= n 1) 1)
        ((> n 1)(+ (fib2 (- n 1)) (fib2 (- n 2))))
                ;Recursive method
    )
)

Output:
Break 2 [3]> (load 'f.lsp)
;;  Loading file f.lsp ...
;;  Loaded file f.lsp
T
Break 2 [3]> (fib 5)
<<<Generation of Fibonacci series with 5 terms>>>
1.Iterative method
2.Recursive method
Enter your choice:1

0
1
1
2
3
NIL
Break 2 [3]> (fib 5)
<<<Generation of Fibonacci series with 5 terms>>>
1.Iterative method
2.Recursive method
Enter your choice:2

0
1
1
2
3
NIL
Break 2 [3]>

Factorial of a number - LISP

Program:
;;FACTORIAL OF A NUMBER USING RECURSION AND ITERATION
(defun factorial(n)
    (format t "~&<<<Finding factorial of ~D>>>~&1.Iterative method~&2.Recursive method~&Enter your choice:" n)
    (setf x (read))
    (if (= x 1) (format t "factorial = ~D (using iteration)" (fact1 n)))
    (if (= x 2) (format t "factorial = ~D (using recursion)" (fact2 n)))
)
(defun fact1(n) ;;function for iterative method
    (setf f 1)
    (do ((i n (- i 1))) ((= i 1))
        (setf f (* f i))
    )
f)
(defun fact2(n) ;;function for recursive method
    (if (= n 0) 1
        (* n (fact2(- n 1)))
    )
)

Output:
Break 1 [2]> (load 'fact.lsp)
;; Loading file fact.lsp ...
;; Loaded file fact.lsp
T
Break 1 [2]> (factorial 5)
<<<Finding factorial of 5>>>
1.Iterative method
2.Recursive method
Enter your choice:1
factorial = 120 (using iteration)
NIL
Break 1 [2]> (factorial 5)
<<<Finding factorial of 5>>>
1.Iterative method
2.Recursive method
Enter your choice:2
factorial = 120 (using recursion)
NIL                   
Break 1 [2]>

Binary Search - LISP

Program:
;;BINARYSEARCH
(defun binsearch(n)
    (format t "~&<<BINARYSEARCH>>~&")
    (bsreadn n)
    (bssortn n)
    (format t "~&Enter the number to be searched:")
    (setf p (read))
    (setf flag 1)
    (setf first 0)
    (setf last (- n 1))
    (dotimes (x n t)
        (setf mid (floor (+ first last) 2))
        (if (= (aref arr mid) p)(setf flag 0))
        (cond
            ( (>(aref arr mid) p) (setf last (- mid 1)))
            ( (<(aref arr mid) p) (setf first (+ mid 1)))
        )
    )
    (if (= flag 1)
        (format t "Number Not found..."))
    (if (= flag 0)
        (format t "Number found..."))
)
(defun bsreadn(n)
    (setf arr (make-array n))
    (format t "Enter the ~d numbers:" n)
    (dotimes (x n t)
        (setf (aref arr x) (read))
    )
)
                        ;;http://www.2k8618.blogspot.com/
(defun bssortn(n)
    (do (( i 1 (+ i 1))) ((= i n))
        (do ((j 0 (+ j 1))) ((= j (- n 1)))
            (if (> (aref arr j) (aref arr (+ j 1)))
                (bswap j (+ j 1))
            )
        )
    )
    (format t "Sorting....")
    (bprintn n)
)
(defun bprintn(n)
    (dotimes(x n t)
        (print (aref arr x))
    )
)
(defun bswap(x y)
    (setf temp (aref arr x))
    (setf (aref arr x) (aref arr y))
    (setf (aref arr y) temp)
)

Output:
Break 2 [3]> (load 'bin.lsp)
;;  Loading file bin.lsp ...
;;  Loaded file bin.lsp
T
Break 2 [3]> (binsearch 5)
<<BINARYSEARCH>>
Enter the 5 numbers:5
4
3
2
1
Sorting....
1
2
3
4
5
Enter the number to be searched:3
Number found...
NIL
Break 2 [3]> (binsearch 5)
<<BINARYSEARCH>>
Enter the 5 numbers:1
4
2
3
5
Sorting....
1
2
3
4
5
Enter the number to be searched:6
Number Not found...
NIL
Break 2 [3]>

Factorial of a number - C++

Program :
#include<iostream>
using namespace std;
int fr(int n);
   
int main()
{
 int n,fact=1,c,f,k=1;
 cout<<"Enter the number:";
 cin>>n;
 while(k==1)
 {
   cout<< "MENU\n\n1.USING ITERATION\n\n2.USING RECURSION\n\n3.EXIT\n\nENTER YOUR CHOICE:";
   cin>>c;
   switch(c)
    {
 
        case 1: if(n==0)
                cout<<"Factorial of 0 is 1";
               else
                 {
                 for(int i=1;i<=n;++i)
                    fact=fact*i;
                 cout<<"\nfactorial= "<<fact<<"\n\n";
                 }
                 break;
         case 2: f=fr(n);
                 cout<<"\nfactorial="<<f<<"\n\n";
                 break;
         case 3:k=0;
                break;
                
        default:cout<<"\nInvalid choice";
                break;
      }
   }
 return 0;
  
 }
 int fr(int n)
 {
   if(n==0)
    return 1;
   else
    return (n*fr(n-1));
  }

Output:
students@cselab-desktop:~$ g++ fact.cpp
students@cselab-desktop:~$ ./a.out
Enter the number:5
MENU

1.USING ITERATION

2.USING RECURSION

3.EXIT

ENTER YOUR CHOICE:1

factorial= 120

MENU

1.USING ITERATION

2.USING RECURSION

3.EXIT

ENTER YOUR CHOICE:2

factorial=120

MENU

1.USING ITERATION

2.USING RECURSION

3.EXIT

ENTER YOUR CHOICE:3
students@cselab-desktop:~$

Operating Systems - Gary Nutt - Presentation Slides


Operating Systems
Gary Nutt

Visit SPELMAN COLLEGE website for the pdf files.







Visit GORDON COLLEGE WEBSITE website for the ppt files.



Download page for ppt.



Chap 01-Intoduction


Chap02-Using the Operating System


Chap 03-Operating System Organisatiom


Chap 04-Computer Organisation


Chap05-Device Management


Chap06-Implementing Processes Threads Resources


Chap07-Scheduling


Chap08-Basic Synchronisation Principles


Chap09-High Level Synchronisation IPC


Chap10-Deadlock


Chap11-Memory Management


Chap11-Memory Management


Chap12-Virtual Memory


Chap13-File Management


Chap14-Protection Security


Chap15-Networks


Chap16-Remote Files


Chap17-Distibuted Computing


Chap18-Distributed Programming Runtime Systems


Chap19-Design Strategies


Chap20-TheLinuxKernel


Chap21-WindowsKernel


workshop_1


workshop_2

Software Engineering (SE) - Previous Question Papers -Semester 5 - 2011 - 2K6CS506







Operating Systems (OS) - Previous Question Papers -Semester 5 - 2011 - 2K6CS505





Theoretical foundation of Computation (TFC) - Previous Question Papers -Semester 5 - 2011 - 2K6CS503









Economics & Business Management (EBM) - Previous Question Papers -Semester 5 - 2011 - 2K6CS502









Engineering Mathematics IV - Previous Question Papers -Semester 5 - 2011 - 2K6CS501



Programming Language Concepts - PLC - Previous Question Paper Fifth Semester B.Tech. Engineering Degree Examination ,January 2011 Kannur University




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 ~ $
Related Posts Plugin for WordPress, Blogger...