Untitled
unknown
plain_text
2 years ago
5.2 kB
14
Indexable
//week 3 1
public class IntegerStack {
private int maxSize;
private int[] stackArray;
private int top;
// Constructor to initialize the stack with a given maximum size
public IntegerStack(int size) {
maxSize = size;
stackArray = new int[maxSize];
top = -1; // Initialize top as -1 when stack is empty
}
// Method to push an integer onto the stack
public void push(int value) {
if (!isFull()) {
stackArray[++top] = value;
} else {
System.out.println("Stack is full. Cannot push element.");
}
}
// Method to pop an integer from the stack
public int pop() {
if (!isEmpty()) {
return stackArray[top--];
} else {
System.out.println("Stack is empty. Cannot pop element.");
return -1; // Return a default value indicating stack underflow
}
}
// Method to display stack contents from top to bottom
public void displayFromTop() {
if (!isEmpty()) {
System.out.println("Stack from top to bottom:");
for (int i = top; i >= 0; i--) {
System.out.println(stackArray[i]);
}
} else {
System.out.println("Stack is empty.");
}
}
// Method to display stack contents from bottom to top
public void displayFromBot() {
if (!isEmpty()) {
System.out.println("Stack from bottom to top:");
for (int i = 0; i <= top; i++) {
System.out.println(stackArray[i]);
}
} else {
System.out.println("Stack is empty.");
}
}
// Method to display the top element of the stack
public int stackTop() {
if (!isEmpty()) {
return stackArray[top];
} else {
System.out.println("Stack is empty.");
return -1; // Return a default value indicating stack underflow
}
}
// Method to check if the stack is empty
public boolean isEmpty() {
return (top == -1);
}
// Method to check if the stack is full
public boolean isFull() {
return (top == maxSize - 1);
}
public static void main(String[] args) {
IntegerStack stack = new IntegerStack(5);
stack.push(10);
stack.push(20);
stack.push(30);
stack.displayFromTop();
System.out.println("Top element: " + stack.stackTop());
System.out.println("Popped element: " + stack.pop());
stack.displayFromBot();
}
}
//week 3 2
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public class IntegerLinkedList {
private Node head;
// Default constructor
public IntegerLinkedList() {
head = null;
}
// Method to check if the linked list is empty
public boolean isEmpty() {
return head == null;
}
// Method to display the linked list
public void display() {
Node current = head;
if (isEmpty()) {
System.out.println("List is empty");
return;
}
System.out.println("Linked list:");
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
// Method to add a node at the end of the linked list
public void addAtLast(int data) {
Node newNode = new Node(data);
if (isEmpty()) {
head = newNode;
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
// Method to delete the node at the head of the linked list
public void deleteAtHead() {
if (isEmpty()) {
System.out.println("List is empty. Nothing to delete.");
return;
}
head = head.next;
}
public static void main(String[] args) {
IntegerLinkedList linkedList = new IntegerLinkedList();
System.out.println("Is list empty? " + linkedList.isEmpty());
linkedList.addAtLast(10);
linkedList.addAtLast(20);
linkedList.addAtLast(30);
System.out.println("Is list empty? " + linkedList.isEmpty());
linkedList.display();
linkedList.deleteAtHead();
System.out.println("After deleting at head:");
linkedList.display();
}
}
//vector demo
import java.util.*;
public class vectordemo
{
public static void main(String arg[])
{
Vector v=new Vector();
int x=10;
Integer y=new Integer(20);
String str="pk";
v.add(x);
v.add(y);
v.add(str);
v.add(2,new Integer(30));
System.out.println("the elements of vector"+v);
System.out.println("the size of vector are"+v.size());
System.out.println("the element at position 2 is"+v.elementAt(2));
System.out.println("the first element of the vector is"+v.firstElement());
v.removeElementAt(2);
System.out.println("the elements of vector is "+v);
System.out.println("the size of vector are"+v.size());
}
}
Editor is loading...
Leave a Comment