Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

package ds; public class Stack { public int size; public int top; public int[] a

ID: 3587272 • Letter: P

Question

package ds;

public class Stack {

public int size;

public int top;

public int[] array;

public Stack () {

size = 0;

top = -1;

array = null;

}

public Stack (int _size) {

size = _size;

top = -1;

array = new int[size];

}

/*

* Implement the Stack-Empty(S) function

*/

public boolean empty () {

}

/*

* Implement the Push(S, x) function

*/

public void push (int x) {

}

/*

* Implement the Pop(S) function

* Return -1 if the stack is empty

*/

public int pop () {

}

/*

* Convert stack to string in the format of #size, [#elements]

*/

public String toString () {

String str;

str = size + ", [";

for (int i = 0; i <= top; i++)

str += array[i] + ", ";

str += "]";

return str;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

Stack s;

s = new Stack(10);

for (int i = 0; i < 5; i++)

s.push(i);

System.out.println(s.toString());

for (int i = 0; i < 2; i++)

s.pop();

System.out.println(s.toString());

}

}

package ds;

public class Queue {

public int size;

public int[] array;

public int head;

public int tail;

public Queue () {

size = 0;

array = null;

head = -1;

tail = 0;

}

public Queue (int _size) {

size = _size;

array = new int[size];

head = -1;

tail = 0;

}

/*

* Implement the ENQUEUE(Q, x) function

*/

public void enqueue (int x) {

}

/*

* Implement the DEQUEUE(Q) function

*/

public int dequeue () {

}

/*

* Convert queue to string in the format of #size, head, tail,

[#elements]

*/

public String toString () {

String str;

str = size + ", " + head + ", " + tail + ", [";

for (int i = head; i%size < tail; i++)

str += array[i] + ",";

str += "]";

return str;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

Queue q;

q = new Queue(10);

for (int i = 0; i < 5; i++)

q.enqueue(i);

System.out.println(q.toString());

for (int i = 0; i < 2; i++)

q.dequeue();

System.out.println(q.toString());

}

}

package ds;

public class LinkedList {

public ListNode head;

public LinkedList () {

head = null;

}

/*

* Implement the LIST-SEARCH(L, k) function

*/

public ListNode search (int k) {

}

/*

* Implement the LIST-INSERT(L, x) function

* Note that x is a integer value, not a ListNode

*/

public void insert (int x) {

}

/*

* Implement the LIST-DELETE(L, x) function

*/

public void delete (ListNode x) {

}

/*

* Convert a LinkedList to a string in the format of [#elements]

*/

public String toString () {

String str;

ListNode n;

str = "[";

n = this.head;

while (n != null) {

str += n.key + ",";

n = n.next;

}

str += "]";

return str;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

LinkedList l;

l = new LinkedList();

for (int i = 0; i < 5; i++)

l.insert(i);

System.out.println(l.toString());

for (int i = 0; i < 2; i++)

l.delete(l.head.next);

System.out.println(l.toString());

}

}

for Stack in Stack.java as discussed in Lecture 5. Queue in Queue.java as discussed in Lecture 5. tion in LinkedList.java as discussed in Lecture 6. .Task 2 (30 pts). Implement the enqueue(int v), dequeue() function for . Task 3 (40 pts). Implement the search(int k), insert(int x), delete) func-

Explanation / Answer

package ds;

public class Stack {

public int size;

public int top;

public int[] array;

public Stack () {

size = 0;

top = -1;

array = null;

}

public Stack (int _size) {

size = _size;

top = -1;

array = new int[size];

}

/*

* Implement the Stack-Empty(S) function

*/

public boolean empty () {

//empty only when top is -1

if(top==-1)

return true;

return false;

}

/*

* Implement the Push(S, x) function

*/

public void push (int x) {

//insert only when there is some space left

if(top<size)

{

top++;

array[top]=x;

}

}

/*

* Implement the Pop(S) function

* Return -1 if the stack is empty

*/

public int pop () {

if(empty())

return -1;

top--;

return array[top+1];

}

/*

* Convert stack to string in the format of #size, [#elements]

*/

public String toString () {

String str;

str = size + ", [";

for (int i = 0; i <= top; i++)

str += array[i] + ", ";

str += "]";

return str;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

Stack s;

s = new Stack(10);

for (int i = 0; i < 5; i++)

s.push(i);

System.out.println(s.toString());

for (int i = 0; i < 2; i++)

s.pop();

System.out.println(s.toString());

}

}

package ds;

public class Queue {

public int size;

public int[] array;

public int head;

public int tail;

public Queue () {

size = 0;

array = null;

head = -1;

tail = 0;

}

public Queue (int _size) {

size = _size;

array = new int[size];

head = -1;

tail = 0;

}

/*

* Implement the ENQUEUE(Q, x) function

*/

public void enqueue (int x) {

if(head==-1)

head=0;

if(tail<size)

{

array[tail]=x;

tail++;

}

}

/*

* Implement the DEQUEUE(Q) function

*/

public int dequeue () {

if(head==-1)

return -1;

head++;

return array[head-1];

}

/*

* Convert queue to string in the format of #size, head, tail,

[#elements]

*/

public String toString () {

String str;

str = size + ", " + head + ", " + tail + ", [";

for (int i = head; i%size < tail; i++)

str += array[i] + ",";

str += "]";

return str;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

Queue q;

q = new Queue(10);

for (int i = 0; i < 5; i++)

q.enqueue(i);

System.out.println(q.toString());

for (int i = 0; i < 2; i++)

q.dequeue();

System.out.println(q.toString());

}

}

package ds;

public class LinkedList {

public ListNode head;

public LinkedList () {

head = null;

}

/*

* Implement the LIST-SEARCH(L, k) function

*/

public ListNode search (int k) {

ListNode node;

node=this.head;

while(node!=null)

{

if(node.key==k)

return node;

node=node.next;

}

return null;

}

/*

* Implement the LIST-INSERT(L, x) function

* Note that x is a integer value, not a ListNode

*/

public void insert (int x) {

ListNode node;

node=this.head;

while(node.next!=null)

{

node=node.next;

}

node.next=new ListNode();

node.next.key=x;

node.next.next=null;

}

/*

* Implement the LIST-DELETE(L, x) function

*/

public void delete (ListNode x) {

ListNode node;

node=this.head;

if(node==x)

this.head=head.next;

while(node.next!=x)

{

node=node.next;

}

node.next=node.next.next;

}

/*

* Convert a LinkedList to a string in the format of [#elements]

*/

public String toString () {

String str;

ListNode n;

str = "[";

n = this.head;

while (n != null) {

str += n.key + ",";

n = n.next;

}

str += "]";

return str;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

LinkedList l;

l = new LinkedList();

for (int i = 0; i < 5; i++)

l.insert(i);

System.out.println(l.toString());

for (int i = 0; i < 2; i++)

l.delete(l.head.next);

System.out.println(l.toString());

}

}