0% found this document useful (0 votes)
5 views

DS Using Java Lab (University Program)

Data Structure using Java lab

Uploaded by

dead wish
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
5 views

DS Using Java Lab (University Program)

Data Structure using Java lab

Uploaded by

dead wish
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 11
LIST OF EXERCISES: 1. Write @ Java programs to implement the List ADT using arrays and linked lists. 2.Write ¢ Java programs to implement the following using a singly linked list. Stack ADT (b) Queue ADT 3. Write @ java program that reads an infix expression, converts the expression to postfix form and then evaluates the postfix expression (use stack ADT). 4, Write @ Java program to implement priority queue ADT. 5.Write a Java program to perform the following operations: (a) _ Insert an element into a binary search tree. (b) Delete an element from a binary search tree. (©) Search for a key element in a binary search tree. 6. Write 2 Java program to pe-form the following operations (a) _ Insertion into an AVL-tree (b) Deletion from an AVL-tree 7.Write e Java programs for the implementation of BFS for a given graph. 8. Write a Java programs for the implementation of DFS for a given graph 9. Write a Java programs for implementing the following searching methods: (a) Linear search (b) Binary search. 10. Write a Java programs for implementing the following sorting methods (a) Bubble sort (b) Selection sort (c) _ Insertion sort (d) Radix sort. 1.Write a Java programs to implement the List ADT using arrays and linked lists. a) ADT using arrays leave two page space b) Linked List public class singlyLinkedList { //Represent a node of the singly linked list class Node{ int data; Node next; public Node(int data) { this.data = data; this.next = null; } } //Represent the head and tail of the singly linked list, public Node head = null; public Node tail = null; /faddNode() will add a new nodeto thelist public void addNode(int data) { 1/Create anew node Node newNode = new Node(data); 1/Checks if the list is empty if(head == null) { /{Mf list is empty, both head end tail will point to new node head = newNode; tail = newNode; } else { //newNode will be added after tail such that tail’s next will point to newNode tail.next = newNode; //newNode will become new tail of the list tail = newNode; } } //display() will display all the nodes present in thelist public void display) { //Node current will point to head Node current = head; if(head == null) { System out printin(’List is empty"); return; } System.out.printin(’Nodes of singly linked list: "); while(current != null) { /|Prints each node by incrementing pointer System out print(current.data +"); current = current.next, } System. out printinO; } public static void main(Stingl] args) { singlyLinkedList sList = new singlyLinkedList0; //sListaddNode(; Add nodes to the list sList.addNode(1); sList.addNode(2); sList.addNode(3), sList.addNode(4); //Displays the nodes present in the list sList.display(); } } 2.Write a Java programs to implement the following using a singly linked list. Stack ADT (b) Queue ADT a)Stack ADT import java.io.*; import java.util; class StkList { StackNode root; static class StackNode { int data; StackNode next; StackNode(int data) {this.data = data; } } public void push(int data) { StackNode newNode = new StackNode(data); if (root == null) { root = newNode; } else { StackNode temp = root; root = newNode; newNode.next = temp; } System out printin(data +" pushed to stack’); } public void pop() { int popped = Integer. MIN_VALUE; if (Foot == null) { System.out.printin('Stack is Empty"); } else { popped = root.data; root = root next; } System out printin("\n the element popped fram stack\n"+popped); } void display() { if (root == null) ‘ystem out printin(‘Stack is empty’); } StackNode tmp1=root; while(tmpt!= null) { System.out print(tmp1.data+'\n’); imp1=tmp1.next; } } class stklinked public static void main(String[] args)throws IOException BufferedReader br=new BufferedReader{new InputStreamReadeSystem.in)); StkList s = new StkList(); int ch ; do { System.out.printin(‘\n1 push’); System. out printin('2.pop"); System. out printin('3.cisplay’); System.out printin(‘enter your choice\n’); ch =Integer.parseint(br.readLine()); switch(ch) { casel int x=Integer parselnt(brreadLine(); spush(x); break; case 2: s.pop0); break case 3: s.cisplay(; break default: System.out.printin(‘invalid choice’); break; } Jwhile(ch>=1&&ch<=3); } } b)Queue ADT class QNode { int key; QNode next; // constructor to create a new linked list node public QNode(int key) { this.key = key; this.next = null; } } class Queue { QNode front, rear; public Queue() { this.front = this.rear = null; } // Method to add an key to the queue. void enqueue(int key) { // Create a new LL node QNode temp = new QNode(key); // |i queue is empty, then new node is front and rear both if (this.rear == null) { thisfront = this.rear = temp; return; } // Add the new node at the end of queue and change rear this.rearnext = temp; this.rear = temp; } // Method to remove an key from queue. void dequeue() { //|f queue is empty, return NULL. if (this front == null) return; // Store previous front and move front one node ahead QNode temp = this.front this.front = this.front.next, // Kf front becomes NULL, then change rear also as NULL if (this front == null) this.rear= null; /! Driver class public class queuelinked{ public static void main(Stringl] args) { Queue q = new Queue(); q.enqueue(10); g.enqueue(20); a.dequeue(); g.dequeue(); g.enqueue(30); a.enqueue(40); g.enqueue(50); q.dequeue(); System. out printin("Queue Front :' + q.front key); System. out printin("‘Queue Rear : "+ q.rear key); 3.Write a java program that reads an infix expression, converts the expression to postfix form and then evaluates the postfix expression (use stack ADT). import java.util. Stack, import java.io*; class Test { // utility function to return // precedence of a given operator // Higher returned value means // higher precedence static int Preo(char ch) { switch (ch) { case '+': case“! return 1; case* case'/' return 2; case" return 3; } return -1; } // The main method that converts // given infix expression // 10 postfix expression String infixToPostfix(String exp) { // initializing empty String for result String result = new Strin // initializing empty stack Stack stack = new Stacko(); for (inti = 0; i

You might also like