心情不错,写个不带头单向链表

package Interface;

public interface ILinked {
    // 1、无头单向非循环链表实现
        //头插法
        void addFirst(int data);
        //尾插法
        void addLast(int data);
        //任意位置插入,第一个数据节点为0号下标
        boolean addindex(int index,int data);
        //查找是否包含关键字key是否在单链表当中
        boolean contains(int key);
         //删除第一次出现关键字为key的节点
          int remove(int key);
        //删除所有值为key的节点
        void removeAllKey(int key);
        //得到单链表的长度
        int getLength();
        void display();
        void clear();
    }


package DS;
import Interface.ILinked;
//无头单向非循环链表
public class MySingleListImpl implements ILinked {
    class Node{
        private int data;
        private Node next;
       Node(int data){
            this.data = data;
            this.next = null;
        }
    }
    private Node head ;
    private MySingleListImpl(Node head){
        this.head = head;
    }
    public Node getHead(){
        return head;
    }
    @Override
    public void addFirst(int data) {
        Node node = new Node(data);
        if (this.head == null) {
            this.head = node;
        } else {
            node.next = this.head;
            this.head = node;
        }
    }
    @Override
    public void addLast(int data) {
        Node node = new Node(data);
        Node cur = this.head;
        if(this.head == null){
            this.head = node;
        }else{
            while(cur.next!=null){
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    @Override
    public boolean addindex(int index, int data) {
        Node cur = this.head;
        if(index<0||index>=getLength()){
            return false;
        }
        if(index == 0){
            addFirst(data);
            return true;
        }
        if(index == getLength()-1){
            addLast(data);
            return true;
        }
        int count = 0;
        Node node = new Node(data);
        while(index != count){
            cur = cur.next;
            count++;
        }
        node.next = cur.next;
        cur.next = node;
        return true;
    }

    @Override
    public boolean contains(int key) {
        Node cur = this.head;
        while(cur!=null){
            if(cur.data == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    public Node SearchPrevIndex(int key){
        Node prev = this.head;
        while(prev.next != null){
            if(prev.next.data == key){
                return prev;
            }
            prev = prev.next;
        }
        return null;
    }
    @Override
    public int remove(int key) {
        if(this.head == null){
            throw new UnsupportedOperationException("单链表为空");
        }
        if(this.head.data == key){
            this.head = this.head.next;
            return key;
        }
        Node prev = SearchPrevIndex(key);
        Node delete = prev.next;
        prev.next = delete.next;
        return delete.data;
    }

    @Override
    public void removeAllKey(int key) {
        if(this.head == null){
            throw new UnsupportedOperationException("单链表为空");
        }
        if(this.head.data == key){
            this.head = this.head.next;
        }
        Node cur = this.head;
        while(cur != null){
            if(cur.data == key){
                Node prev = SearchPrevIndex(key);
                prev.next = cur.next;
            }
            cur = cur.next;
        }
    }

    @Override
    public int getLength() {
        Node cur = this.head;
        int length = 0;
        while(cur != null){
            length++;
            cur = cur.next;

        }
        return length;
    }

    @Override
    public void display() {
        Node cur = this.head;
        while(cur != null){
            System.out.print(cur.data +" ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {
        while(this.head.next!=null){
            Node del = this.head.next;
            this.head.next = del.next;
        }
        this.head = null;
    }

    public static void main(String[] args) {
        MySingleListImpl mySingleList = new MySingleListImpl(null);
        mySingleList.addFirst(12);
        mySingleList.addLast(13);
        mySingleList.addFirst(11);
        mySingleList.addLast(14);
        mySingleList.addLast(16);
        mySingleList.display();
        System.out.println(mySingleList.contains(16));
        System.out.println(mySingleList.getLength());
        mySingleList.addLast(16);
        mySingleList.addFirst(16);
        mySingleList.display();
        mySingleList.remove(16);
        mySingleList.display();
        mySingleList.removeAllKey(16);
        mySingleList.display();
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答这个问题。单向链表是一种常见的数据结构,它由一系列节点组成,每个节点包含一个数据元素和一个指向下一个节点的指针。以下是一个简单的单向链表的实现: ``` #include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* next; } Node; typedef struct LinkedList { Node* head; int size; } LinkedList; LinkedList* createLinkedList() { LinkedList* list = (LinkedList*)malloc(sizeof(LinkedList)); list->head = NULL; list->size = ; return list; } void addNode(LinkedList* list, int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; if (list->head == NULL) { list->head = newNode; } else { Node* current = list->head; while (current->next != NULL) { current = current->next; } current->next = newNode; } list->size++; } void removeNode(LinkedList* list, int data) { if (list->head == NULL) { return; } if (list->head->data == data) { Node* temp = list->head; list->head = list->head->next; free(temp); list->size--; return; } Node* current = list->head; while (current->next != NULL) { if (current->next->data == data) { Node* temp = current->next; current->next = current->next->next; free(temp); list->size--; return; } current = current->next; } } void printList(LinkedList* list) { Node* current = list->head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } int main() { LinkedList* list = createLinkedList(); addNode(list, 1); addNode(list, 2); addNode(list, 3); printList(list); removeNode(list, 2); printList(list); return ; } ``` 这个程序实现了一个简单的单向链表,包括创建链表、添加节点、删除节点和打印链表等功能。当然,这只是一个基础的实现,你可以根据自己的需求进行扩展和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值