1 Replies - 750 Views - Last Post: 09 October 2012 - 05:26 PM Rate Topic: -----

#1 MitchKrendel  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 09-February 12

Printing a Linked List of integers in reverse

Posted 09 October 2012 - 04:32 PM

Hello everyone. I have created a program that involves entering integers into a linked list. I created printLinked (a method that prints the list), delrep (a method that deletes any integers in the list that occur more than once) and invert (a method which reverses the order of integers in the linked list).

Everything seems to be working fine, the only problem being that I am not able to use my printLinked method to print the reversed order of integers. I am able to output the reversed list via a println in my invert() method, so therefore I seem to at least have that method working for the most part.

Can anyone please let me know what I am doing wrong? I have to print the inverted list of integers via the printLinked() method and seem to be missing something in order to make this happen.

Thanks.


import java.util.Scanner;


public class LinkedList

{
	public static void main(String[] args)
	{

		ListNode head = new ListNode();
		ListNode tail = head;
		tail.link = null;
		int input;

		Scanner keyboard = new Scanner(System.in);
		System.out.println("Enter a list of integers seperated with blanks and ending with a 0");
		input = keyboard.nextInt();
		ListNode temp;


		while(input != 0)
		{

			temp = new ListNode();
			temp.data = input;
			temp.link = null;
			tail.link = temp;
			tail = temp;
			input = keyboard.nextInt();




		}

			System.out.println("You entered the following integers : " );
			printLinked(head);
			delrep(head);
			System.out.println("After deleting repeating integers, you are left with : " );
			printLinked(head);
			invert(head);
			System.out.println("Your inverted list of integers is : ");
			printLinked(head);



}




	public static void printLinked(ListNode list)
		{
			ListNode cursor = list;

			while (cursor != null)
			{

			        System.out.print(cursor.data + " ");
				cursor = cursor.link;
			}



			System.out.println("");


		}

	public static void delrep(ListNode head)
	{
			ListNode previous = head;
			ListNode current = previous.link;


			while (current != null)
			{
				ListNode dummy = head;

				while (dummy != current)
					{


					      if (dummy.data == current.data)
						{

							ListNode temp = current.link;
							previous.link = temp;
							current = temp;

						}

						dummy = dummy.link;
					}

			if (dummy == current)
				{
					previous = current;
					current = current.link;

				}
			}
	}


	public static void invert(ListNode head)
		{

			ListNode temp1 = null;
			ListNode temp2 = null;

			while (head != null)
			{
				temp1 = head.link;
				head.link = temp2;
				temp2 = head;
				head = temp1;



			}

			head = temp2;


			while (head.link != null)
			{
				System.out.print(head.data + " ");
				head = head.link;
			}
			System.out.println("");


		}
}










Is This A Good Question/Topic? 0
  • +

Replies To: Printing a Linked List of integers in reverse

#2 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Printing a Linked List of integers in reverse

Posted 09 October 2012 - 05:26 PM

It all has to do with the fact you have a instance variable head and a local variable head. You are never actually changing the value of the instance variable head, only the local variable inside invert. When your invert method is complete the local variable is gone and you never really changed anything. Be sure when you are wanting to refer to the instance variable head that you use

this.head //code


This post has been edited by Kinaces: 09 October 2012 - 05:26 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1