Linked List->single linked list

#include<bits/stdc++.h>
using namespace std;

typedef struct List{
    int value;
    List* next;
}*Link;
Link head = NULL;
Link tail = NULL;

void InsertHead(int value){
    Link tmp = new List;
    tmp->value = value;
    tmp->next = head;
    head = tmp;
    if(NULL==tail){
        tail = head;
    }
}

void InsertTail(int value){
    Link tmp = new List;
    tmp->value = value;
    tmp->next = NULL;
    if(NULL==tail){
        tail = tmp;
        head = tmp;
    }else{
        tail->next = tmp;
        tail = tmp;
    }
}

int Search(int value){
    Link p = head;
    int index = 0;
    while(p){
        if(p->value==value){
            return index;
        }
        p = p->next;
        index++;
    }
    return -1;
}

void RemoveHead(){
    if(NULL==head){
        return ;
    }
    Link tmp = head;
    head = head->next;
    delete tmp;
    return  ;
}

void RemoveTail(){
    if(NULL==head){
        return;
    }
    Link prev = NULL;
    Link tmp;
    if(NULL==head->next){
        prev = head;    
        head = tail = NULL;
        delete prev;
        return ;
    }
    prev = head;
    tmp = prev->next;
    while(tmp->next!=NULL){
        prev = tmp;
        tmp = tmp->next;
    }
    prev->next = NULL;
    tail = prev;
    delete tmp;
    return; 
}

void print(){
    Link p = head;
    while(p){
        cout<<p->value<<' ';
        p = p->next;
    }
    cout<<endl;
}

int main(){

    InsertHead(5);
    print();
    InsertTail(8);
    cout<<Search(8)<<endl;
    print();
    RemoveHead();
    print();
}
Linked List is a data structure that consists of a sequence of nodes, where each node contains a piece of data and a reference to the next node in the sequence. Single Linked List is a type of linked list where each node only has a reference to the next node in the sequence, and the sequence can only be traversed in one direction. Here is the Java program to perform the operations you requested: ```java public class LinkedListExample { // Define a Node class static class Node { int data; Node next; public Node(int data) { this.data = data; this.next = null; } } Node head; // head of the linked list // Method to add a new node to the end of the linked list public void add(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; return; } Node last = head; while (last.next != null) { last = last.next; } last.next = newNode; } // Method to add a new node to the start of the linked list public void addFirst(int data) { Node newNode = new Node(data); newNode.next = head; head = newNode; } // Method to display the entire linked list public void display() { Node current = head; while (current != null) { System.out.print(current.data + " "); current = current.next; } System.out.println(); } public static void main(String[] args) { LinkedListExample list = new LinkedListExample(); // Add elements to the end of the linked list list.add(1); list.add(2); list.add(3); // Display the entire linked list list.display(); // Output: 1 2 3 // Add an element to the start of the linked list list.addFirst(0); // Display the entire linked list again list.display(); // Output: 0 1 2 3 } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值