Data structures List

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Suppose that we are using the usual Node class ( with instance variables called data and link) and that variable, locate us referring to a node in a linked list. Write an assignment statement that will make locate refer to the next node in the list(if there is one). It there is no next node, they your assignment statement should set locate to null

If(locate.link ==null) { locate = null; } else { locate = locate.link; }

A(n) _ Is an indexed data structure, which means you can select its elements in an arbitrary order as determined by the subscript value

Array

What are the steps to insert a new item at the head of a linked list

Assign a head, give node, increment size by 1 Head= new Node(0,head); Size++;

Draw a picture of the memory after the following statements Int[] m = new int[2]; M[0] = 0; M[1] = 1; Int[] p = m; p[0] = 4; p[0] = 5;

M -^ 0 1 P-^ Both pointing at same array

A(n) _ is a data structure that contains a data item and one or more links

Node

Implement the following method as a new method for the Node class(use the usual Node definition with instance variables called data and link). //precondition: head is the head reference of a linked list //the list might or might not be empty // postcondition: the return value is true if the list has atleasts one occurrence of the number 21 in the data part of the node

Node nodeRef = head; While(nodeRef != null) { If(nodeRef.data == 21) { Return true; } nodeRef = nodeRef.link; } Return false;

Suppose we re using the usual Node class(with instance variable called data and link). Your program is using a node variable called head to refer to the first node of a linked list( or head is null for an empty list). Write a few lines of Java code that will print out the values of all the even nodes of the list

Node nodeRef = head; int counter = 0; While(nodeRef != null) { counter++; If(counter % 2 ==0) { System.out.println(nodeRef.data); } nodeRef = nodeRef.link; }

Suppose that p,q and r are three references type variable, which point to nodes in a (single) linked list consisted of 10 nodes. The variable p refers to the first node, q refers to the 5th node and r refers to the last node. Write a few lines of code that will make a new copy of the list. Your code should set 3 new variable called x, y and z so that x refers to the first node of the copy, y refers to the 5th node of the copy and z refers to the last node of the copy

Node nodeRef = p; Node x = new Node(nodeRef.data); Node y = null; Node z = null; int counter =1; Node curnode =x; Node prevnode =null; nodeRef = nodeRef.link; While(nodeRef != null) { counter ++; prevnode= curnode; curnode= new Node(nodeRef.data); prevnode.link =curnode; If(counter == 5) { y = curnode; } If( counter == 10) { z= curnode; } nodeRef = nodeRef.link; }

Suppose that p is a reference variable that contains the bulk reference. What happens if the program tries to activate p.data;

NullPointerException

Write a class definition that could be used to define a node in a doubly linked list include only the instance variables, not the methods

Public static class Node { Public int data; Public node prev; Public node next; }

T/F the size is fixed for an array object

True

Draw a picture of the memory after the filling statements Int[] x = new int[2]; X[0] = 1; Int[]Y = new int[3]; Y[0] = 2; Y=x; Y[0] = 3;

Xy -> 3

Implement the following method as a new method for the Node class(use the usual Node definition with instance variables called data and link). Public int product(Node head) //precondition: head is the head reference of a linked list //the list might or might not be empty // postcondition: the return value is the product of all the data components of all the nodes. Note: if the list is empty the method returns 1

int result =1; Node nodeRef = head; while(nodeRef != null) { result = result * nodeRef.data; nodeRef = nodeRef.link; } return result;

Suppose that p is a reference to a node in a linked list and it is not the tail node. What are the steps to removing the node after p

p.next = p.next.next; Size--;

Suppose cursor refers to a node in a linked list (using the node class with instance variables called data and link). What Boolean expression will be true when cursor refers to the last node of the list

cursor.link == null

Suppose that I have the following declarations int[] data = new int[100]; int i; Write a segment of Java code that will shift data[51] ... data[99] down one spot to the locations data[50] ... data[98]. The value of data[99] remains at its original value. Use a for loop(not system.arraycopy)

for(int i=51; i<= 99; i++) { data[i-1] = data[i]; }

Suppose that I have the following declarations int[] data = new int[100]; int i; Write a small segment of Java code that shift data[50] ... data[98] up one spot to the locations data[51] ... data[99]. Then insert number 42 at the location data[50]. Use a for loop(not system.arraycopy)

for(int i=98; I>= 50; i--) { data[i+1] = data[i]; } data[50] = 42;

You could create a circular list from a single linked list by executing what statement

tail.next = head;


Kaugnay na mga set ng pag-aaral

Chapter 15 - Job Search and Resumes

View Set

Primerica: Types of Life Policies

View Set

INTG BUS POLICY/STRATEGY - Week 3

View Set

Security Final Exam Question Study Guide

View Set

CompTIA A+ 220-1002 Lesson 17:Installing Configuring and Troubleshooting Print Devices - Activity questions

View Set

Health and Wellness Chapter 1 Review Questions

View Set