473,396 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

help with singly linklists

14
We were giving a sample link list to play around with for class. When running the program, the last line of the print out says:

List After adding a Node: 7 6 5 4 3 2 1 0

However I added a method called stepOne that is suppose to swap positions of the 5 and the 4 in order that it would print out

List After adding a Node: 7 6 4 5 3 2 1 0

However I don't get this print out.

Expand|Select|Wrap|Line Numbers
  1.  public void stepOne(){
  2.  
  3.    Node position=first;
  4.  
  5.  
  6.    while(position.data!=5 && position!=null){
  7.  
  8.     position=position.next;
  9.  
  10.     position.next=position;
  11.  
  12.  
  13.     }
  14.  
  15.  
  16.     position=first.next.next;
  17.  
  18.     position.next.next=position;
  19.  
  20.     }
  21.  
Oct 27 '06 #1
7 1172
Ganon11
3,652 Expert 2GB
We were giving a sample link list to play around with for class. When running the program, the last line of the print out says:

List After adding a Node: 7 6 5 4 3 2 1 0

However I added a method called stepOne that is suppose to swap positions of the 5 and the 4 in order that it would print out

List After adding a Node: 7 6 4 5 3 2 1 0

However I don't get this print out.

Expand|Select|Wrap|Line Numbers
  1.  public void stepOne(){
  2.  
  3.    Node position=first;
  4.  
  5.  
  6.    while(position.data!=5 && position!=null){
  7.  
  8.     position=position.next;
  9.  
  10.     position.next=position;
  11.  
  12.  
  13.     }
  14.  
  15.  
  16.     position=first.next.next;
  17.  
  18.     position.next.next=position;
  19.  
  20.     }
  21.  
I can see one problem in your while(...) loop.

First, you set position to position.next. This basically moves position up one place in your linked list. However, your next statement stores position into position.next. Now, the next time the loop executes, you will set position equal to itself - since position.next IS position! Your while loop will execute infinitely, since the data is never 5 and position is never null.
Oct 27 '06 #2
yolkman
14
I've got it to the point now where instead of displaying: 7 6 5 4 3 2 1 0
it says 7 6 5 3 2 1 0. However the 4 doesn't show up between the 6 and the 5.

Expand|Select|Wrap|Line Numbers
  1. while(position.data!=5 && position!=null){
  2.  
  3.     position=position.next;
  4.  
  5.     previous=position;
  6.  
  7.     }
  8.  
  9.  
  10.     position.next=position.next.next;
  11.  
  12.  
  13.  
  14.  
  15.  
  16.     }
  17.  
Oct 27 '06 #3
yolkman
14
I've modified the code some more and now instead of printing out 7 6 5 4 3 2 1 0
i get 7 6 4 5. How do I get the 3 2 1 and 0 to show after the 5?

Expand|Select|Wrap|Line Numbers
  1. Node previous=first;
  2. Node current=first.next;
  3.  
  4. while(current.data!=5 and current!=null){
  5. previous=current;
  6. current=previous.next;
  7. }
  8.  
  9. previous.next=current.next;
  10. previous.next.next=new Node(5);
  11.  
  12.  
Oct 28 '06 #4
Ganon11
3,652 Expert 2GB
From your code, I'm not sure where to go...so let me suggest the following code:

Expand|Select|Wrap|Line Numbers
  1. Node current = first;
  2. Node prev = null;
  3.  
  4. while (current.data != 6 && current != null) {
  5.    prev = current;
  6.    current = current.next;
  7. }
  8.  
  9. if (current == null) System.out.println("Error; expected value not found!");
  10. else {
  11.    Node temp = prev;
  12.    prev = current;
  13.    current = temp;
  14. }
See if this will work for you.
Oct 29 '06 #5
yolkman
14
From your code, I'm not sure where to go...so let me suggest the following code:

Expand|Select|Wrap|Line Numbers
  1. Node current = first;
  2. Node prev = null;
  3.  
  4. while (current.data != 6 && current != null) {
  5.    prev = current;
  6.    current = current.next;
  7. }
  8.  
  9. if (current == null) System.out.println("Error; expected value not found!");
  10. else {
  11.    Node temp = prev;
  12.    prev = current;
  13.    current = temp;
  14. }
See if this will work for you.

It didn't give me the printout 7 6 4 5 3 2 1 0
Instead it printed out 7 6 5 4 3 2 1 0
Oct 30 '06 #6
r035198x
13,262 8TB
It didn't give me the printout 7 6 4 5 3 2 1 0
Instead it printed out 7 6 5 4 3 2 1 0
Could you post the entire code you are using to test this. I'd like to join but I think I need to look at all the parts first.
Oct 30 '06 #7
yolkman
14
Could you post the entire code you are using to test this. I'd like to join but I think I need to look at all the parts first.
I got it to work now. Thanks for your help!
Oct 30 '06 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: HS-MOON | last post by:
I'm asking you to help me. I'm a beginner of studying c++. I'm trying to make the Singly Linked List(Ordered). Anyway, I've been debugging all day. I can't sort it out!! As you see, I don't...
19
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
7
by: Shwetabh | last post by:
Hi, can some one tell me: -> how to remove a loop from a singly linked list with a loop. -> how to count the number of nodes in a looped singly link list. Thanks
13
by: XXXXXX.working.in.my.blood | last post by:
hi all, i need help with linked lists... the problem is this, "reverse the contents of a singly linked list without using a temporary node"... solution with code will be appreciated...
13
by: Raj | last post by:
Is there any way to delete a particular node from a singly linked list where the header of the list is unknown.Only pointer available is the one which points to the node to be deleted
3
by: malik | last post by:
// Linked Lists in classes(excluding structures) without using tail pointer # include<iostream.h> # include<stdlib.h> void Swap(int num1, int num2) { int a = num1; num2 = num1; num2 = a;
3
by: lrsweene | last post by:
How to swap elements in a singly linked list?
6
by: Alien | last post by:
Hi, I am having problem with ordering a singly linked list. Since a singly linked list goes only one way, is it possible to order them? If my structs look like the one below. struct block...
23
by: Himanshu Chauhan | last post by:
Hi! I was wondering, In the first parse of a singly linked list of unknown length, is it possible to know when we are at middle of the linked list? Regards --Himanshu
4
by: saki | last post by:
How do we reverse a singly linked list without using extra memory.Extra pointers can however be used
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.