DATA STRUCTURES & ALGORITHMS LAB 1. Quadratic Equation
Recommend Documents
that are based on the data structures of the program. ... This laboratory guide is
intended to facilitate understanding of the widely used data structures such as ...
CS1020: Data Structures and Algorithms I. Tutorial 1: Java and Simple OOP. (
Week starting 25 August 2013). 1. [Basic Concept and OOP] a) Match the terms in
...
Oct 3, 2007 ... algorithmic toolbox: structures that allow efficient organization and retrieval ..... for
i := 0 to n − 1 do add ai, bi, and c to form si and a new carry c.
21 Jan 2009 ... About the Course: Reading. Data Structures and Algorithm Analysis in Java,. 2nd
Edition by Mark Allen Weiss. ISBN-10: 0321370139 ...
Dec 22, 2013 - 22. Programming language. 27. Algorithm. 40. Deterministic algorithm. 65. Data structure. 68. List (abstract data type). 70. Array data structure.
C.2 Some problems are recursive in nature . . . . . . . . . . . . . . . 95 ... with chunks of
text describing how the data structure or algorithm in question works and various
... implementing your own version of the algorithms in question. Through
Dec 12, 2003 ... Note that the inner for loop of the above sorting procedure is simply the .... A
linked list is a data structure in which the objects are arranged in a ...
Stein. Introduction to Algorithms. McGraw-Hill, 2002. [GT] Michael T. Goodrich
and Roberto Tamassia. Algorithm Design –. Foundations, Analysis, and Internet ...
Dec 27, 2008 - String data is frequently obtained from user-input to a program. As such, it is ... Terminated by a newline sequence, for example in Windows INI files. Non-text .... from the end of the needle, so it can usually jump ahead a whole.
Oct 3, 2007 ... This book is a concise introduction to this basic toolbox intended for students ...
plementations in real programming languages such as C++ or Java. ...... the
processing unit, the sum ai + bi + c has to be formed, the digit si of .
A cornerstone of this theory of data structures is the distinction between ... on
several examples of problems in which recursion allows for a most natural ...
was introduced by Karl Weierstrass in 1841. ..... It is a straightforward deduction from Wilson's theorem that[30] ...... logarithms-tutorial) ...... Not until 1882, with Ferdinand von Lindemann's proof of the transcendence of Ï, was squaring the ci
Provide examples of pointer-related C code ... Common data structures and
algorithms serve as “high ... Free: Free nodes while traversing; free Table
structure.
The algorithms and data structures selected are basic ones, constituting the ...
implementation of data structures and algorithms, as well as their issues are.
Dec 22, 2013 - See http://code.pediapress.com/ for more information. PDF generated ...... [4] http://dbs.uni-leipzig.de/skripte/ADS1/PDF4/kap4.pdf. [5] Y. Han.
CSCL228-10: Data Structures and Algorithms Lab. Spring 2014. Instructor: Dr.
Xuejun Liang. Office: NSB 307. Office Hours: MWF 8:30-9:50AM and 12:30-2: ...
efficient algorithms and appropriate data structures are used. This course will
help the students to develop efficient data structures and algorithms in a
systematic ...
CA213 Data Structures & Algorithms. Lab Exercise, Week 5. Monica Ward. 1.
Introduction. These lab exercises are to give you a chance to practise some of the
...
Data Structures and Algorithms. ___ Lab Session 01. NED University of
Engineering & Technology – Department of Computer & Information Systems ...
ECE242 Data Structures and Algorithms ... to write any Java code, however, you
need to show each step in the sorting process to receive full credit. Solution:.
with chunks of text describing how the data structure or algorithm in question ...
standing which data structure or algorithm to use for a certain scenario. In all.
Dec 22, 2013 - One example application of recursion is in parsers for programming ...... is given a choice to use it to free her father as well as the builder of.
Dec 22, 2013 - PDF generated using the open source mwlib toolkit. ...... AVL trees and red-black trees are both forms of self-balancing binary .... Madru, Justin (18 August 2009). "Binary ... "Python Search Tree Empirical Performance Comparison" (htt
Infix to Postfix Conversion. • We use a stack. • When an operand is read, output it.
• When an operator is read. – Pop until the top of the stack has an element of ...
DATA STRUCTURES & ALGORITHMS LAB 1. Quadratic Equation
DATA STRUCTURES & ALGORITHMS LAB. 1. Quadratic Equation. AIM: Write a
Java program that prints all real solutions to the quadratic equation . Read in a ...
Program No:
Page No: 1
DATA STRUCTURES & ALGORITHMS LAB 1. Quadratic Equation AIM: Write a Java program that prints all real solutions to the quadratic equation . Read in a, b, c and use the quadratic formula. If the discriminant (
) is negative; display a message stating that there are no
real solutions. PROGRAM: import java.lang.Math; import java.util.Scanner; class QuadraticEquation { double a, b, c; // Coefficients QuadraticEquation( double a, double b, double c) { this.a = a; this.b = b; this.c = c; } public void roots() { if( a == 0.0 ) { System.out.println("\n One root = " + (-c/b)); return; } double d = b*b - 4.0*a*c; if(d < 0) // Roots are imaginary { System.out.println("\n There are no real solutions."); return; } if(d == 0) // Roots are equal { G NATA RAJU
M.Tech (CNIS) – I Sem
Roll No: 11031D6427
Program No:
Page No: 2
DATA STRUCTURES & ALGORITHMS LAB System.out.println("\n Two roots are equal: " + (-b /(2.0*a))); return; } // Roots are real double x1 = (-b + Math.sqrt(d))/(2.0*a); double x2 = (-b - Math.sqrt(d))/(2.0*a); System.out.println("\n Roots are: " + x1 + ", " + x2); } } //////////////////// Quadratic Equation Program //////////////////// public class QuadEquPgm { public static void main(String[] args) { Scanner scr = new Scanner(System.in); System.out.print(" Enter the value of coefficient a : "); double a = scr.nextDouble(); System.out.print(" Enter the value of coefficient b : "); double b = scr.nextDouble(); System.out.print(" Enter the value of coefficient c : "); double c = scr.nextDouble(); System.out.print(" Entered Quadratic Equation is : "+a+"x^2+"+b+"x+"+c); QuadraticEquation qe = new QuadraticEquation(a, b, c); qe.roots(); } } Output: (For different values of a, b, and c) Enter the value of coefficient a : 0 Enter the value of coefficient b : 4 Enter the value of coefficient c : 1 Entered Quadratic Equation is : 0.0x^2+4.0x+1.0 One root = -0.25
G NATA RAJU
M.Tech (CNIS) – I Sem
Roll No: 11031D6427
Program No:
Page No: 3
DATA STRUCTURES & ALGORITHMS LAB Enter the value of coefficient a : 1 Enter the value of coefficient b : 4 Enter the value of coefficient c : 4 Entered Quadratic Equation is : 1.0x^2+4.0x+4.0 Two roots are equal: -2.0 Enter the value of coefficient a : 1 Enter the value of coefficient b : 4 Enter the value of coefficient c : 3 Entered Quadratic Equation is : 1.0x^2+4.0x+3.0 Roots are: -1.0, -3.0
G NATA RAJU
M.Tech (CNIS) – I Sem
Roll No: 11031D6427
Program No:
Page No: 4
DATA STRUCTURES & ALGORITHMS LAB 2. Fibonacci Sequence AIM: Write a Java program that uses both recursive and non-recursive functions to print the nth value in the Fibonacci sequence. PROGRAM: import java.io.*; class Fibonacci { public int fibonacciSeq(int n) // Iterative method { int term1 = 0, term2 = 1, nextTerm = 0; if( n == 0 || n == 1) return n; int count = 2; while( count 22 -> 33 -> 44 -> null Linked List: 55 -> 11 -> 22 -> 33 -> 44 -> null Linked List: 55 -> 11 -> 22 -> 33 -> 66 -> 44 -> null deleteFirst(): 55 Linked List: 11 -> 22 -> 33 -> 66 -> 44 -> null deleteAfter(22): 33 Linked List: 11 -> 22 -> 66 -> 44 -> null Size(): 4 G NATA RAJU
M.Tech (CNIS) – I Sem
Roll No: 11031D6427
Program No:
Page No: 38
DATA STRUCTURES & ALGORITHMS LAB 17. Stacks and Queues AIM: Write Java programs to implement the following using an array. (a) Stack ADT (b) Queue ADT PROGRAM: (a)
Stack ADT
interface Stack { public void push(Object ob); public Object pop(); public Object peek(); public boolean isEmpty(); public int size(); } class ArrayStack implements Stack { private Object a[]; private int top; // stack top public ArrayStack(int n) { // constructor a = new Object[n]; // create stack array top = -1; }// no items in the stack public void push(Object item) { // add an item on top of stack if(top == a.length-1) { System.out.println("Stack is full"); return;
}
top++; // increment top a[top] = item; } // insert an item public Object pop() { // remove an item from top of stack if( isEmpty() ) { System.out.println("Stack is empty"); return null; } Object item = a[top]; // access top item top--; // decrement top return item; } public Object peek() {// get top item of stack if( isEmpty() ) return null; G NATA RAJU
M.Tech (CNIS) – I Sem
Roll No: 11031D6427
Program No:
Page No: 39
DATA STRUCTURES & ALGORITHMS LAB return a[top]; } public boolean isEmpty() {// true if stack is empty return (top == -1); } public int size() {// returns number of items in the stack return top+1; } } public class StackADT { public static void main(String[] args) { ArrayStack stk = new ArrayStack(4); // create stack of size 4 Object item; stk.push('A'); // push 3 items onto stack stk.push('B'); stk.push('C'); System.out.println("Size(): "+ stk.size()); item = stk.pop(); // delete item System.out.println(item + " is deleted"); stk.push('D'); // add three more items to the stack stk.push('E'); stk.push('F'); System.out.println(stk.pop() + " is deleted"); stk.push('G'); // push one item item = stk.peek(); // get top item from the stack System.out.println(item + " is on top of stack"); } } OUTPUT: Size(): 3 C is deleted Stack is full E is deleted G is on top of stack
G NATA RAJU
M.Tech (CNIS) – I Sem
Roll No: 11031D6427
Program No:
Page No: 40
DATA STRUCTURES & ALGORITHMS LAB (b)
Queue ADT
interface Queue { public void insert(Object ob); public Object remove(); public Object peek(); public boolean isEmpty(); public int size(); } class ArrayQueue implements Queue { private int maxSize; // maximum queue size private Object[] que; // que is an array private int front; private int rear; private int count; // count of items in queue (queue size) public ArrayQueue(int s) { // constructor maxSize = s; que = new Object[maxSize]; front = rear = -1; count = 0; } public void insert(Object item) { // add item at rear of queue if( count == maxSize ) { System.out.println("Queue is Full"); return; } if(rear == maxSize-1 || rear == -1) { que[0] = item; rear = 0; if( front == -1) front = 0; } else que[++rear] = item; count++; } // update queue size public Object remove() { // delete item from front of queue if( isEmpty() ) { System.out.println("Queue is Empty"); return 0; } G NATA RAJU
M.Tech (CNIS) – I Sem
Roll No: 11031D6427
Program No:
Page No: 41
DATA STRUCTURES & ALGORITHMS LAB Object tmp = que[front]; // save item to be deleted que[front] = null; // make deleted item‟s cell empty if( front == rear ) rear = front = -1; else if( front == maxSize-1 ) front = 0; else front++; count--; // less one item from the queue size return tmp; } public Object peek() { // peek at front of the queue return que[front]; } public boolean isEmpty() { // true if the queue is empty return (count == 0); } public int size() { // current number of items in the queue return count; } public void displayAll() { System.out.print("Queue: "); for( int i = 0; i < maxSize; i++ ) System.out.print( que[i] + " "); System.out.println(); } } public class QueueADT { public static void main(String[] args) { /* Queue holds a max of 5 items */ ArrayQueue q = new ArrayQueue(5); Object item; q.insert('A'); q.insert('B'); q.insert('C'); q.displayAll(); item = q.remove(); // delete item System.out.println(item + " is deleted"); item = q.remove(); System.out.println(item + " is deleted"); q.displayAll(); G NATA RAJU
M.Tech (CNIS) – I Sem
Roll No: 11031D6427
Program No:
Page No: 42
DATA STRUCTURES & ALGORITHMS LAB q.insert('D'); // insert 3 more items q.insert('E'); q.insert('F'); q.displayAll(); item = q.remove(); System.out.println(item + " is deleted"); q.displayAll(); System.out.println("Peek(): " + q.peek()); q.insert('G'); q.displayAll(); System.out.println("Queue size: " + q.size()); } } OUTPUT: Queue: A B C null null A is deleted B is deleted Queue: null null C null null Queue: F null C D E C is deleted Queue: F null null D E Peek(): D Queue: F G null D E Queue size: 4
G NATA RAJU
M.Tech (CNIS) – I Sem
Roll No: 11031D6427
Program No:
Page No: 43
DATA STRUCTURES & ALGORITHMS LAB 18. Infix to Postfix Conversion AIM: Write a java program that reads an infix expression, converts the expression to postfix form and then evaluates the postfix expression (use stack ADT). PROGRAM: (a) InfixToPostfixPgm class InfixToPostfix { java.util.Stack stk = new java.util.Stack(); public String toPostfix(String infix) { infix = "(" + infix + ")"; // enclose infix expr within parentheses String postfix = ""; /* scan the infix char-by-char until end of string is reached */ for( int i=0; i= precedence(ch) ) { stk.push(item); stk.push(ch); } else { postfix = postfix + item; stk.push(ch); } } else { stk.push(item); stk.push(ch); } } // end of if(isOperator(ch)) if( ch == ')' ) { G NATA RAJU
M.Tech (CNIS) – I Sem
Roll No: 11031D6427
Program No:
Page No: 44
DATA STRUCTURES & ALGORITHMS LAB item = stk.pop(); while( item != '(' ) { postfix = postfix + item; item = stk.pop(); } } } // end of for-loop return postfix; } // end of toPostfix() method public boolean isOperand(char c) { return(c >= 'A' && c