Lab Manual Shaheer
Lab Manual Shaheer
Lab Manual Shaheer
LAB MANNUAL
DATA
SRRUCTURE AND
ALGORITHM
NAME:
Muhammad Shaheer
STUDENT ID:
CSC-21F-081
SECTION:
3B
COURSE SUPERVISOR:
SYEDA NAZIA ASHRAF
DATA STRUCTURE & ALGORITM LAB MANNUAL MAA’M SYEDA NAZIA ASHRAF
Largest Element in Array Find the largest element in array using the
Objective above two algorithms.
To find the largest element in an
Array.
PROGRAM 1
Algorithm import java.util.Scanner;
A nonempty array DATA with N numerical
values is given. This algorithm finds the public class largestEIN {
location LOC and value MAX of the largest public static void main(String[] args) {
element of DATA. The variable K is used as
a counter. int[] array = new int[100];
int NUM , LOC = 0;
Step 1: [Initialize] set K: =1, LOC: =1 and Scanner scan = new Scanner
MAX: =DATA [1] (System.in);
Step 2: [Increment counter] set K: =K+1. System.out.println("ENTER
Step 3: [Test counter] if K>N, then THE NUMBERS OF ELEMENTS IN
Write LOC, MAX, and Exit. ARRAY");
[End of If structure] NUM = scan.nextInt();
Step 4: [Compute and update] if System.out.println("ENTER
MAX<DATA [K], then
" + NUM + " INTEGERS");
Set LOC: =K and MAX: =DATA
for(int i=0; i < NUM; i++)
[K].
[End of If structure] {
Step 5: [Repeat loop] Go to step 2 array[i] =
[GOTO - To link one line with another.] scan.nextInt();
}
OUTPUT int MAX=array[0];
int MIN=array[0];
for(int i=0;i<NUM;i++ )
{
if (MAX < array[i]) MAX =
array[i];
else if(MIN > array[i]) MIN =
array[i];
}
System.out.println("LARGEST
ELEMENT IS :" + MAX);
System.out.println("SMALLEST
ELEMENT IS :" + MIN);
}
}
DATA STRUCTURE & ALGORITM LAB MANNUAL MAA’M SYEDA NAZIA ASHRAF
PROGRAM 1
import java.util.Scanner;
public class BinaryS {
public static void main(String[] args) {
L
AB No: 2(B)
Objective int[] a = new int[100];
int i, NUM;
To find the element in an Array using
Binary Search Scanner scan = new
Scanner(System.in);
Algorithm (Binary Search)
System.out.println("ENTER THE
Here A is a sorted Linear Array with N NUMBERS OF ELEMENTS IN ARRAY");
elements and SKEY is a given item of
NUM = scan.nextInt();
information to search. The variables
START, END and MID denote, System.out.println("ENTER ARRAY
respectively, the beginning, end and middle ELEMENTS:");
locations of a segments of element of A.
This algorithm finds the location LOC of for (i = 0; i < NUM; ++i) {
SKEY in A or sets LOC= NULL.
a[i] = scan.nextInt();
BinarySearch (A, SKEY) }
1. [Initialize segment variables.] System.out.print("ENTER ELEMENT
Set START:=0, END:=N-1 and FOR SEARCHING:");
MID=INT((START+END)/2).
NUM = scan.nextInt();
DATA STRUCTURE & ALGORITM LAB MANNUAL MAA’M SYEDA NAZIA ASHRAF
LAB TASK
Perform Bubble Sort with help of array.
PROGRAM 1
import java.util.Scanner;
OUTPUT
public static void main(String[] args) {
int[] array = new int[100];
int n, position;
DATA STRUCTURE & ALGORITM LAB MANNUAL MAA’M SYEDA NAZIA ASHRAF
position = scan.nextInt();
for (int i = n - 1; i >= position; i--) {
array[i + 1] = array[i];
}
System.out.println("WHAT
ELEMENT YOU WANT TO INSERT");
number = scan.nextInt();
n = n + 1;
array[position] = number;
System.out.println("AFTER
INSERTING THE ELEMENT");
for (int i = 0; i < n; i++) {
System.out.println("a[" + i + "] = " +
PROGRAM 2 array[i]);
import java.util.Scanner;
}
public class InsertionIArray {
}
public static void main(String[] args) {
}
int[] array = new int[100];
int n, position, number;
OUTPUT
PROGRAM :
int[] array = new int[100];
int n=array.length ,element,index=0;
Scanner scan = new Scanner (System.in);
System.out.println("ENTER NUMBER OF
ELEMENTS IN ARRAY");
n = scan.nextInt();
System.out.println("ENTER " + n + " INTEGER");
for( int i=0; i < n; i++) {
array[i] = scan.nextInt();
} for(int i = 1; i < n; i++) {
element = array[i];
if(element < array[i - 1]) {
for(int j = 0; j <= i; j++) {
if(element < array[j]) {
index = j; LAB 6
for(int k = i; k > j; k--) {
array[k] = array[k - 1]; Objective
} break; Perform Matrix Addition, Multiplication
operations.
DATA STRUCTURE & ALGORITM LAB MANNUAL MAA’M SYEDA NAZIA ASHRAF
System.out.println("MULTIPLICATION
OF TWO MATRICS IS : ");
for (int[] row : result) {
for (int column : row) {
System.out.print(column + "
"); PROGRAM 2 (MATRIX ADDITION)
} import java.util.Scanner;
DATA STRUCTURE & ALGORITM LAB MANNUAL MAA’M SYEDA NAZIA ASHRAF
LIST. The variable PTR points to the node 2. Repeat Step 3 while PTR≠NULL:
currently being processed. 3. If ITEM < INFO[PTR], then:
Set PTR:=LINK[PTR].
1. Set PTR:=START.[Initializes pointer [PTR now points to the next node]
PTR.] Else If ITEM=INFO[PTR],then
2. Repeat step 3 and 4 while Set LOC:=PTR, and Exit.
PTR≠NULL. [Search is successful.]
3. Apply PROCESS to Else:
INFO[PTR]. LOC:=NULL , and Exit.
4. Set PTR=LINK[PTR]. [PTR [ITEM now exceeds INFO[PTR].]
now points to the next node.] [End of If structure.]
[End of Step 2 loop.] [End of Step 2 loop.]
5. Exit 4. Set LOC:=NULL.
5. Exit.
Algorithm: SEARCH (INFO,
LINK,START, ITEM, LOC)
Assignment:
LIST is a linked list in the memory. This Write a program to create, display and
algorithm finds the location LOC of the search an element in a Single Link List.
node where ITEM first appear in LIST, or
PROGRAM 1
sets LOC=NULL.
public class linklistSD {
public static void
1. Set PTR:=START.
main(String[] args) {
2. Repeat Step 3 while PTR≠NULL:
// TODO code
3. If ITEM = INFO[PTR], then:
application logic here
Set LOC:=PTR, and Exit.
LinkList sList = new
[Search is successful.]
LinkList();
Else:
sList.addNode(1);
Set PTR:=LINK[PTR].
sList.addNode(2);
[PTR now points to the next node]
sList.addNode(3);
[End of If structure.]
sList.addNode(4);
[End of Step 2 loop.]
sList.display();
4. Set LOC:=NULL.
}
[Search is unsuccessful.]
}
5. Exit.
class LinkList {
Algorithm: SRCHSL (INFO,
LINK,START, ITEM, LOC)
class Node {
LIST is sorted list in memory. This
algorithm finds the location LOC of the
int data;
node where ITEM first appear in LIST, or
Node next;
sets LOC=NULL.
public Node(int data) {
this.data = data;
1. Set PTR:=START.
this.next = null;
}
DATA STRUCTURE & ALGORITM LAB MANNUAL MAA’M SYEDA NAZIA ASHRAF
}
public Node head = null;
public Node tail = null;
System.out.println("List is sList.addNode(2);
empty");
sList.addNode(3);
return;
} sList.addNode(4);
System.out.println("Nodes sList.searchNode(2);
of singly linked list: ");
sList.searchNode(7);
while (current != null) {
}
System.out.print(current.dat
a + " -> "); }
current =
current.next;
} class linkSearch {
System.out.println();
}
}
class Node {
System.out.print(current.dat
PROGRAM 1
a + "-> ");
a) Insert node at Start/First Position in
current =
Singly Linked List current.next;
public class InsertIlink { }
class Node { System.out.println();
}
int data;
Node next; public static void
main(String[] args) {
public Node(int data) { // TODO code
this.data = data; application logic here
this.next = null; InsertIlink sList = new
} InsertIlink();
} sList.addAtStart(1);
public Node head = null; sList.display();
public Node tail = null; sList.addAtStart(2);
sList.display();
public void addAtStart(int sList.addAtStart(3);
data) { sList.display();
Node newNode = new sList.addAtStart(4);
Node(data); sList.display();
if (head == null) {
head = newNode; }
tail = newNode; }
} else {
Node temp = head;
head = newNode;
head.next = temp;
DATA STRUCTURE & ALGORITM LAB MANNUAL MAA’M SYEDA NAZIA ASHRAF
}
size++;
}
public void addInMid(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = newNode;
C) Insert Node at Middle Position in Singly
Linked List } else {
Node temp, current;
public class insertinmiddle { int count = (size % 2 == 0) ? (size / 2) :
class Node { ((size + 1) / 2);
} }
} current.next = newNode;
} else { return;
tail.next = newNode; }
PROGRAM 1
import java.util.Scanner;
import java.util.Stack;
LAB 9 /**
Objective *
Evaluate Postfix Expression
* @author SHY
Algorithm of Evaluation of Postfix
Expression */
public class Postfixexp {
P postfix expression
1. Add a right parenthesis “)” at the
end of P. [This act as sentinel.]
2. Scan P public static void main(String[] args) {
from left // TODO code application logic here
to right
and
repeat
steps3 Scanner scan = new
and 4 Scanner(System.in);
for each
String EXPRESSION;
element
of P System.out.println("ENTER POSTFIX
until the EXPRESSION :");
sentinel
“)” is EXPRESSION = scan.next();
encoun
System.out.print("ENTER
red,
3. If an operand is encountered, EVALUATION OF THE POSTFIX
put it on stack EXPRESSION " + (EXPRESSION) + "
4. If an operator is encountered, IS :");
then:
evaluatePostfix(EXPRESSION);
(a) Remove the top two elements of stack,
where A is the top element and B is the }
next-to-top element.
(b) Evaluate B [operator] A.
(c) Place the result of static boolean isOperator(char ch) {
(b) on stack.
[End of If structure]
DATA STRUCTURE & ALGORITM LAB MANNUAL MAA’M SYEDA NAZIA ASHRAF
Deletion in Queue:
Algorithm: DEQUEUE(QUEUE,
MAXSIZE, FRONT, REAR,COUNT,
ITEM)
This procedure deletes an element from a
queue and assigns it to the
variable ITEM.
1. [QUEUE already empty?]
If COUNT= 0, then: Write: UNDERFLOW,
and Return.
2. Set ITEM = QUEUE[FRONT].
DATA STRUCTURE & ALGORITM LAB MANNUAL MAA’M SYEDA NAZIA ASHRAF
front = 0; int i;
} if (isEmpty()) {
rear++; System.out.println("Empty Queue");
items[rear] = element; } else {
System.out.println("Inserted " + System.out.println("\nFront index-> " +
element); front);
} System.out.println("Items -> ");
} for (i = front; i <= rear; i++) {
System.out.print(items[i] + " ");
int deQueue() { }
int element; System.out.println("\nRear index-> " +
rear);
if (isEmpty()) {
}
System.out.println("Queue is empty");
}
return (-1);
}
} else {
element = items[front];
if (front >= rear) { OUTPUT
front = -1;
rear = -1;
} /* Q has only one element, so we reset
the queue after deleting it. */ else {
front++;
}
System.out.println("Deleted -> " +
element);
return (element);
}
}
void display() {
LAB 12
/* Function to display elements of Queue */
DATA STRUCTURE & ALGORITM LAB MANNUAL MAA’M SYEDA NAZIA ASHRAF