Subject Computer Science Java Programming

Question

1.Assume the following declarations (which are used to process singly-linked lists)

static class Node
{
public int data;
public Node next;
};

And the following statemens have been executed,

      Node p1, p2, p3;
      p1 = new Node();
      p2 = new Node();
      p3 = new Node();

Tell what will be displayed by each of the code segments or explain why an error occurs,

1)p1.data=123;
      p2.data=456;
      p1.next=p2;
      p2.next=null;

      // output results
      System.out.printf( " %d %d\n", p1.data, p1.next.data);


2)p1.data = 123;
p2.data = 456;
p1 = p2;

// output results
      System.out.printf( " %d %d\n", p1.data, p2.data);


3)p1.data=123;
      p2.data=456;
      p1.next=p2;
      p2.next=null;

      // output results
      System.out.printf( " %d %d\n", p2.data, p2.next.data);

4)p1.data=123;
      p2.data=456;
      p3.data =456;
p1.next=p2;
p2.next=p3;
      p3.next=null;

      // output results
      System.out.printf( " %d %d\n", p1.data, p1.next.data);
      System.out.printf( " %d %d\n", p2.data, p2.next.data);
      System.out.printf( " %d\n", p1.next.next.data);
System.out.printf( " %d\n", p3.data);

5)p1.data=123;
      p2.data=456;
p1.next=p2;
p2.next=p1;

      // output results
      System.out.printf( " %d %d\n", p1.data, p2.data);
      System.out.printf( " %d %d\n", p1.next.data, p2.next.data);
      System.out.printf( " %d\n", p1.next.next.data);


6)p1.data=123;
      p2.data=456;
p1=p2;
p2.next=p1;

      // output results
      System.out.printf( " %d %d\n", p1.data, p2.data);
      System.out.printf( " %d %d\n", p1.next.data, p2.next.data);


7)p1.data=123;
      p2.data=456;
p3.data=789;
p1.next = p2;
p2.next=p3;

p3.next = p2.next;
p2.next = p1.next;
p1.next = p1;

      // output results
      System.out.printf( " %d %d %d\n", p1.data, p2.data, p3.data);
      System.out.printf( " %d %d %d\n", p1.next.data, p2.next.data, p3.next.data);

8)p1.data=123;
      p2.data=456;
p3.data=789;
p1.next = p2;
p2.next=p3;

p3.next = p2;
p3.next.next = p1;

      // output results
      System.out.printf( " %d %d %d\n", p1.data, p2.data, p3.data);
      System.out.printf( " %d %d %d\n", p1.next.data, p2.next.data, p3.next.data);


Problems 2 and 3 will have the following class Node.

static class Node
{
public char data;
public Node next;
public Node previous;
};


2.Answer the following questions for following doubly-linked list with the external reference P1, write the program fragment of each following question. There is NO first reference.


You can only use pointer P1 to access the doubly-linked list, write program statements to do the following, each question is independent based on the original status.

1)Display the content of the nodes in alphabetical order from D to Y.
2)Replace ‘D’ by ‘M’ and replace ‘R’ by ‘S’.
3)Delete the node containing ‘T’
4)Inserting ‘S’ after node ‘R’ and before node ‘T’.


3.Answer all the sub-questions but for the following circular doubly-linked list with the external reference P2, write the statements of each following question.


You can only use pointer P2 to access the doubly-linked list, write program statements to do the following, each question is independent based on the original status.

1)Display the content of the nodes in alphabetical order from B to T.

2)Replace ‘E’ by ‘M’ and replace ‘S’ by ‘R’.

3)Delete the node containing ‘T’

4)Inserting ‘D’ after node ‘B’ and before node ‘E’.

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

Assume the following declarations (which are used to process singly-linked lists)

static class Node
{
public int data;
public Node next;
};

And the following statemens have been executed,

      Node p1, p2, p3;
      p1 = new Node();
      p2 = new Node();
      p3 = new Node();

Tell what will be displayed by each of the code segments or explain why an error occurs,

1)p1.data=123;
      p2.data=456;
      p1.next=p2;
      p2.next=null;...

This is only a preview of the solution. Please use the purchase button to see the entire solution

$40.00

or free if you
register a new account!

Assisting Tutor

Related Homework Solutions

Get help from a qualified tutor
Live Chats