public class Node {
private Person obj;
public Node next;
public Node () {
}
public Person getObj() {
return obj;
}
public void setObj(Person obj) {
this.obj = obj;
}
public Node (Person newobj) {
this.obj=newobj;
next=null;
}
public void Show () {
System.out.println (obj.getData());
}
}
public class Person {
private int seqno;
private String surname="";
public Person() {
String sr = "";
sr += (char) randInt(65, 65 + 32);
for (char c = 2; c <= 5; c++) {
sr += (char) randInt(65 + 32, 65 + 25 + 32);
}
setSurname(sr);
}
public Person(int seqno,String sur) {
String x = "";
//Uppercase 1st character
x += (char)(65 + (int)(Math.random()*(double)((90)-65+1)));
//4 lowercase characters
for (int charNo = 2; charNo <= 5; charNo++){
x += (char)((97) + (int)(Math.random()*(double)((122)-(97) +1)));
}
this.seqno=seqno;
this.surname=sur;
surname=x;
setSurname(surname);
}
public void setSeqno(int sqno) {
this.seqno = sqno;
}
public void setSurname(String srn) {
this.surname = srn;
}
public int getSeqno () {
return seqno;
}
public String getSurname () {
return surname;
}
public String getData () {
return ("the surname is\t"+surname+"the seq no is \t"+seqno);
}
public static int randInt(int min, int max) {
return min + (int) (Math.random() * (double) (max - min + 1));
}
}
public class LinList {
private Node head;
private Node last;
private int NodesinList;
public int getNodesinList() {
return NodesinList;
}
public void setNodesinList(int NodesinList) {
this.NodesinList = NodesinList;
}
LinList() {
head = null;
last = null;
}
public void Add(Person newObj) {
Node newNode = new Node(newObj);
NodesinList++;
if (head == null) {
head = newNode;
last = newNode;
} else {
last.next = newNode;
last = newNode;
}
newNode.getObj().setSeqno(NodesinList);
}
public void ListAll() {
if (head != null) {
Node temp = head;
while (temp != null) {
temp.Show();
temp = temp.next;
}
}
}
public Node SearchKey(int key) {
Node temp = head;
while (temp != null) {
if (temp.getObj().getSeqno() == key) {
return temp;
}
temp = temp.next;
}
return null;
}
public void deleteObj(int key) {
Node prev;
Node thisN;
boolean isFound = false;
if (head != null) {
prev = head;
thisN = head;
do {
if (prev.getObj().getSeqno() == (key)) {
isFound = true;
break;
} else {
prev = thisN;
thisN = thisN.next;
}
} while (prev != last);
if (isFound == true) { //test marginal cases
if (thisN == prev) {
head = head.next;
} else {
prev.next = thisN.next;
}
thisN = null;
}
}
NodesinList--;
}
}
Deletion from a Linear list
Page 1 of 11 Replies - 150 Views - Last Post: 31 May 2012 - 02:29 PM
#1
Deletion from a Linear list
Posted 31 May 2012 - 03:49 AM
Hi, I am currently doing a Linear List but I'm having problems when I come to delete from it.The code isn't deleting the node that I'm requesting. Here is my code:
Replies To: Deletion from a Linear list
#2
Re: Deletion from a Linear list
Posted 31 May 2012 - 02:29 PM
I think you mean that you're writing a linked list. I did a simple test that only checked the no-argument Person() constructor, and the deleteObj() method of your linked list seems to be working fine. What code are you running to test the linked list, what results are you expecting, and what results are you getting? The way you're setting the variable Person.seqno may be confusing you in two ways: It's not 0-indexed, and it's being set by the LinList class. If the sequence number is a Person class attribute (and I'm not sure it should be), then Person.seqno should be controlled by (encapsulated within) the Person class.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|