Uploaded by dev.sector522012

A2-22BCE9115

advertisement
Assignment 2: Linear data Structures
-22BCE9115
Dev Kumar
Question 1: Write a program to implement singly linked lists and its
operations.
Code:
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
Node head;
public void insert(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
}
public void delete(int key) {
Node temp = head;
Node prev = null;
if (temp != null && temp.data == key) {
head = temp.next;
return;
}
while (temp != null && temp.data != key) {
prev = temp;
temp = temp.next;
}
if (temp == null) {
return;
}
prev.next = temp.next;
}
public void display() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
}
public boolean search(int key) {
Node temp = head;
while (temp != null) {
if (temp.data == key) {
return true;
}
temp = temp.next;
}
return false;
}
}
class Main {
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.insert(22);
list.insert(21);
list.insert(34);
list.insert(45);
list.insert(59);
System.out.println("Original Linked List:");
list.display();
System.out.println();
list.delete(34); // Delete node with value 3
System.out.println("Linked List after deleting 3:");
list.display();
System.out.println();
int searchKey = 21;
System.out.println("Is " + searchKey + " present in the list? " +
list.search(searchKey));
}
}
Output:
Question 2: Write a Program to implement doubly linked list and its
operations.
Code:
class Node {
int data;
Node prev;
Node next;
public Node(int
this.data =
this.prev =
this.next =
}
data) {
data;
null;
null;
}
class DoublyLinkedList {
Node head;
public void insert(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
newNode.prev = temp;
}
}
public void delete(int key) {
Node temp = head;
while (temp != null && temp.data != key) {
temp = temp.next;
}
if (temp == null) {
return;
}
if (temp.prev != null) {
temp.prev.next = temp.next;
}
if (temp.next != null) {
temp.next.prev = temp.prev;
}
if (temp == head) {
head = temp.next;
}
}
public void displayForward() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
public void displayBackward() {
Node temp = head;
while (temp != null && temp.next != null) {
temp = temp.next;
}
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.prev;
}
System.out.println();
}
public boolean search(int key) {
Node temp = head;
while (temp != null) {
if (temp.data == key) {
return true;
}
temp = temp.next;
}
return false;
}
}
class Main {
public static void main(String[] args) {
DoublyLinkedList list = new DoublyLinkedList();
list.insert(1);
list.insert(2);
list.insert(3);
list.insert(4);
list.insert(5);
System.out.println("Original Doubly Linked List (Forward):");
list.displayForward();
System.out.println("Original Doubly Linked List (Backward):");
list.displayBackward();
list.delete(3); // Delete node with value 3
System.out.println("Doubly Linked List after deleting 3 (Forward):");
list.displayForward();
System.out.println("Doubly Linked List after deleting 3 (Backward):");
list.displayBackward();
int searchKey = 2;
System.out.println("Is " + searchKey + " present in the list? " +
list.search(searchKey));
}
}
Output:
Question 3: Write a Program to implement stack operations using arrays.
Code:
class Stack
private
private
private
{
int maxSize;
int top;
int[] stackArray;
public Stack(int size) {
maxSize = size;
stackArray = new int[maxSize];
top = -1;
}
public void push(int value) {
if (top < maxSize - 1) {
stackArray[++top] = value;
} else {
System.out.println("Stack overflow, cannot push element " + value);
}
}
public int pop() {
if (top >= 0) {
return stackArray[top--];
} else {
System.out.println("Stack underflow, cannot pop element");
return -1;
}
}
public int peek() {
if (top >= 0) {
return stackArray[top];
} else {
System.out.println("Stack is empty");
return -1;
}
}
public boolean isEmpty() {
return top == -1;
}
public boolean isFull() {
return top == maxSize - 1;
}
}
class Main {
public static void main(String[] args) {
Stack stack = new Stack(5);
stack.push(1);
stack.push(2);
stack.push(3);
System.out.println("Top element: " + stack.peek());
stack.pop();
System.out.println("Top element after pop: " + stack.peek());
System.out.println("Is the stack empty? " + stack.isEmpty());
System.out.println("Is the stack full? " + stack.isFull());
}
}
Output:
Question 4: Write a Program to implement queue operations using linked
list.
Code:
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
class Queue {
private Node front;
private Node rear;
public Queue() {
front = null;
rear = null;
}
public void enqueue(int value) {
Node newNode = new Node(value);
if (rear == null) {
front = newNode;
rear = newNode;
} else {
rear.next = newNode;
rear = newNode;
}
}
public int dequeue() {
if (front == null) {
System.out.println("Queue is empty");
return -1;
} else {
int value = front.data;
front = front.next;
if (front == null) {
rear = null;
}
return value;
}
}
public boolean isEmpty() {
return front == null;
}
}
class Main {
public static void main(String[] args) {
Queue queue = new Queue();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
System.out.println("Dequeued element: " + queue.dequeue());
System.out.println("Dequeued element: " + queue.dequeue());
System.out.println("Is the queue empty? " + queue.isEmpty());
}
}
Output:
Question 6: Write a program for postfix evaluation.
Code:
class Stack {
private int[] array;
private int top;
public Stack(int capacity) {
array = new int[capacity];
top = -1;
}
public void push(int value) {
if (top < array.length - 1) {
array[++top] = value;
}
}
public int pop() {
if (top >= 0) {
return array[top--];
}
return Integer.MIN_VALUE;
}
public boolean isEmpty() {
return top == -1;
}
}
class PostfixEvaluation {
public static int evaluatePostfix(String expression) {
Stack stack = new Stack(expression.length());
for (int i = 0; i < expression.length(); i++) {
char ch = expression.charAt(i);
if (Character.isDigit(ch)) {
stack.push(ch - '0');
} else {
int operand2 = stack.pop();
int operand1 = stack.pop();
switch (ch) {
case '+':
stack.push(operand1 + operand2);
break;
case '-':
stack.push(operand1 - operand2);
break;
case '*':
stack.push(operand1 * operand2);
break;
case '/':
stack.push(operand1 / operand2);
break;
}
}
}
return stack.pop();
}
public static void main(String[] args) {
String postfixExpression = "23*5+";
int result = evaluatePostfix(postfixExpression);
System.out.println(result);
}
}
Output:
Download