单链表的特点是只能从头开始向后遍历,所以我们在增删查改的过程中都要考虑从头开始的问题,单链表的临界情况是对于头结点的操作,我们需要对头节点多加考虑。
通过画图来解决单链表的基本操作
-
头部插入 -
中间位置插入 -
删除头结点 -
删除中间结点 -
查找结点 -
修改结点 代码实现这些功能
class NodePractice{
int data;
NodePractice next;
public NodePractice(int data) {
this.data = data;
}
}
public class SingleLinkedListPractice {
int size;
NodePractice head;
public void addFirst(int data)
{
NodePractice nodePractice=new NodePractice(data);
if(size==0)
{
head=nodePractice;
size++;
}
else
{
nodePractice.next=head;
head=nodePractice;
size++;
}
}
public void addIndex(int index,int data){
if(index < 0 || index > size)
{
System.out.println("index illegal");
return;
}
if(index==0)
{
addFirst(data);
return;
}
NodePractice prev=head;
for (int i = 0; i < index-1; i++) {
prev=prev.next;
}
NodePractice nodePractice=new NodePractice(data);
nodePractice.next=prev.next;
prev.next=nodePractice;
size++;
}
public void addLast(int data)
{
addIndex(size,data);
}
public boolean checkIndex(int index){
if(index < 0 || index >size-1)
{
System.out.println("index illegal");
return false;
}
return true;
}
public void removeIndex(int index){
if(checkIndex(index))
{
if(index==0)
{
NodePractice nodePractice=head;
head=head.next;
nodePractice.next=null;
size--;
return;
}
else
{
NodePractice prev=head;
for (int i = 0; i < index-1; i++) {
prev=prev.next;
}
NodePractice nodePractice=prev.next;
prev.next=nodePractice.next;
nodePractice.next=null;
size--;
}
}
}
public boolean checkData(int data) {
NodePractice nodePractice=head;
for (int i = 0; i < size; i++) {
if(nodePractice.data==data)
{
return true;
}
nodePractice=nodePractice.next;
}
return false;
}
public void removeFirstData(int data)
{
if(checkData(data))
{
if(head.data==data)
{
removeIndex(0);
}
NodePractice prev=head;
while (prev.next != null)
{
if(prev.next.data==data)
{
NodePractice nodePractice=prev.next;
prev.next=nodePractice.next;
nodePractice.next=null;
size--;
return;
}
}
}
}
public void removeAllData(int data){
while (head != null && head.data==data)
{
NodePractice nodePractice=head;
head=head.next;
nodePractice.next=null;
size--;
}
if(head == null)
{
return;
}
NodePractice prev=head;
while (prev.next != null)
{
if(prev.next.data==data)
{
NodePractice nodePractice=prev.next;
prev.next=nodePractice.next;
nodePractice.next=null;
size--;
}
else
{
prev=prev.next;
}
}
}
public int seekIndex(int index){
if(checkIndex(index))
{
NodePractice nodePractice=head;
for (int i = 0; i < index; i++) {
nodePractice=nodePractice.next;
}
return nodePractice.data;
}
return -1;
}
private int count;
public int seekData(int data){
NodePractice nodePractice=head;
for (int i = 0; i < size; i++) {
if(nodePractice.data==data)
{
System.out.println("找到了");
return count;
}
nodePractice=nodePractice.next;
count++;
}
System.out.println("没找到");
return -1;
}
public void setIndex(int index,int data){
if(checkIndex(index))
{
NodePractice nodePractice=head;
for (int i = 0; i < index; i++) {
nodePractice=nodePractice.next;
}
nodePractice.data=data;
}
}
public void setData(int oleData, int newData){
if(checkData(oleData))
{
NodePractice nodePractice=head;
for (int i = 0; i < size; i++) {
if(nodePractice.data==oleData)
{
nodePractice.data=newData;
}
nodePractice=nodePractice.next;
}
}
}
@Override
public String toString() {
String ret="";
NodePractice nodePractice=head;
for (int i = 0; i < size; i++) {
ret+=nodePractice.data+"->";
nodePractice=nodePractice.next;
}
ret+="NULL";
return ret;
}
public static void main(String[] args) {
SingleLinkedListPractice singleLinkedListPractice=new SingleLinkedListPractice();
singleLinkedListPractice.addFirst(9);
singleLinkedListPractice.addFirst(7);
singleLinkedListPractice.addFirst(5);
singleLinkedListPractice.addFirst(3);
singleLinkedListPractice.addFirst(3);
singleLinkedListPractice.addFirst(3);
singleLinkedListPractice.addFirst(1);
singleLinkedListPractice.addFirst(3);
singleLinkedListPractice.addFirst(3);
singleLinkedListPractice.addFirst(3);
System.out.println(singleLinkedListPractice);
singleLinkedListPractice.setData(3,6);
System.out.println(singleLinkedListPractice);
}
}
|