Java linklist Code

Discussion in 'Java' started by cyrow, Nov 19, 2007.

  1. cyrow

    cyrow New Member

    Joined:
    Nov 19, 2007
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Home Work Problem.
    I understand what the program is suppose to do, but I do not understand how to implement it using Link List.

    Please help me to understand the problem.

    Write a program to perform ariththmetic with integers of unlimited size. You must read data from the input file, input.txt and send output to the file, output.txt.

    Each data line has the following format:

    number1 operator number 2, where the operator can be one of +, - or *. The operator is preceeded and followed by one or more spaces.

    For + and -, Number1 and Number 2 are to be treated as large integers and each must be stored in a link list. Output the given expression and on the next line print the answer. For example,
    22222233333333 + 4444455555 =

    For *, Number1 is a large integer to be stored in a linked list. Number2 is a small integer whcih can be stored in an int variable. Output the given expression ans on the next line, print the answer for example,
    23222222222222 * 7 =
    223232323223333
    the last line of data contains a zero.

    Please help me to under the algorithm in solving this problem
    Code:
    Class Node {
    	Int num;
    	Node next;
    }
    
    Public Class Number {
    Public static void main (String [] args) {
    	Scanner in = new Scanner (new File Reader (“input.txt”)
    	PrinterWriter out = new PrintWriter (new FileWriter (“output.txt”)
    	
    	Node top, last, np = null;
    	top = null;
    
    // Load the input file into the link list 
    
    	int n = in.nextInt()
    	While (n! =0) {
    	np = new node (num)
    	 
    	if (top = = null) {
    		top = np;
    	else {
    		last.next = np;
    		num = in.nextInt ()
    	  }
    	}
    
    	Curr = top
    While (curr !=Null)
    String sign = in.next()
    If (sign. equals (‘+’) {
    	Int sum = prev.next +curr.next
    	Curr = curr.next
     //write to output file
    Elseif{ 
    	If (sign. equals (‘-’) {
    	Int difference = prer.next -curr.next
    	Curr = curr.next
    //write to output file
    
    }
    Else{
    	
    If (sign. equals (‘*’) {
    	Int product = curr.num *curr.next
    	Curr = curr.next
    //write to output file
    
    }
     
    Last edited by a moderator: Nov 19, 2007

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice