Download Example

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Intuition and Example for Linked Lists
Consider a situation where we have an array that stores letters. Along with each letter, we store the position
of the next letter in alphabetical order. We store these two pieces of information (the letter and index of the
next letter) in an object of class Node:
class Node {
int data;
int next;
theArray[0].data
// …
}//end class Node;
Assume we declare an array of Node
shown at right:
Node[] theArray =new Node[10];
// …
0
1
2
3
4
5
6
7
8
9
data
a
f
g
h
m
p
next
1
2
3
4
5
-1
theArray[0].next
Consider the following code used to insert the letter ‘b’ in ascending alphabetical order:
0. Node array[ 10];
1. int freeRow=6;
2. char letter = ’b’;
3. // now insert it
4. array[ freeRow].data = letter;
5. array[ freeRow].next = 1;
6. array[0].next = freeRow;
7. freeRow++;
…
// And to display letters in order:
current = 0;
while ( current != -1) {
System.out.print( array[current].data + ” ”);
current = array[ current].next;
0
1
2
3
4
5
6
7
8
9
data
a
f
g
h
m
p
next
1
2
3
4
5
-1
}
Issues in doing this are that we want our code to work in general for any letter. We also have the restriction
above in that we can store at most 10 letters. Instead of using an array, we modify the Node class and get a
new Node each time we want to add a letter, connecting this new node into the exiting list.
public class Node
{
private int data;
private Node pNext;
// ... Constructors, get & set methods, and toString method
//end class Node
Consider the code needed to create:
pHead
e
d
See the next page for a sample program to add numbers to a list, though not in order.
import java.util.Scanner;
// used for keyboard input
public class LinkedList
{
public static void main(String[] args){
LinkedList instance = new LinkedList();
instance.mainLoop();
// chain off to avoid static errors
}//end main()
public void mainLoop(){
int number = 0;
// Used to store numbers read in
Node pHead = null;
// head of linked list
Node pTemp;
// used in new node creation
int menuChoice;
// Menu choice
int answer;
// Answer of chosen operation
Scanner keyboard = new Scanner( System.in);
// use for user input
System.out.print( "Find sum or product of a variable number \n" +
" of integers. Enter as many integers > 0 as you like \n" +
" followed by the value -1.\n" +
"Enter the numbers: ");
while( number != -1) {
// loop to read in numbers
number = keyboard.nextInt();
if (number != -1) {
pTemp = new Node();
// Insert at head of list
pTemp.setData( number);
// store the number
pTemp.setpNext( pHead);
// set the "pointer"
pHead = pTemp;
// reset the head of the list
}
}//end while( number...
displayList( pHead);
// display the contents of the list
System.out.print( "Please select the number of one of these options: \n" +
"
1. Sum the numbers in the list\n" +
"
2. Multiply the numbers in the list \n" +
"
3. Exit the program.\n" +
"Your choice: ");
menuChoice = keyboard.nextInt();
if (menuChoice == 3) {
System.exit( 0); // exit the program
}
// process the number, either adding them or multiplying them
answer = processNumbers( pHead, menuChoice);
System.out.print( "Answer is: " + answer + "\n");
System.out.println( "Exiting program...");
}//end mainLoop()
public void displayList( Node pTemp)
{
// since parameter is a copy, changing it doesn't affect the original
System.out.println("Just entered method displayList...");
while( pTemp != null) {
System.out.print( pTemp + " "); // display the node
pTemp = pTemp.getpNext();
// advance to next node
}//end while( pTemp...
System.out.println("");
}//end displayList()
public int processNumbers( Node pTemp, int operation)
{
int result = 0;
// stores the calculated answer
if (operation == 2)
{
// Multiply was the action chosen
result = 1;
}
while (pTemp != null) {
// Go through list, doing the operation
if (operation == 1)
// Add was the action chosen
result += pTemp.getData();
else if (operation == 2)
// Multiply was the action chosen
result *= pTemp.getData();
pTemp = pTemp.getpNext();
// Advance to next list member
}
return result;
}//end processNumbers()
}//end class LinkedList