Nineth Day of Java Learning

Source: Internet
Author: User

---restore content starts---

Today is 2016, 5,12.java study Nineth Day! 2016-05-12

Today is mainly the study last time did not finish, words do not say more, on the code!

Node:

Package Com.cqvie.lianbiao;

public class Node {
public String data;
Public Node Next;


}

Link:

Package Com.cqvie.lianbiao;

public class Link {//lead node's one-way list
Private Node Head=null;
Public Link () {//constructor method
Head=new Node ();
Head.next=null;
}
public void Add (String data) {//Add nodes at the end
Node A=head;
while (a.next!=null) a=a.next;//Find tail node
Node tmp=new node ();//NEW Nodes TMP
Tmp.data=data;
Tmp.next=null;
a.next=tmp;//after connecting to the tail node
}
public void AddAll (String data[]) {

}
public int size () {//Length of List
Node A=head;
int n=0;
while (A.next!=null) {
n=n+1;
A=a.next;
}
return n;
}
public Boolean isEmpty () {//Determine if the linked list is empty
return head.next==null;
}
Public Boolean contains (String data) {//To determine if a node is contained in a linked list
Node A=head;
while (A.next!=null) {
A=a.next;
if (a.data.equals (data))
return true;
}
return false;

}
public void Remove (String data) {//delete a node
Node P=head;
Node Q;
while (P.next!=null) {
Q=p;
P=p.next;
if (p.data.equals (data))
Q.next=p.next;
}
}
Public string[] ToArray () {
return null;
}
Public String get (int index) {
return null;
}
public void Clear () {

}
public string print () {//Concatenate all nodes of the list into a string and output
Node A=head;
String s= "";
while (A.next!=null) {
A=a.next;
s+=a.data+ "--";
}
return s;
}
}

Test:

Package Com.cqvie.lianbiao;

public class Test {

public static void Main (string[] args) {
Link link=new link ();
Link.add ("Node 1");
Link.add ("Node 2");
Link.add ("Node 3");
System.out.println ("Whether this list is empty:" +link.isempty ());
System.out.println ("The length of this list:" +link.size ());
System.out.println ("This list is:" +link.print ());
System.out.println ("This list has:" Node 3 ":" +link.contains ("Node 3"));
Link.remove ("Node 2");
System.out.println ("After deleting Node 2:" +link.print ());

}

}

Output Result:

Whether this list is empty: false
Length of this list: 3
This list is: node 1--> node 2--> node 3-->
Is there: "Node 3" in this list: true
After removing Node 2: node 1--> node 3-->

Today's study feel the snack force, mainly on the structure of Java is not familiar with the previous C code mastery is not very strong, for example, delete the operation of the node, the code in C is not safe to master, here change a structure, feel very laborious. So, the basics are important. These are not finished, the day after tomorrow to fill 51 false lessons, and then fight!

---restore content ends---

Nineth Day of Java Learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.